stevenhalim / cpbook-code

CP4 Free Source Code Project (C++17, Java11, Python3 and OCaml)
2k stars 493 forks source link

Area of polygon gives wrong answer #82

Closed SimonBerens closed 3 years ago

SimonBerens commented 3 years ago

In polygon.cpp I think the shoelace formula is missing the a_n b1 - b_n a_1 term, and in the alternative area function is missing the triangle with vertices O, P[0], P[P.size() -1]. Am I correct in thinking this?

howsiwei commented 3 years ago

I don't think so, because in polygon.cpp the convention is that a n-sided polygon is representated as a vector of n + 1 vertices where the last vertex equals the first. You can see this at https://github.com/stevenhalim/cpbook-code/blob/6fa2d291848a8da299fde2ebe2baa43478ff42c8/ch7/polygon.cpp#L178-L186