cad-waulader 发表于 2005-8-15 13:31:11

寻找三维向量的方位角

大家好,我有一个小问题,除了一个小部分,基本上解决了
我在3D中定义了一条线。我想确定这条线相对于线的起点在由法线(0,0,1)和点(0,零,0)定义的平面中的方位角。该平面是xy平面。方位角将从xy平面中的矢量(0,1,0)测量
以下是获取角度的方法

N is the normal of the plane (0,0,1)
P is the line that I am working with. It has coordinates Psx,Psy,Psz and Pex,Pey,Pez. where Ps refers to the starting point and Pe refers to the end point.
V is a vector in the direction of P. Vx = Pex-Psx;Vy = Pey-Psy;Vz = Pez-Psz
Now, project V onto the xy plane:
U = V - (V.N)*N; where V.N is the dot product
Now we have a vector in the xy plane, we can find the angle of the this vector and the vector M (0,1,0) which defines North.
To determine the angle:
theta = arccos(U.M/(|U|*|M|))

我遇到的问题是,θ将是两个向量之间的锐角。换句话说,如果我有一条270度的直线,计算结果将返回90度。
**** Hidden Message *****

cad-waulader 发表于 2005-8-15 14:20:15

我想我需要取相关向量的行列式,以找出角度的测量方式

N = 0,0,1; this is the direction of the normal describing the xy plane
M = 0,1,0; This vector defines north
U = Ux,Uy,Uz; this is the vector that I want to find the angle of relative to M
For an example, lets say U = -1,1,0; this would put the vector in the 2nd quadrant and it should have an angle of 315 degrees. If I use the procedure in my last post I get an angle of 45 degrees.
Now if I find the determinant of:
|-1 1 0|
| 0 1 0|= -1
| 0 0 1|
I believe, according to the sign that the angle has been calculated the wrong way. So If I take 360 and subtract 45 I arrive at 315 degrees. Which is what I want.
Say I used U=2,2,0 which is 45 degrees, I would get
| 2 2 0|
| 0 1 0|= 2
| 0 0 1|
The positive result would indicate that I do not need a correction.
If the result is 0, it means that the vectors are parallel.

我找遍了所有地方,似乎找不到答案(或者至少我能理解)。我相信对3个向量进行确定并检查结果的符号是正确的方法。我已经对一些案例的结果进行了测试,在每种情况下似乎都有效。我没有改变我工作的平面或北向量的方向
有人能帮我核实一下吗?或者我做的事情有误吗?

t-bear 发表于 2005-8-15 14:57:09

这样如何:
将北方位角定义为从正北顺时针测量的度数,计算直线的Δx和Δy。忘记三角洲z。将xy网格的原点置于线的起点,然后查看线所在的象限。根据哪个象限,即应用不同的规则来计算方位角:
象限一:
方位角=弧坦(δx/δy)
第二象限:
感谢您的回复 cad-waulader.
感谢您的方法。在计算地下钻孔角度时,我使用了类似的方法,并且效果很好(但是我更喜欢您的方法)。
我可能会使用您的方法,但我仍然想知道我是否在上一篇文章中就行列式外出吃午饭?

t-bear 发表于 2005-8-15 15:08:55

必须查阅那方面的书籍...矢量计算保存在我大脑生锈/尘土飞扬的一侧。

SwiftGuest 发表于 2005-8-15 15:17:34


告诉我,我应该在学校更加注意。我从未想过我会如此广泛地与他们一起工作。
从我目前正在阅读的一些文本中,我在之前的帖子中所做的决定因素是平行六面体的签名卷。签名卷意味着它指示扫描的方向。我相信这可以告诉我角度的计算方式。我已经做了不少测试,它们似乎证实了我的怀疑。
无论如何,我可能会为autocad模拟一些vba来确认我的计算。如果有人感兴趣,我明天某个时候会发布我的结果以及宏。

cad-waulader 发表于 2005-8-15 15:28:15

如果有人感兴趣,这里是我想出的代码。它是用 VBA for AutoCAD 编写的。它似乎像我预期的那样工作。只需将其复制并粘贴到空模块中即可。

Option Explicit
Private Const x As Integer = 0
Private Const y As Integer = 1
Private Const z As Integer = 2
Private Const PI As Double = 3.14159
Public Sub findAzimuth()
Dim pickedEntity As AcadEntity
Dim pickedPoint As Variant
Dim myLine As AcadLine
ThisDrawing.Utility.GetEntity pickedEntity, pickedPoint, "Please select a line: " & vbCr
If pickedEntity Is Nothing Then Exit Sub
If TypeOf pickedEntity Is AcadLine Then
    Set myLine = pickedEntity
Else
    ThisDrawing.Utility.Prompt vbCrLf & "Be sure to select a line next time!" & vbCr
    Exit Sub
End If
Dim N(2) As Double 'the normal of the plane
Dim M(2) As Double 'the direction of the azimuth line
Dim v(2) As Double 'the vector representing the line
Dim U(2) As Double 'the projection of V onto the plane
N(x) = 0
N(y) = 0
N(z) = 1
M(x) = 0
M(y) = 1
M(z) = 0
v(x) = myLine.EndPoint(x) - myLine.StartPoint(x)
v(y) = myLine.EndPoint(y) - myLine.StartPoint(y)
v(z) = myLine.EndPoint(z) - myLine.StartPoint(z)
Dim dpResult As Double 'dot product result
dpResult = dotProduct(v, N)
U(x) = v(x) - dpResult * N(x)
U(y) = v(y) - dpResult * N(y)
U(z) = v(z) - dpResult * N(z)
Dim theta As Double
Dim dpVectorNormal As Double
Dim vectMagnitude As Double
vectMagnitude = (vectorMagnitude(U) * vectorMagnitude(M)) 'pre-calculate this value - if it is zero, are angle calculation is greatly simplified
If vectMagnitude > 0 Then
    dpVectorNormal = dotProduct(U, M) 'calculate the dot product of the projected vector and the azimuth vector
   
    theta = aCos(dpVectorNormal / vectMagnitude)
   
    'now we need to figure out which way the angle was measured from
    Dim D As Double
    D = determinant(U, M, N)
   
    If D < 0 Then
      theta = 2 * PI - theta
    End If
Else
    'the hole looks vertical in that plane
    theta = 0
End If
'convert the angle to degrees
theta = theta * 180 / PI
ThisDrawing.Utility.Prompt vbCr & "The Azimuth is: " & theta
Set myLine = Nothing
Set pickedEntity = Nothing
End Sub
Private Function determinant(v1 As Variant, v2 As Variant, v3 As Variant) As Double
'this will calculate the 3x3 determinant of the passed vectors
'v1(x),v1(y),v1(z)
'v2(x),v2(y),v2(z)
'v3(x),v3(y),v3(z)
Dim a As Double
Dim b As Double
Dim c As Double
a = v1(x) * (v2(y) * v3(z) - v2(z) * v3(y))
b = v1(y) * (v2(x) * v3(z) - v2(z) * v3(x))
c = v1(z) * (v2(x) * v3(y) - v2(y) * v3(x))
determinant = a - b + c
End Function
Private Function aCos(v As Double) As Double
aCos = Atn(-v / Sqr(-v * v + 1)) + 2 * Atn(1)
End Function
Private Function vectorMagnitude(v As Variant) As Double
    vectorMagnitude = v(x) * v(x) + v(y) * v(y) + v(z) * v(z)
    vectorMagnitude = Sqr(vectorMagnitude)
End Function
Private Function dotProduct(v1 As Variant, v2 As Variant) As Double
dotProduct = v1(0) * v2(0) + v1(1) * v2(1) + v1(2) * v2(2)
End Function

cad-waulader 发表于 2005-8-16 08:51:33

看起来棒极了!我一点头绪都没有.......但是看起来很棒!:dood:

cad-waulader 发表于 2005-8-16 09:36:49


如果您在autocad中运行它,它会要求您选择一条线。然后它会告诉您该线的方位角。它假设您想要该线的方位角,就好像它被投影到xy平面(平面图)一样,并且它假设正y轴是北。
可以进行许多优化以使其更快,但我没有进行优化,因为它可能会模糊一些数学工作原理。

cad-waulader 发表于 2005-8-16 09:54:58

谢谢特洛伊.........我是一个机械类型的人,从来没有理由需要一个方位角......以为它像阿斯玛一样是总和......
好吧......不好笑......

t-bear 发表于 2005-8-16 11:17:55

特洛伊,对我来说看起来还不错
斯威夫特
页: [1] 2
查看完整版本: 寻找三维向量的方位角