[英]Project (x, y) coordinates onto a line as a distance
I would like to project two (x, y) points C and E normally onto a line defined by two (x, y) points A and B. I want the projection result as a distance from A towards B (eg AD and AF in the below image), not as (x, y) coordinates on the line.我想将两个(x,y)点 C 和 E 通常投影到由两个(x,y)点 A 和 B 定义的线上。我希望投影结果为从 A 到 B 的距离(例如 AD 和 AF 在下图),而不是作为(x,y)坐标就行了。 Notice the negative distance for AF when it falls outside the A to B line.
请注意 AF 位于 A 到 B 线之外时的负距离。 Please help me out with the below
coordinates_to_distances
function.请帮助我解决以下
coordinates_to_distances
到距离 function。
import numpy as np
A = np.array([2, 1]) # (x, y) coordinates of A
B = np.array([6, 2]) # (x, y) coordinates of B
x = np.array([3, 1]) # x-coordinates of C and E
y = np.array([3, 2]) # y-coordinates of C and E
def coordinates_to_distances(A, B, x, y):
# Help
return distances
distances = coordinates_to_distances(A, B, x, y) # expected result: [1.46, -0.73]
I've modified your task a little to work with points as dictionaries ({x, y}).我对您的任务进行了一些修改,以便将点用作字典 ({x, y})。
projection returns a projection point of c to line ab projection将c的投影点返回到线ab
distance_to_point returns distance of point c to point a , where direction from a is given by point b distance_to_point返回点c到点a 的距离,其中从a的方向由点b给出
NOTE : The sample will not work when the line ab is vertical!注意:当ab线垂直时,示例将不起作用!
import math
def projection(a,b,c):
n = (b['y'] - a['y']) / (b['x'] - a['x'])
m = a['y'] - n * a['x']
p = c['y'] + c['x'] / n
x = (p - m) / (n + 1 / n)
y = n * x + m
return {'x': x, 'y': y}
def distance_to_point(a, b, c):
dir = 1 if bool(b['x'] > a['x']) == bool(c['x'] > a['x']) else -1
return math.hypot(c['y'] - a['y'], c['x'] - a['x']) * dir
a = {'x': 2, 'y': 1}
b = {'x': 6, 'y': 2}
c = {'x': 3, 'y': 3}
d = projection(a, b, c)
dta = distance_to_point(a, b, d)
print(dta)
NumPy solution: NumPy 解决方案:
import numpy as np
A = np.array([2, 1]) # (x, y) coordinates of A
B = np.array([6, 2]) # (x, y) coordinates of B
x = np.array([3, 1]) # x-coordinates of C and E
y = np.array([3, 2]) # y-coordinates of C and E
def coordinates_to_distances(A, B, x, y):
dy = B[1] - A[1]
dx = B[0] - A[0]
x_new, y_new = x * 0 + 1, y * 0 + 1
if dx == 0:
x_new, y_new = x * 0 + A[0], y
if dy == 0:
x_new, y_new = x, y * 0 + A[1]
if dx != 0 and dy != 0:
n = dy / dx
m = A[1] - n * A[0]
p = y + x / n
x_new = (p - m) / (n + 1 / n)
y_new = n * x_new + m
dir = x_new * 0 - 1
dir[(B[0] > A[0]) == (x_new > A[0])] = 1
return dir * np.sqrt((A[0] - x_new)**2 + (A[1] - y_new)**2)
distances = coordinates_to_distances(A, B, x, y)
print(distances) # [1.46, -0.73]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.