python坐标系不均匀_python中不规则点之间的坐标列表

Imagine we have two randomly selected points between 0 and 100 for both x and y.

For example:

(95,7), (35,6)

Now using the simple pygame.draw.line() function we could easily draw a line between these points without any gaps.

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.

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)

This has already been covered in this question:

解决方案

Use Bresenham's line algorithm. You can find a simple python implementation here. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
栅格转的实现需要考虑到栅格边缘因裁剪而不规则的情况。下面是一个简单的Python实现,可以将栅格转换为: ```python def raster_to_point(raster): # 获取栅格宽度和高度 width, height = raster.shape # 存储所有列表 points = [] # 循环遍历栅格 for y in range(height): for x in range(width): # 如果栅格值不为零,则将其转换为 if raster[y, x] != 0: # 计算坐标,注意加上偏移量 point_x = x + 0.5 point_y = height - y - 0.5 # 添加列表 points.append((point_x, point_y)) return points ``` 这个函数将栅格作为输入,返回一个坐标列表。在这个函数,我们使用双重循环遍历栅格的每个像素。如果像素的值不为零,则将其转换为一个坐标,并将其添加到列表。请注意,我们需要对y值进行一些计算以使其与常见的坐标系一致。 在处理栅格边缘不规则的情况时,我们可以使用一个裁剪函数来剪切栅格。这个函数可以在裁剪栅格的同时,返回一个偏移量,用于将输出坐标转换回原始坐标系。下面是一个简单的Python实现: ```python def clip_raster(raster): # 获取栅格宽度和高度 width, height = raster.shape # 计算左上角和右下角的坐标 left = None top = None right = None bottom = None for y in range(height): for x in range(width): if raster[y, x] != 0: if left is None or x < left: left = x if top is None or y < top: top = y if right is None or x > right: right = x if bottom is None or y > bottom: bottom = y # 如果没有像素,则返回空列表 if left is None or top is None or right is None or bottom is None: return [], (0, 0) # 计算偏移量 offset_x = left + 0.5 offset_y = height - bottom - 0.5 # 裁剪栅格 clipped_raster = raster[top:bottom+1, left:right+1] # 转换为 points = raster_to_point(clipped_raster) # 转换坐标到原始坐标系 transformed_points = [] for point in points: transformed_points.append((point[0] + offset_x, point[1] + offset_y)) return transformed_points, (offset_x, offset_y) ``` 这个函数将栅格作为输入,返回一个坐标列表以及一个偏移量。在这个函数,我们首先计算栅格的左上角和右下角坐标。然后,我们使用这些坐标来计算偏移量,并使用裁剪函数将栅格裁剪为一个规则的矩形。最后,我们将裁剪后的栅格转换为,并使用偏移量将坐标转换回原始坐标系。 需要注意的是,这个实现是比较简单的,可能对于某些特殊情况无法有效处理。在实际应用,需要根据具体情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值