简体   繁体   English

将 (x, y) 坐标投影到直线上作为距离

[英]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。

通常将点 C 和 E 投影到从 A 到 B 的直线上

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 projectionc的投影点返回到线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.

相关问题 从(x,y)坐标中找到行进的距离 - Find the distance traveled from (x,y) coordinates PyProj:使用x,y坐标将纬度/经度映射到矩形上 - PyProj: Mapping latitude/longitude onto rectangle using x,y coordinates 如何将距离公式应用于python中的[x,y]坐标列表 - How to apply the distance formula to a list of [x,y] coordinates in python 在OpenCV中获取具有X,Y和到对象的距离的3D坐标 - Get 3D coordinates in OpenCV having X,Y and distance to object 蟒蛇。 如何从ax,y列表和偏移距离获取偏移样条的x,y坐标 - Python. How to get the x,y coordinates of a offset spline from a x,y list of points and offset distance 如何将x,y坐标投影到netcdf文件中的纬度/经度 - How to project x,y coordinates to lat/lon in netcdf file 如何使用两点的 x 和 y 坐标绘制一条线? - How can draw a line using the x and y coordinates of two points? 对于线长的(x,y)像素坐标,调整角度变成圆形 - For (x, y) pixel coordinates in length of line, adjust angle to become circle 在 xy 平面上绘制与 (x,y) 坐标法线的垂直线? - Ploting perpendicular line to normal of (x,y) coordinates on xy plane? 如何使用skimage获得hough线峰值的x,y坐标 - How to get extream x,y coordinates for hough line peaks with skimage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM