How to link four points to a convex 开发者_StackOverflowpolygon? I mean how to identify the order of these four points.
Thanks.
Zhong
Take the center point (i.e. average of x and y coords), then calculate x/y values for y<centery
, then for y>=centery
. would be fastest I guess.
(that is, if I understood the question in the first place...)
Sort them vertically, connect 2 top most to each other and two lowest to each other.
Sort horizontally and then connect 2 leftmost to each other and two rightmost to each other.
EDIT: anyways, SO's cool related section on the right suggests an answered duplicate: Sort Four Points in Clockwise Order
The atan2() method is handy for this, and is found in most languages.
atan2(y,x)
and converts rectangular coordinates (x,y)
to the angle theta
from the polar coordinates (r,theta)
.
Given 4 points, find their average. Then calculate the four (x,y) vectors obtained by subtracting the average from each of the four points.
For each of these (x,y) vectors, calculate the angle θ = atan2(y,x). θ will be between -π/2 and π/2.
Sort the θ's. This will give you the order of the points, in clockwise order.
This only works for convex quadrilaterals.
精彩评论