我有这些坐标:
坐标= [(10,10),(13,10),(13,13)
现在我需要新的坐标。 两个坐标之间的路总是之一。 例如:
(10,10)
(11,10)
(12,10)
(13,10)
(13,11)
(13,12)
(13,13)
任何想法?
#我找到了解决办法。
对于n的范围(LEN(坐标)-1):
lengthx =坐标[N + 1] [0] - 坐标[n]的[0]
冗长=坐标[N + 1] [1] - 坐标[n]的[1]
长度=(lengthx ** 2 +冗长** 2)** 5
为米范围(长度):
打印坐标[N] [0] + lengthx /长* M,坐标[N] [1] +冗长/长* M
解决方案
这是一个简单的变化布氏线算法将实现你想要的只是使用的是什么整数运算(所以它应该是明显快):
高清步骤(路径):
如果len(路径)> 0:
因为我在范围内(1,LEN(路径)):
为一步steps_between(路径[我 - 1],路径[I]):
产量步
产量路径[-1]
高清steps_between(开始,结束):
X0,Y0 =启动
X1,Y1 =结束
陡峭= ABS(Y1 - Y0)> ABS(X1 - X0)
如果陡峭的:
X0,Y0 = Y0,X0
X1,Y1 = Y1,X1
如果Y0> Y1:
X0,X1 = X1,X0
Y0,Y1 = Y1,Y0
如果Y0< Y1:
ystep = 1
其他:
ystep = -1
DELTAX = X1 - X0
DELTAY = ABS(Y1 - Y0)
错误= -deltax / 2
Y = Y0
x的范围(X0,X1):
如果陡峭的:
收益率(Y,X)
其他:
产率(X,Y)
错误+ = DELTAY
如果错误> 0:
Y + = ystep
错误 - = DELTAX
如果陡峭的:
收益率(Y,X)
其他:
产率(X,Y)
的的coords = [(10,10),(13,10),(13,13)]
打印\ N。加入(STR(步骤)的步骤,步骤(coords)使用)
上面打印:
(10,10)
(11,10)
(12,10)
(13,10)
(13,11)
(13,12)
(13,13)
当然,布氏按预期工作当两个 X
和是
路径上的两点之间的变化:
的的coords = [(10,10),(13,12),(15,13)]
打印\ N。加入(STR(步骤)的步骤,步骤(coords)使用)
这将显示:
(10,10)
(11,10)
(11,11)
(12,11)
(12,12)
(13,12)
(14,12)
(14,13)
(15,13)
I have these coordinates:
coord = [(10,10), (13,10), (13,13)]
Now i need new coordinates. The way between two coordinates is always one. For example:
(10,10)
(11,10)
(12,10)
(13,10)
(13,11)
(13,12)
(13,13)
Any ideas?
#I found the solution.
for n in range(len(coord)-1):
lengthx = coord[n+1][0] - coord[n][0]
lengthy = coord[n+1][1] - coord[n][1]
length = (lengthx**2 + lengthy**2)**.5
for m in range(length):
print coord[n][0]+lengthx/length*m, coord[n][1]+lengthy/length*m
解决方案
A simple variation on Bresenham's line algorithm will achieve what you want using integer arithmetic only (so it should be noticeably faster):
def steps(path):
if len(path) > 0:
for i in range(1, len(path)):
for step in steps_between(path[i - 1], path[i]):
yield step
yield path[-1]
def steps_between(start, end):
x0, y0 = start
x1, y1 = end
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if y0 > y1:
x0, x1 = x1, x0
y0, y1 = y1, y0
if y0 < y1:
ystep = 1
else:
ystep = -1
deltax = x1 - x0
deltay = abs(y1 - y0)
error = -deltax / 2
y = y0
for x in range(x0, x1):
if steep:
yield (y, x)
else:
yield (x, y)
error += deltay
if error > 0:
y += ystep
error -= deltax
if steep:
yield (y, x)
else:
yield (x, y)
coords = [(10, 10), (13, 10), (13, 13)]
print "\n".join(str(step) for step in steps(coords))
The above prints:
(10, 10)
(11, 10)
(12, 10)
(13, 10)
(13, 11)
(13, 12)
(13, 13)
Of course, Bresenham works as expected when both x
and y
change between two points on the path:
coords = [(10, 10), (13, 12), (15, 13)]
print "\n".join(str(step) for step in steps(coords))
That prints:
(10, 10)
(11, 10)
(11, 11)
(12, 11)
(12, 12)
(13, 12)
(14, 12)
(14, 13)
(15, 13)