c - I'm losing the type of my object at some point -
// construct array of length max_points. // populate array given points , initialize number points being handled. // print given points user. struct point points[max_points]; int numpoints = readpoints(points); printf("set of points: \n"); displaypoints(points, numpoints); // construct array represent hull max possible points (numpoints). struct point hull[numpoints]; struct point pointonhull = leftmostpoint(points, numpoints); struct point endpoint; int e = 0; // perform jarvis march. { hull[e] = pointonhull; endpoint = points[0]; ( int = 1; < numpoints; i++ ) if ( (endpoint == pointonhull) || (ccw(hull[e], endpoint, points[i]) > 0) ) endpoint = points[i]; ++e; } while ( endpoint != hull[0] ); // we've looped beginning. // print mapped hull points. printf("convex hull: \n"); displaypoints(hull, numpoints);
that's code jarvis march (gift wrapping) program solve convex hull. gcc raising errors on both of struct point comparisons (endpoint == pointonhull
, endpoint != hull[0])
. i'm brand new c, , 1 of first projects i'm doing play around language. can me see i'm messing up?
the specific errors:
jarvis.c:31:19: error: invalid operands binary == (have 'struct point' , 'struct point')
jarvis.c:34:21: error: invalid operands binary != (have 'struct point' , 'struct point')
there no comparison operator structs in c
. have define own comparison function , use in place of ==
. try this:
bool equalpoints(const struct point p1, const struct point p2) { return (p1.x == p2.x) && (p1.y == p2.y); } if (equalpoints(endpoint, pointonhull)) { /* code here */ }
or if structure more complex (though, not case):
bool equalpoints(const struct point *p1, const struct point *p2) { /* * in case structure more complex here goes * more complex comparison code */ return (p1->x == p2->x) && (p1->y == p2->y); } if (equalpoints(&endpoint, &pointonhull) { /* code here */ }
the latter won't copy whole point
structure pass equalpoints
, pass pointer (reference type) instead. const
important because won't let accidentally modify points when want compare them.
Comments
Post a Comment