This is prob more of a geometry question, but
I have an array of 4 points (from user input), that could be in any order.
I’m trying to work out a procedure that would return what each point is likely to be, i.e.
top left,
top right,
bottom left,
bottom Right
I’m working in PHP now, but any help in any language is fine.
{‘Ill add what i’ve got so far in PHP shortly}
Would this question be better asked on another stackexchange, math maybe?
You could sort the points by their x coordinates ascending and if there’s a tie, the y coordinate ascending.
The first two points are the “left” side – the one with the greater y is the “top” and the other is “bottom”.
The last two points are the “right” side, same deal.
This won’t work for a diamond though! (But what’s “top-left” on a diamond anyway?)
In general, for a convex set of points, you can calculate the centroid (x value is the average of all x values, y value is the average of all y values).
Then, you can calculate the argument (i.e. angle) of the line between the centroid and the point, being
arctan2of(y_pt-y_centroid,x_pt-x_centroid).If you then sort the points by their arguments, they’ll be in anticlockwise order, with -pi being the West point of the compass, 0 being the East, wrapping around again to +pi being the west point.