Sort points by angle from given axis?

前端 未结 8 3256
無奈伤痛
無奈伤痛 2021-02-19 12:11

How can I sort an array of points/vectors by counter-clockwise increasing angle from a given axis vector?

For example:

8条回答
  •  轮回少年
    2021-02-19 12:35

    If S is an array of PointF, and mid is the PointF in the centre:

    S = S.OrderBy(s => -Math.Atan2((s.Y - mid.Y), (s.X - mid.X))).ToArray();
    

    will sort the list in order of rotation around mid, starting at the point closest to (-inf,0) and go ccw (clockwise if you leave out the negative sign before Math).

提交回复
热议问题