Get Time
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
bug? | Reply
Hi, I think u have a little bug in your code that calculates the area of a polygon:

int area = 0;
int N = lengthof(p);
//We will triangulate the polygon
//into triangles with points p[0],p[i],p[i+1]

for(int i = 1; i+1<N; i++){
int x1 = p[i][0] - p[0][0];
int y1 = p[i][1] - p[0][1];
int x2 = p[i+1][0] - p[0][0];
int y2 = p[i+1][1] - p[0][1];
int cross = x1*y2 - x2*y1;
area += cross;
return abs(cross/2.0);

the return value should be calculated with area and not cross. cross is already out of scope. Greez, axel>
Re: bug? (response to post by axelmoser) | Reply
yeah,I agree with you
Re: bug? (response to post by axelmoser) | Reply
This mistake remains :s

return abs(cross/2.0);

Should be

return abs(area/2.0);
Re: bug? (response to post by mogers) | Reply
If I am going to make a function to calculate polygon area, I would left the return as area / 2.0. That way, the function also serves to identify if a polygon is defined clockwise or counterclockwise.
Re: A slight mistake and a doubt (response to post by axelmoser) | Reply
I am sure many people must have noticed it, but there is a small mistake in the polygon area description. In the 4th line below the polygon figure it should be "the same as ABC+ACD" instead of "the same as ABC+ABD".

Also, I found the desciption of the line segment joining (1,3) to (5,1) as (4,-2) (under the sub-heading "Vectors" at the beginning of the article) a bit confusing. Has it been represented simply as (x2-x1,y2-y1)? Or is it something else?