M
M
Marat Nagayev2021-12-23 23:40:13
C++ / C#
Marat Nagayev, 2021-12-23 23:40:13

How to build a polygon from points?

The user selects points with a mouse click. The dots must be connected to form a figure.
1woi3ylfmwa1ofb0d2pjkiwru24.png
How to avoid this?
You can display an error that the figure cannot be built, but how to determine this?
I am using Windows forms C++, drawPolygon method.
UPD: I found out that drawPolygon connects the points in order, and not in such a way that the correct figure is obtained.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-12-24
@nagayev

The picture is not visible, but it is clear from the comments that you want to get only convex polygons?
You can check that something is wrong through the vector product of successive sides.
For all 3 consecutive vertices a, b and c, then you need to check that (ba)*(cb) always gives the same sign (either always <=0 or always >=0). This is a vector product .
The formula in your case would be (xb-xa)*(yc-yb)-(xc-xb)*(yb-ya).
You can also build a convex hull.
Here is the algorithm how to build it from given points: https://e-maxx.ru/algo/convex_hull_graham
Edit: You can also, instead of sorting the points into a convex hull, ignore clicks from the user that make the current polygon non-convex. Again, the non-convexity check can only be done for new sides: At the last, new and first points.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question