I have been tasked with allowing a user to enter any number of arbitrary points on a canvas and link them together in the order that they were specified to draw a polygon.
However, each time the user tries to add a point I must validate whether the polygon can still be drawn without intersecting itself.
I have searched SO and found only this post which doesn't help me.
I need to form constraints everytime a new point is added to the canvas, and check that the next point doesn't validate those constraints.
I've added some poorly drawn illustrations of what I'm trying to achieve below. It might help to define the coordinate system I'm using: point 1
is the origin (0,0)
, x
is positive to the right, and y
is positive towards the top.
Adding Point 3
The first two points have only the constraint that 1 != 2
, Adding point 3
I have to make sure that it doesn't sit anywhere on the line that passes through both 1
and 2
.
Adding Point 4
Now, having added point 3
, point 4
is blocked out as illustrated below:
The Yellow areas are constrained by line 1-2
and the Green areas are constrained by the line 2-3
.
In pretty unreadable markup (there's no MathJax
or anything) I figured the constraints for 4
are:
Y_4 < ( (Y_2 - Y_1) / (X_2 - X_1) )*X_4 + Y_1
Y_3 < ( (Y_2 - Y_1) / (X_2 - X_1) )*X_3 ? Y_4 > ( (Y_3 - Y_2) / (X_3 - X_2) )*X_4 + Y_2 : Y_4 < ( (Y_3 - Y_2) / (X_3 - X_2) )*X_4 + Y_2
Y_4 =/= ( (Y_3 - Y_1) / (X_3 - X_1) )*X_4 + Y_1
Adding Point 5
Now adding on point 5 the constrained areas are:
And it's starting to get complicated to do.
I was wondering If there are any established algorithms for these kind of things, or if there are general equations in terms of vertex n
to generate the constraint equations. There could feasibly be tens, if not hundreds of points, so figuring out by brute force and hand coding doesn't seem to be an option.