[英]List of coordinates between irregular points in python
Imagine we have two randomly selected points between 0 and 100 for both x and y. 想象一下,对于x和y,我们有两个随机选择的0到100之间的点。
For example: 例如:
(95,7), (35,6) (95,7),(35,6)
Now using the simple pygame.draw.line() function we could easily draw a line between these points without any gaps. 现在使用简单的pygame.draw.line()函数,我们可以轻松地在这些点之间画一条线而没有任何间隙。
My question is, how could we find a list of all the coordinates in a single pixel thick line between the two points without any gaps in the line? 我的问题是,我们怎样才能找到两点之间单个像素粗线中所有坐标的列表,而且线上没有任何间隙?
Secondly, is this even possible? 其次,这甚至可能吗?
I am using this list of pixel for the crack maze algorithm that needs to "shoot" another pixel while regarding any blocking walls that may interfere with its path. 我正在使用这个像素列表用于裂缝迷宫算法,该算法需要“射击”另一个像素,同时考虑可能干扰其路径的任何阻挡墙。
http://www.astrolog.org/labyrnth/algrithm.htm http://www.astrolog.org/labyrnth/algrithm.htm
By irregular, I refer to points which would not generate simple straight lines. 通过不规则,我指的是不会产生简单直线的点。
For example, it would be easy to find all the points between: 例如,很容易找到以下所有点:
(0,5) and (5,5) (0,5)和(5,5)
This has already been covered in this question: 这已经包含在这个问题中:
Use Bresenham's line algorithm . 使用Bresenham的线算法 。 You can find a simple python implementation here .
你可以在这里找到一个简单的python实现。 Here's a modified version of that implementation, which, given a starting and ending point, can return a list of intermediate points:
这是该实现的修改版本,给定起点和终点,可以返回中间点列表:
def line(x0, y0, x1, y1):
"Bresenham's line algorithm"
points_in_line = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
x, y = x0, y0
sx = -1 if x0 > x1 else 1
sy = -1 if y0 > y1 else 1
if dx > dy:
err = dx / 2.0
while x != x1:
points_in_line.append((x, y))
err -= dy
if err < 0:
y += sy
err += dx
x += sx
else:
err = dy / 2.0
while y != y1:
points_in_line.append((x, y))
err -= dx
if err < 0:
x += sx
err += dy
y += sy
points_in_line.append((x, y))
return points_in_line
Perhaps it's an overkill but I'd just find the line equation and use a generator expression. 也许这是一个矫枉过正,但我只是找到线方程并使用生成器表达式。 To find the equation you can use this example algorithm which will return something like
要找到等式,您可以使用此示例算法 , 该算法将返回类似的内容
lambda x: 2*x +1
With that we can do: 有了这个,我们可以做到:
f = find_line_equation(A, B) #A B are tuples
points = [(x, f(x)) for x in range(min(A[0], B[0]), max(A[0], B[0]))]
This is assuming you want only integer points. 这假设您只需要整数点。 You can also use this loop:
你也可以使用这个循环:
points = []
x = min(A[0], B[0])
increment = 0.1
while x <= max(A[0], B[0]):
points.append((x, f(x))
x += increment
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.