Quote:

Originally Posted by **xpi0t0s**
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);
}