Determinate perimeter and area of an n-angle in c++

84 Views Asked by At

Okay, so this is what the textbook says: "The user will input n points in $R^2$, the program needs to determine wether or not this is a n-angle polygon and if so calculate the area and perimeter of such a figure".

To determine wether or not this is a n-angle polygon is rather easy, I made a point and a vector class, the program takes the first point and then forms n-1 vectors, if any of this vectors is parallel or antiparallel with any other, well it's not an n-angle polygon, otherwise it is, so far so good.

Now for the real problem, you need to determine which points form amongst them a side, to determine the perimiter and area, but this program is suppoused to work with any type of polygon, wether regular or not.

Is there any efficient way that actually works to know how two points form a side and not a diagonal? Having this I could use Gauss' formula or even Heron's.

Thank you in advance.

1

There are 1 best solutions below

6
On BEST ANSWER

enter image description here

As I understand, the vertices are to be assumed to be entered in order otherwise there are multiple $n$-gons possible of different shapes, perimeters and areas with given set of data points.

Under this assumption, the sides will be uniquely determined by consecutive vertices.

The perimeter can be found by using Pythagoras theorem for $n$ side lengths.

The area can be found by using Shoelace formula.