0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Well I've already told you what code I think you should write next. Several times. So there's no point repeating it.

No I'm not going to write ANY of the code for you. This is YOUR task. You learn programming by programming, not by having others write the code for you. Sorry if that seems harsh, but if I write the code for you this time then that'll create a dependency where every time you're stuck you'll expect me (or some other gullible idiot) to do your homework for you, which is great as far as it goes but on exam day when you have no internet access you will fail the exam.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
well at least tell me how many functions i need and where do i have the most errors
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
How many functions do you need? Well, you could do the whole lot in main, so 1, I suppose.

Where do you have the most errors: difficult to say, they're all over the code. They're mainly caused, IMHO, by you trying to do too much in one go. Keep it simple: write just a few lines of code (5-10) and make sure they compile, run and produce expected results before continuing. Add extra output statements so that you can see what is going on.

But I'm beginning to wonder if this task is simply beyond your level of ability; maybe you should focus on some simpler tasks and come back to this one when you've done a few easier exercises.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
How many functions do you need? Well, you could do the whole lot in main, so 1, I suppose.

Where do you have the most errors: difficult to say, they're all over the code. They're mainly caused, IMHO, by you trying to do too much in one go. Keep it simple: write just a few lines of code (5-10) and make sure they compile, run and produce expected results before continuing. Add extra output statements so that you can see what is going on.

But I'm beginning to wonder if this task is simply beyond your level of ability; maybe you should focus on some simpler tasks and come back to this one when you've done a few easier exercises.

i have done this but id doesn't run, can you tell me what's wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EPSILON    0.000001        /* a quantity small enough to be zero */
#define    MAXN    100                /* maximum number of circles */
#define DIMENSION    2            /* dimension of points */
#define X        0                /* x-coordinate index */
#define    Y        1                /* y-coordinate index */
#define PI 3,1415
#define max(A, B) ((A) > (B) ? (A) : (B))
#define min(A, B) ((A) < (B) ? (A) : (B))

typedef struct {
    double a;                    /* x-coefficient */
    double b;                    /* y-coefficient */
    double c;                    /* constant term */
} line;


typedef double point[DIMENSION];

typedef struct {
    point c;                    /* center of circle */
    double r;                    /* radius of circle */
} circle;


point s;                         /* initial position */
point t;                         /* target position */
int ncircles;                     /* number of circles */
circle c[MAXN];                     /* circles data structure */

void ReadFile( const char *filename, point s, point t, int *ncircles )
{
    FILE *file;
    int i;
    if (( file = fopen( filename, "r" )) == NULL )
    {
        printf ( "Error opening file!\n" );
        exit(1);
        }

    scanf("%lf l%f",&s[X],&s[Y]);
    scanf("%lf l%f",&t[X],&t[Y]);
    scanf("%d",&ncircles);
    for (i=1; i<=*ncircles; i++){
        scanf("%lf %lf %lf",&c[i].c[X],&c[i].c[Y],&c[i].r);
    }
    fclose( file );
}

points_to_line(point p1, point p2, line *l)
{
        l->b = 1;
        l->a = -(p1[Y]-p2[Y])/(p1[X]-p2[X]);
        l->c = -(l->a * p1[X]) - (p1[Y]);
}

point_and_slope_to_line(point p, double m, line *l)
{
    l->a = -m;
    l->b = 1;
    l->c = -((l->a*p[X]) + (l->b*p[Y]));
}

void intersection_point(line l1, line l2, point p)
{
    p[X] = (l2.b*l1.c - l1.b*l2.c) / (l2.a*l1.b - l1.a*l2.b);
    p[Y] = -(l1.a * p[X] + l1.c) / l1.b;
}


void closest_point(point p_in, line l, point p_c)
{
    line perp; // perpendicular to l through (x,y)
    if(fabs(l.b) <= EPSILON) // vertical line
    {
        p_c[X]=-(l.c);
        p_c[Y]=p_in[Y];
    return;
    }

    if(fabs(l.a) <= EPSILON) // horizontal line
    {
        p_c[X]=p_in[X];
        p_c[Y]=-(l.c);
    return;
    }

point_and_slope_to_line(p_in,1/l.a,&perp);
intersection_point(l,perp,p_c);
}

double distance(point p1, point p2)
{
    return sqrt(pow(p1[Y]-p2[Y],2) + pow(p1[X]-p2[X],2));
}
int main()
{
    line l;
    point close;              /* closest point */
    double d;                 /* distance from circle-center */
    double xray = 0.0;    /* length of intersection with circles */
    double around = 0.0;    /* length around circular arcs */
    double angle;                /* angle subtended by arc */
    double dist;                  /* total travel distance */
    int i;
    ReadFile( "route.dat", s, t, &ncircles );

    points_to_line(s,t,&l);

    for (i=1; i<=ncircles; i++) {
        closest_point(c[i].c,l,close);
        d = distance(c[i].c,close);
        if ((d < c[i].r))  {
            xray += 2*sqrt(c[i].r*c[i].r - d*d);
            angle = acos(d/c[i].r);
            around += ((2*angle)/(2*PI)) * (2*PI*c[i].r);
        }
    }

    dist = distance(s,t) - xray + around;
    printf("%f\n", dist);
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
"doesn't run" is a bit vague; could you be more specific? If you add some printf statements to the code to let you know where it's up to, does that help you find out where it goes wrong? Presumably the fact that you're expecting it to run means that you got no compiler errors this time.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
"doesn't run" is a bit vague; could you be more specific? If you add some printf statements to the code to let you know where it's up to, does that help you find out where it goes wrong? Presumably the fact that you're expecting it to run means that you got no compiler errors this time.
well, i mean that when i try to run it it throws me a blank page!!!
yes no errors!!
i hope that with your compiler there won't be too many errors!!
what do you suggest me to do? how to write printf?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Same way you do at the end of the program. Just put some messages indicating where it's got up to. For example:
Code:
int main()
{
    line l;
    point close;              /* closest point */
    double d;                 /* distance from circle-center */
    double xray = 0.0;    /* length of intersection with circles */
    double around = 0.0;    /* length around circular arcs */
    double angle;                /* angle subtended by arc */
    double dist;                  /* total travel distance */
    int i;
printf("Entered main\n");
    ReadFile( "route.txt", s, t, &ncircles );
printf("Back from ReadFile\n");
//......and so on
You might also find it useful to display numeric values as it goes along, so you can check the results are correct. So again for example:
Code:
    ReadFile( "route.txt", s, t, &ncircles );
printf("Back from ReadFile\n");
	printf("Readfile put values %lf %lf into s[X,Y]\n",s[X],s[Y]);
	printf("Readfile put values %lf %lf into t[X,Y]\n",t[X],t[Y]);
Of course, all this is a lot easier if you can step through the code in a debugger. What compiler are you using, and what OS?
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Same way you do at the end of the program. Just put some messages indicating where it's got up to. For example:
Code:
int main()
{
    line l;
    point close;              /* closest point */
    double d;                 /* distance from circle-center */
    double xray = 0.0;    /* length of intersection with circles */
    double around = 0.0;    /* length around circular arcs */
    double angle;                /* angle subtended by arc */
    double dist;                  /* total travel distance */
    int i;
printf("Entered main\n");
    ReadFile( "route.txt", s, t, &ncircles );
printf("Back from ReadFile\n");
//......and so on
You might also find it useful to display numeric values as it goes along, so you can check the results are correct. So again for example:
Code:
    ReadFile( "route.txt", s, t, &ncircles );
printf("Back from ReadFile\n");
    printf("Readfile put values %lf %lf into s[X,Y]\n",s[X],s[Y]);
    printf("Readfile put values %lf %lf into t[X,Y]\n",t[X],t[Y]);
Of course, all this is a lot easier if you can step through the code in a debugger. What compiler are you using, and what OS?
i am using the Codeblocks in the Windows7
i just found out that if i put a printf just after the Readfile in the main it doesn't post it. Damn!! what does this mean? all i have done is wrong???!!!
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It means for some reason it's not getting past Readfile. Try adding some printfs to ReadFile and see if you can spot anything.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
It means for some reason it's not getting past Readfile. Try adding some printfs to ReadFile and see if you can spot anything.
well the error starts just before the scanf, so it doesn't read anything and that's why it posts nothing.
But why is this happening? how to fix it?