直线中每个相交点的距离,按2D坐标递增 [英] Distance for each intersected points of a line in increased order in 2D coordinate

查看:125
本文介绍了直线中每个相交点的距离,按2D坐标递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算2D(x,y)中A(x0,y0)和B(x1,y1)点之间的距离.

I am calculating the distance between A(x0,y0) and B(x1,y1) points in a 2D (x,y).

我有以下代码,表示给定两个边界点A(x0,y0)和B(x1,y1)中每个像元的相交点.

I have the following code which represents intersected points of each cell in the given two boundary points, A(x0,y0) and B(x1,y1).

def intersected_points(x0, x1, y0, y1):
    # slope 
    m = (y1 - y0 )/( x1 - x0)
    # Boundary of the selected points
    x_ceil = ceil( min (x0, x1 ))
    x_floor = floor( max(x0, x1))
    y_ceil = ceil( min(y0, y1))
    y_floor = floor( max(y0, y1))

    # calculate all intersected x coordinate
    ax = []
    for x in arange(x_ceil, x_floor + 1, 1):
          ax.append([ x, m * ( x - x0 ) + y0 ])
    # calculate all intersected y coordinate
    for y in arange(y_ceil, y_floor + 1, 1):
          ax.append([ ( y - y0 ) * ( 1./m ) + x0, y ])
    return ax

样本值:

intersected_points(1.5, 4.4, 0.5, 4.1);

输出:

[
    [2.0, 1.1206896551724137],
    [3.0, 2.3620689655172411],
    [4.0, 3.6034482758620685],
    [1.9027777777777779, 1.0],
    [2.7083333333333335, 2.0],
    [3.5138888888888893, 3.0],
    [4.3194444444444446, 4.0]
]

我得到的输出是混合的和未排序的值,因此,对于每个单元格坐标,线都是交叉的. 但是,我要获取的结果应该是(包括边界点):(x0,y0),所有相交的点,(x1,y1) 其中x0,y0-初始值,x1,y1-终点.其他值是相交的线坐标!

The output I got is mixed and unsorted values, so, for each cell coordinates, where line crosses. BUT, The result I want to get should be something (including boundary points): (x0,y0), all intersected points, (x1,y1) where x0, y0 - intial, x1,y1 - final point. Other values are intersected line coordinates!

推荐答案

这是您代码的修改版本.它更多地使用了numpy,并将端点包含在排序的点数组中.

Here's a modified version of your code. It uses numpy a bit more, and it includes the end points in the sorted array of points.

import numpy as np

def intersected_points(x0, x1, y0, y1):
    # slope
    m = (y1 - y0) / (x1 - x0)
    # Boundary of the selected points
    x_ceil = np.ceil(min(x0, x1))
    x_floor = np.floor(max(x0, x1))
    y_ceil = np.ceil(min(y0, y1))
    y_floor = np.floor(max(y0, y1))

    # calculate all intersected x coordinate
    x = np.arange(x_ceil, x_floor + 1)
    y = m * (x - x0) + y0
    ax = zip(x, y)
    # calculate all intersected y coordinate
    y = np.arange(y_ceil, y_floor + 1)
    x = (y - y0) / m + x0
    ax.extend(zip(x, y))
    ax.append((x0, y0))
    ax.append((x1, y1))
    ax.sort()
    return np.array(ax)


if __name__ == "__main__":
    import matplotlib.pyplot as plt

    x0, y0 = 1.5, 0.5
    x1, y1 = 4.4, 4.1

    points = intersected_points(x0, x1, y0, y1)
    print points

    rect = plt.Rectangle((x0, y0), x1 - x0, y1 - y0,
                         facecolor="#60ff60", alpha=0.2)
    plt.gca().add_patch(rect)
    plt.plot([x0, x1], [y0, y1], 'b-')
    plt.plot(points[:, 0], points[:, 1], 'bo')
    plt.grid(True)
    plt.xticks(np.arange(np.floor(x0), np.ceil(x1)))
    plt.yticks(np.arange(np.floor(y0), np.ceil(y1)))
    plt.show()

它打印:

[[ 1.5         0.5       ]
 [ 1.90277778  1.        ]
 [ 2.          1.12068966]
 [ 2.70833333  2.        ]
 [ 3.          2.36206897]
 [ 3.51388889  3.        ]
 [ 4.          3.60344828]
 [ 4.31944444  4.        ]
 [ 4.4         4.1       ]]

并生成图:

这篇关于直线中每个相交点的距离,按2D坐标递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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