python二维列表排序_使用Python按顺时针方向排序二维坐标列表?

这应该说明问题,给出一个可视化工具

但对于在同一距离上获得一组点的正确入口点来说,这并不总是有效的import random

import pylab

import cmath

from itertools import groupby

pts = [(random.randrange(-5,5), random.randrange(-5,5)) for _ in range(10)]

# for this problem complex numbers are just too good to pass up

z_pts = [ i[0] + 1j*i[1] for i in pts if i != (0, 0)]

z_pts.sort(key = lambda x: abs(x))

gpts = [[*g] for _, g in groupby(z_pts, key = lambda x: abs(x) ) ]

print(*gpts, sep='\n')

spts = [1j/2]

for e in gpts:

if len(e) > 1:

se = sorted(e, key = lambda x: cmath.phase(-x / spts[-1]))

spts += se

else:

spts += e

print(spts)

def XsYs(zs):

xs = [z.real for z in zs]

ys = [z.imag for z in zs]

return xs, ys

def SpiralSeg(a, b):

'''

construct a clockwise spiral segment connecting

ordered points a, b specified as complex numbers

Inputs

a, b complex numbers

Output

list of complex numbers

'''

seg = [a]

if a == 0 or a == b:

return seg

# rotation interpolation with complex numbers!

rot = ( b / a ) ** ( 1 / 30 )

# impose cw rotation direction constraint

if cmath.phase( b / a ) > 0: # add a halfway point to force long way around

plr = cmath.polar( b / a )

plr = (plr[0]**(1/2), plr[1] / 2 - 1 * cmath.pi ) # the rotor/2

a_b = cmath.rect(*plr) * a # rotate the start point halfway round

return SpiralSeg(a, a_b) + (SpiralSeg(a_b, b))

for _ in range(30):

a *= rot

seg.append(a)

return seg

segs = [SpiralSeg(a, b) for a, b in zip(spts, spts[1:])]

pylab.axes().set_aspect('equal', 'datalim')

pylab.scatter(*XsYs(z_pts))

for seg in segs:

pylab.plot(*XsYs(seg))

[(1-2j), (-2-1j)]

[(2-3j)]

[(1+4j)]

[(3+3j)]

[(-3-4j), (3-4j), (4-3j)]

[(1-5j)]

[(-4-4j)]

[0.5j, (-2-1j), (1-2j), (2-3j), (1+4j), (3+3j), (-3-4j), (3-4j), (4-3j), (1-5j), (-4-4j)]

01V9h.png[-1j]

[(-1-1j)]

[(-1-2j), (-1+2j), (2+1j)]

[(-4+0j)]

[(1-4j)]

[-5j, (-4-3j)]

[(1-5j)]

[0.5j, -1j, (-1-1j), (-1-2j), (2+1j), (-1+2j), (-4+0j), (1-4j), (-4-3j), -5j, (1-5j)]

EAlC6.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值