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

本文介绍如何使用Bresenham线算法找到两点间所有像素坐标,适用于绘制单像素宽度的直线。该算法广泛应用于图形绘制,特别是对于需要精确控制像素的场景非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值