We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
for people from the future: if the above link is no longer working, here's the basic gist. For every adjacent pair of points in the polygon, we "integrate" the line connecting the two points, with a + sign for going left to right and a - sign for the other way (or you can choose the other direction, as long as you're consistent and take the absolute value at the end).
Make sure to "complete the cycle", e.g. there should be N pairs of points "integrated" this way, not N-1, if the polygon has N points.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Compute the Area of a Polygon
You are viewing a single comment's thread. Return to all comments →
there's a neat trick to get the area even when it's concave.
see this site, which has very nice intuitive explanation https://www.mathsisfun.com/geometry/area-irregular-polygons.html
for people from the future: if the above link is no longer working, here's the basic gist. For every adjacent pair of points in the polygon, we "integrate" the line connecting the two points, with a + sign for going left to right and a - sign for the other way (or you can choose the other direction, as long as you're consistent and take the absolute value at the end).
Make sure to "complete the cycle", e.g. there should be N pairs of points "integrated" this way, not N-1, if the polygon has N points.