python二维列表排序,使用Python按顺时针角度对二维坐标列表进行排序?

I have a set of points with x and y coordinates that can be seen in the figure below. The coordinates of the 9 points were stored in a list as follows:

L = [[5,2], [4,1], [3.5,1], [1,2], [2,1], [3,1], [3,3], [4,3] , [2,3]]

The idea is to sort the points clockwise from an origin. In this case, the origin is the point that is colored and that has an arrow that indicates the direction of the ordering. Do not worry about creating methodology to determine the origin because it is already solved.

Thus, after being ordered, the list L should be as follows:

L = [[2,3], [3,3], [4,3], [5,2], [4,1], [3.5,1], [3,1], [2,1], [1,2]]

Note that the x and y coordinates are not changed. What changes is the storage order.

Do you have any idea of an algorithm, script or methodology for this problem in the python language?

b7f78dd243cfa336c22352f23d375f28.png

解决方案

With a bit of trigonometry it's not that hard. Maybe you know but the angle between two (normalized) vectors is acos(vec1 * vec2). However this calculates only the projected angle but one could use atan2 to calculate the direction-aware angle.

To this means a function calculating it and then using it as key for sorting would be a good way:

import math

pts = [[2,3], [5,2],[4,1],[3.5,1],[1,2],[2,1],[3,1],[3,3],[4,3]]

origin = [2, 3]

refvec = [0, 1]

def clockwiseangle_and_distance(point):

# Vector between point and the origin: v = p - o

vector = [point[0]-origin[0], point[1]-origin[1]]

# Length of vector: ||v||

lenvector = math.hypot(vector[0], vector[1])

# If length is zero there is no angle

if lenvector == 0:

return -math.pi, 0

# Normalize vector: v/||v||

normalized = [vector[0]/lenvector, vector[1]/lenvector]

dotprod = normalized[0]*refvec[0] + normalized[1]*refvec[1] # x1*x2 + y1*y2

diffprod = refvec[1]*normalized[0] - refvec[0]*normalized[1] # x1*y2 - y1*x2

angle = math.atan2(diffprod, dotprod)

# Negative angles represent counter-clockwise angles so we need to subtract them

# from 2*pi (360 degrees)

if angle < 0:

return 2*math.pi+angle, lenvector

# I return first the angle because that's the primary sorting criterium

# but if two vectors have the same angle then the shorter distance should come first.

return angle, lenvector

A sorted run:

>>> sorted(pts, key=clockwiseangle_and_distance)

[[2, 3], [3, 3], [4, 3], [5, 2], [4, 1], [3.5, 1], [3, 1], [2, 1], [1, 2]]

and with a rectangular grid around the origin this works as expected as well:

>>> origin = [2,3]

>>> refvec = [0, 1]

>>> pts = [[1,4],[2,4],[3,4],[1,3],[2,3],[3,3],[1,2],[2,2],[3,2]]

>>> sorted(pts, key=clockwiseangle_and_distance)

[[2, 3], [2, 4], [3, 4], [3, 3], [3, 2], [2, 2], [1, 2], [1, 3], [1, 4]]

even if you change the reference vector:

>>> origin = [2,3]

>>> refvec = [1,0] # to the right instead of pointing up

>>> pts = [[1,4],[2,4],[3,4],[1,3],[2,3],[3,3],[1,2],[2,2],[3,2]]

>>> sorted(pts, key=clockwiseangle_and_distance)

[[2, 3], [3, 3], [3, 2], [2, 2], [1, 2], [1, 3], [1, 4], [2, 4], [3, 4]]

Thanks @Scott Mermelstein for the better function name and @f5r5e5d for the atan2 suggestion.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值