XYZ坐标的3D旋转-某些角度的结果是否错误?

问题描述 投票:0回答:1

[请忍受,我是编程新手,我正在尝试学习如何围绕0,0,0旋转3D点(XYZ),稍后我将尝试改进代码以允许围绕任意点旋转点(XYZ)。

我从VB开始,在这里和在Web上进行了大量搜索之后,我找不到我的问题的解释。我快要学习数学和编程,已经快40岁了,所以请多多包涵,因为我将需要一些时间来消化所有这些问题的数学方面。

[基本上,我正在尝试编写一种算法来旋转3D点,但是,似乎我的代码在某些角度上是可行的,而在另一些角度上,我只是得到了在某些方面可能正确的怪异的时髦数字,但我在代码中找不到缺陷。我已经研究了好几天,并尝试了多种方法,但我只是无法发现错误。

这是我的小应用程序的用户界面。原始坐标输入到表单的顶部,在表单底部输入旋转的坐标。

[请注意,在下图中,将Y10.0的坐标围绕Z轴简单旋转90度会返回正确的X值(-10),但是Y显示的是时髦的数字(6.1230 ...)...但是,如果我将围绕Z的旋转角度更改为45,则结果似乎是正确的...

我不知道要弄出这个怪异的Y我在做什么错。由于这个错误,我完全不相信该算法的结果,但我目前处于盲点...

Conceptual UI

这是计算按钮的代码:

 Private Sub BtnCompute_Click(sender As Object, e As EventArgs) Handles BtnCompute.Click

    'Capture the values from the text boxes and parse then to doubles
    ValidateAllFieldsWithDoubleValues()

    'Rotate the coordinates
    RotateXYZCoordinates(dblOriginalCoordX, dblOriginalCoordY, dblOriginalCoordZ, dblCurrentRotationAroundX, dblCurrentRotationAroundY, dblCurrentRotationAroundZ)

    'Update the text boxes for the rotated coordinates for XYZ
    txtResultX.Text = dblResultX.ToString
    txtResultY.Text = dblResultY.ToString
    txtResultZ.Text = dblResultZ.ToString

End Sub

这是计算旋转度的函数代码:

Private Function RotateXYZCoordinates(ByVal XCoord As Double, ByVal YCoord As Double, ByVal ZCoord As Double, ByVal Pitch As Double, ByVal Roll As Double, ByVal Yaw As Double)

    'X Rotation
    Dim RadPitch As Double = 0
    Dim CosPitch As Double = 0
    Dim SinPitch As Double = 0
    Dim XRotatedAroundX As Double = 0
    Dim YRotatedAroundX As Double = 0
    Dim ZRotatedAroundX As Double = 0
    RadPitch = Pitch * Math.PI / 180
    CosPitch = Math.Cos(RadPitch)
    SinPitch = Math.Sin(RadPitch)

    XRotatedAroundX = XCoord
    YRotatedAroundX = YCoord * CosPitch - ZCoord * SinPitch
    ZRotatedAroundX = YCoord * SinPitch + ZCoord * CosPitch

    'Y Rotation
    Dim RadRoll As Double = 0
    Dim CosRoll As Double = 0
    Dim SinRoll As Double = 0
    Dim XRotatedAroundY As Double = 0
    Dim YRotatedAroundY As Double = 0
    Dim ZRotatedAroundY As Double = 0
    RadRoll = Roll * Math.PI / 180
    CosRoll = Math.Cos(RadRoll)
    SinRoll = Math.Sin(RadRoll)

    XRotatedAroundY = ZRotatedAroundX * CosRoll - XRotatedAroundX * SinRoll
    YRotatedAroundY = YRotatedAroundX
    ZRotatedAroundY = ZRotatedAroundX * SinRoll + XRotatedAroundX * CosRoll

    'Z Rotation
    Dim RadYaw As Double = 0
    Dim CosYaw As Double = 0
    Dim SinYaw As Double = 0
    Dim XRotatedAroundZ As Double = 0
    Dim YRotatedAroundZ As Double = 0
    Dim ZRotatedAroundZ As Double = 0
    RadYaw = Yaw * Math.PI / 180
    CosYaw = Math.Cos(RadYaw)
    SinYaw = Math.Sin(RadYaw)

    XRotatedAroundZ = XRotatedAroundY * CosYaw - YRotatedAroundY * SinYaw
    YRotatedAroundZ = XRotatedAroundY * SinYaw + YRotatedAroundY * CosYaw
    ZRotatedAroundZ = ZRotatedAroundY

    'Final result

    dblResultX = XRotatedAroundZ
    dblResultY = YRotatedAroundZ
    dblResultZ = ZRotatedAroundZ

    Return Nothing

End Function

我知道这不是一个很好的代码,但是我现在可以编写它。。。如果有人可以看一下并指出错误的源头,我将不胜感激。视频,并在发布之前在该网站上进行了广泛的搜索...但是,目前看来有些事情对我来说还是非常先进的...我并不懒惰,我愿意学习是否有人将我指向我的方向现在可以消化...

[如果有人可以分享有关如何使此旋转功能提示以支持围绕0,0,0以外的其他点旋转的提示,]

谢谢,

丹尼尔

[请忍受,我是编程新手,我正在尝试学习如何围绕0,0,0旋转3D点(XYZ),稍后我将尝试改进代码以允许围绕任意点旋转点(...

vb.net math 3d rotation
1个回答
0
投票

答案是正确的。由于双精度数学和90度旋转,因此精度受到限制。答案实际上是6.12303176911189E-16或.000000000000000612303176911189。将数字四舍五入为小数点的实际值。这也是为什么在浮点数学中1 + 1不等于2而是1.9999999999999999999999999999999999的原因。

© www.soinside.com 2019 - 2024. All rights reserved.