矩形の四頂点に対応する座標を『コ』の字状に並べ替える


富豪的に。

| vertices center pi thetaOf |

vertices := {100@100. 200@100. 200@200. 100@200} shuffled.   "任意順に与えられた四座標"

center := (vertices min + vertices max) / 2.
pi := Float pi.
thetaOf := [:point | ((point - center) theta - pi) \\ (2 * pi)].

^ vertices sort: [:aa :bb | (thetaOf value: aa) < (thetaOf value: bb)]


同じ方針で、コの字にこだわらない(匚の字でもいい)のなら…。

| vertices center |

vertices := {100@100. 200@100. 200@200. 100@200} shuffled.

center := (vertices min + vertices max) / 2.

^ vertices sort: [:aa :bb | (aa - center) theta < (bb - center) theta]
=> #(200@200 100@200 100@100 200@100)


小市民的に。

| vertices rect |

vertices := {100@100. 200@100. 200@200. 100@200} shuffled.

rect := vertices min corner: vertices max.

^ {rect topLeft. rect topRight. rect bottomRight. rect bottomLeft}


同じく、コの字にこだわらない(凵の字でもいい)のなら…。

| vertices |

vertices := {100@100. 200@100. 200@200. 100@200} shuffled.

^ (vertices min corner: vertices max) corners
=> #(100@100 100@200 200@200 200@100)