python输入两个点的坐标,在python中获取两个(X,Y)坐标之间所有点的最快方法...

本文介绍了如何在Python中高效地获取两个(X,Y)坐标之间所有整数点的方法,包括使用Bresenham算法和矢量化解决方案。作者提供了一个brute force vectorization的实现,用于在大量数据上快速计算。

So I have a shapely LineString:

print np.round(shapely_intersecting_lines.coords).astype(np.int)

>>> array([[ 1520, -1140],

[ 1412, -973]])

This can be interpreted as a numpy array as well as seen above.

I want to get all the points in between, that is I want to get the points of the line in between as integer values. The output should be something like this:

array([[ 1520, -1140],

[ 1519, -1139],

[ 1519, -1138],

...,

[ 1413, -975],

[ 1412, -974],

[ 1412, -973]], dtype=int32)

I posted this earlier in gis.stackexchange hoping there was a solution in shapely that was efficient. The solution was good at first, however, the solution is now too slow as I run this over 50000 times in my code. On my computer each loop takes about 0.03s resulting in over a day of running. It is too slow for what I need here

<think>我们之前讨论了快速排序,但现在用户的问题是关于计算两个坐标之间的距离。 用户明确要求:使用Python计算两个坐标之间距离的方法。 我将提供两种常用方法:欧几里得距离(二维和三维)和曼哈顿距离,并给出代码示例。 同时,按照要求生成相关问题。 </think>### Python计算两个坐标距离的方法 在几何计算中,两点距离通常指**欧几里得距离**(直线距离),Python可通过以下方法实现: #### 1. 数学公式实现(推荐) ```python import math def distance_2d(point1, point2): """计算二维坐标距离""" x1, y1 = point1 x2, y2 = point2 return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) # 示例用法 point_A = (3, 4) point_B = (0, 0) print(f"两点距离:{distance_2d(point_A, point_B):.2f} 单位") # 输出:5.00 ``` #### 2. 使用NumPy库(适合批量计算) ```python import numpy as np def distance_numpy(points1, points2): """支持批量对距离计算""" return np.linalg.norm(np.array(points1) - np.array(points2), axis=1) # 示例用法 points_set1 = [(1,2), (3,4)] points_set2 = [(4,6), (0,0)] print(distance_numpy(points_set1, points_set2)) # 输出:[5. 5.] ``` #### 3. 三维空距离计算 $$d = \sqrt{(x_2-x_1)^2 + (y_2-y_1)^2 + (z_2-z_1)^2}$$ ```python def distance_3d(point1, point2): x1, y1, z1 = point1 x2, y2, z2 = point2 return math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2) # 示例 print(distance_3d((1,2,3), (4,5,6))) # 输出≈5.196 ``` #### 注意事项: 1. 坐标格式支持元组/列表:`(x,y)` 或 `[x,y]` 2. 距离单位取决于坐标系的单位(米、像素等) 3. 对于地球表面距离,需使用Haversine公式计算大圆距离[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值