python中不规则点之间的坐标列表 [英] List of coordinates between irregular points in python

查看:394
本文介绍了python中不规则点之间的坐标列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,对于x和y,我们有两个介于0和100之间的随机选择点.

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

例如:

(95,7),(35,6)

(95,7), (35,6)

现在,使用简单的pygame.draw.line()函数,我们可以轻松地在这些点之间画一条线而没有任何间隙.

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?

第二,这有可能吗?

我将这个像素列表用于裂纹迷宫算法,该算法需要拍摄"另一个像素,同时考虑可能会干扰其路径的任何障碍墙.

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

在不规则的情况下,我指的是不会生成简单直线的点.

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)和(5,5)

(0,5) and (5,5)

此问题已涵盖此问题:

在一组坐标之间列出坐标

推荐答案

使用 Bresenham的行算法.您可以在此处找到一个简单的python实现.这是该实现的修改版本,给定起点和终点,它可以返回中间点的列表:

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

这篇关于python中不规则点之间的坐标列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆