【问题标题】:Plot a point in 3D space perpendicular to a line vector在垂直于线向量的 3D 空间中绘制一个点
【发布时间】:2020-07-17 17:53:53
【问题描述】:

我正在尝试计算 3D 空间中与线向量正交/垂直的点。

所以我有 P1 和 P2 形成一条线。然后我试图找到一个半径以 P1 为中心的点,它与直线正交。

我想用三角函数来做这件事,没有任何编程语言特定的函数。

目前我正在测试这个函数的精确度,方法是在线向量周围画一个圆圈。

我还在 3D 空间中旋转线向量,以查看绘制的圆会发生什么变化,这就是我的结果不同的地方。

一些不良影响包括: 旋转并通过线向量的圆。 随着线向量改变方向,圆的半径在增大之前似乎减小到零。

我使用this question 来帮助我开始,并在此后对代码进行了一些调整。

对此的任何帮助将不胜感激 - 我现在已经花了 3 天时间画圆圈。到目前为止,这是我的代码

//Define points which form the line vector
dx = p2x - p1x;
dy = p2y - p1y;
dz = p2z - p1z;

// Normalize line vector
d = sqrt(dx*dx + dy*dy + dz*dz);

// Line vector
v3x = dx/d;
v3y = dy/d;
v3z = dz/d;

// Angle and distance to plot point around line vector
angle = 123 * pi/180 //convert to radians
radius = 4;

// Begin calculating point
s = sqrt(v3x*v3x + v3y*v3y + v3z*v3z);

// Calculate v1.
// I have been playing with these variables (v1x, v1y, v1z) to try out different configurations.
v1x = s * v3x;
v1y = s * v3y;
v1z = s * -v3z;

// Calculate v2 as cross product of v3 and v1.
v2x = v3y*v1z - v3z*v1y;
v2y = v3z*v1x - v3x*v1z;
v2z = v3x*v1y - v3y*v1x;

// Point in space around the line vector
px = p1x + (radius * (v1x * cos(angle)) + (v2x * sin(angle)));
py = p1y + (radius * (v1y * cos(angle)) + (v2y * sin(angle)));
pz = p1z + (radius * (v1z * cos(angle)) + (v2z * sin(angle)));

编辑

在锁定期间与这个问题搏斗了几天后,我终于设法让它工作了。我要感谢 MBo 和 Futurologist 的宝贵意见。

虽然我无法让他们的示例发挥作用(更可能是因为我有过错),但他们的回答为我指明了正确的方向和那个灵光乍现的时刻!

关键在于交换正确的向量。

非常感谢你们两位,你们真的帮助了我。这是我的最终(工作)代码:

//Set some vars
angle = 123 * pi/180;
radius = 4;

//P1 & P2 represent vectors that form the line.
dx = p2x - p1x;
dy = p2y - p1y;
dz = p2z - p1z;

d = sqrt(dx*dx + dy*dy + dz*dz)

//Normalized vector
v3x = dx/d;
v3y = dy/d;
v3z = dz/d;

//Store vector elements in an array
p = [v3x, v3y, v3z];

//Store vector elements in second array, this time with absolute value
p_abs = [abs(v3x), abs(v3y), abs(v3z)];

//Find elements with MAX and MIN magnitudes
maxval = max(p_abs[0], p_abs[1], p_abs[2]);
minval = min(p_abs[0], p_abs[1], p_abs[2]);

//Initialise 3 variables to store which array indexes contain the (max, medium, min) vector magnitudes.
maxindex = 0;
medindex = 0;
minindex = 0;

//Loop through p_abs array to find which magnitudes are equal to maxval & minval. Store their indexes for use later.
for(i = 0; i < 3; i++) {
    if (p_abs[i] == maxval) maxindex = i;
    else if (p_abs[i] == minval) minindex = i;
}

//Find the remaining index which has the medium magnitude
for(i = 0; i < 3; i++) {
    if (i!=maxindex && i!=minindex) {
        medindex = i;
        break;
    }
}

//Store the maximum magnitude for now.
storemax = (p[maxindex]);

//Swap the 2 indexes that contain the maximum & medium magnitudes, negating maximum. Set minimum magnitude to zero.
p[maxindex] = (p[medindex]);
p[medindex] = -storemax;
p[minindex] = 0;

//Calculate v1. Perpendicular to v3.
s = sqrt(v3x*v3x + v3z*v3z + v3y*v3y);
v1x = s * p[0];
v1y = s * p[1];
v1z = s * p[2];

//Calculate v2 as cross product of v3 and v1.
v2x = v3y*v1z - v3z*v1y;
v2y = v3z*v1x - v3x*v1z;
v2z = v3x*v1y - v3y*v1x;

//For each circle point.
circlepointx = p2x + radius * (v1x * cos(angle) + v2x * sin(angle))
circlepointy = p2y + radius * (v1y * cos(angle) + v2y * sin(angle))
circlepointz = p2z + radius * (v1z * cos(angle) + v2z * sin(angle))

【问题讨论】:

  • 我已经用language-agnostic 替换了javac++ 标签,因为你明确地想知道一些与语言无关的东西。如果这不是您的意图,请告诉我。

标签: math geometry language-agnostic trigonometry


【解决方案1】:

你的问题太模糊了,但我可能猜到你真正想要的是什么。

您有通过两个点 p1p2 的线。你想建立一个以p1 为中心并垂直于直线的半径为r 的圆。

首先找到这条线的方向向量 - 你已经知道如何 - 归一化向量v3

现在您需要垂直于v3 的任意向量:找到v3 的最大量级和第二级分量。例如,abs(v3y) 是最大的,abs(v3x) 是第二个量级。交换它们,否定最大的,并使第三个分量为零:

p = (-v3y, v3x, 0)

这个向量与v3 正交(它们的点积为零)

现在规范化它

pp = p / length(p)

现在得到二法向量作为v3pp的叉积(我有单位长度,不需要归一化),它垂直于v3pp

b = v3 x pp

现在建立需要的圈子

circlepoint(theta) = p1 + radius * pp * Cos(theta) + radius * b * Sin(theta)

还要注意弧度的角度是

angle = degrees * pi / 180

【讨论】:

  • 我更新了我的问题,使其更清晰(还有弧度问题)。只是为了澄清一下,在您的示例中,向量 p 元素 x,y,z 分别变为 -v3y, v3x, 0?
  • 是的,完全正确。看来我很理解你的需求。
【解决方案2】:
#Input:
# Pair of points which determine line L: 
P1 = [x_P1, y_P1, z_P1]
P2 = [x_P1, y_P1, z_P1]

# Radius:
Radius = R

# unit vector aligned with the line passing through the points P1 and P2:
V3 = P1 - P2
V3 = V3 / norm(V3)

# from the three basis vectors, e1 = [1,0,0], e2 = [0,1,0], e3 = [0,0,1] 
# pick the one that is the most transverse to vector V3
# this means, look at the entries of V3 = [x_V3, y_V3, z_V3] and check which
# one has the smallest absolute value and record its index. Take the coordinate 
# vector that has 1 at that selected index. In other words, 
# if min( abs(x_V3), abs(y_V)) = abs(y_V3), 
# then argmin( abs(x_V3), abs(y_V3), abs(z_V3)) = 2 and so take e = [0,1,0]:   
e = [0,0,0]
i = argmin( abs(V3[1]), abs(V3[2]), abs(V3[3]) )
e[i] = 1

# a unit vector perpendicular to both e and V3:  
V1 = cross(e, V3)
V1 = V1 / norm(V1)

# third unit vector perpendicular to both V3 and V1:
V2 = cross(V3, V1)

# an arbitrary point on the circle (i.e. equation of the circle with parameter s):
P = P1 + Radius*( np.cos(s)*V1 + np.sin(s)*V2 ) 


# E.g. say you want to find point P on the circle, 60 degrees relative to vector V1:
s = pi/3
P = P1 + Radius*( cos(s)*V1 + sin(s)*V2 ) 

Python 中的测试示例:

import numpy as np

#Input:
# Pair of points which determine line L: 
P1 = np.array([1, 1, 1])
P2 = np.array([3, 2, 3])
Radius = 3

V3 = P1 - P2
V3 = V3 / np.linalg.norm(V3)
e = np.array([0,0,0])
e[np.argmin(np.abs(V3))] = 1

V1 = np.cross(e, V3)
V1 = V1 / np.linalg.norm(V3)

V2 = np.cross(V3, V1)

# E.g., say you want to rotate point P along the circle, 60 degrees along rel to V1:
s = np.pi/3
P = P1 + Radius*( np.cos(s)*V1 + np.sin(s)*V2 ) 

【讨论】:

  • 如果这假设我已经在某处与这条线有一个点,恐怕我没有。我已经更新了我的答案,让事情变得更清楚。
  • @pJay 现在怎么样。基本上,找到半径为 R 的圆的方程,以点 P1 为圆心,位于通过点 P1 并垂直于线 P1 P2 的平面内。
猜你喜欢
  • 2017-08-23
  • 2012-05-28
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多