Post by krotus on Mar 15, 2020 21:54:21 GMT
My language/environment: VB.NET using Microsoft Visual Studio Enterprise 2017 version 15.9.6 with Microsoft .NET Framework version 4.8.03572
Hello!
Up to this point, everything had been working. But, as I try implementing my code for rotating around an axis, I'm not getting the same results for 90 degree rotations as shown in the book. 45 degrees work fine, just not 90 degrees.
I have a class called MyMath that contains the math functions of my project. The class has a constant declared for converting degrees to radians:
Const Degrees2Radians As Double = Math.PI / 180.0
Here is my Matrix * Tuple function:
Public Shared Function MultiplyMatrixByTuple(MyMatrix(,) As Double, MyTuple As Tuple(Of Double, Double, Double, Double)) As Tuple(Of Double, Double, Double, Double)
Dim d(3) As Double
Dim r As Integer
For r = 0 To 3
d(r) = (MyMatrix(r, 0) * MyTuple.Item1) + (MyMatrix(r, 1) * MyTuple.Item2) + (MyMatrix(r, 2) * MyTuple.Item3) + (MyMatrix(r, 3) * MyTuple.Item4)
Next
Dim result As Tuple(Of Double, Double, Double, Double) = New Tuple(Of Double, Double, Double, Double)(d(0), d(1), d(2), d(3))
Return result
End Function
These are my axis rotation functions:
Public Shared Function XRotation(MyDegrees As Double) As Array
Dim result(3, 3) As Double
Dim MyRadians As Double = MyDegrees * Degrees2Radians
Dim r, c As Integer
For r = 0 To 3
For c = 0 To 3
If r = 0 And c = 0 Then
result(r, c) = 1
ElseIf r = 1 And c = 1 Then
result(r, c) = Math.Cos(MyRadians)
ElseIf r = 1 And c = 2 Then
result(r, c) = -Math.Sin(MyRadians)
ElseIf r = 2 And c = 1 Then
result(r, c) = Math.Sin(MyRadians)
ElseIf r = 2 And c = 2 Then
result(r, c) = Math.Cos(MyRadians)
ElseIf r = 3 And c = 3 Then
result(r, c) = 1
Else
result(r, c) = 0
End If
Next
Next
Return result
End Function
Public Shared Function YRotation(MyDegrees As Double) As Array
Dim result(3, 3) As Double
Dim MyRadians = MyDegrees * Degrees2Radians
Dim r, c As Integer
For r = 0 To 3
For c = 0 To 3
If r = 0 And c = 0 Then
result(r, c) = Math.Cos(MyRadians)
ElseIf r = 0 And c = 2 Then
result(r, c) = Math.Sin(MyRadians)
ElseIf r = 1 And c = 1 Then
result(r, c) = 1
ElseIf r = 2 And c = 0 Then
result(r, c) = -Math.Sin(MyRadians)
ElseIf r = 2 And c = 2 Then
result(r, c) = Math.Cos(MyRadians)
ElseIf r = 3 And c = 3 Then
result(r, c) = 1
Else
result(r, c) = 0
End If
Next
Next
Return result
End Function
Public Shared Function ZRotation(MyDegrees As Double) As Array
Dim result(3, 3) As Double
Dim MyRadians = MyDegrees * Degrees2Radians
Dim r, c As Integer
For r = 0 To 3
For c = 0 To 3
If r = 0 And c = 0 Then
result(r, c) = Math.Cos(MyRadians)
ElseIf r = 0 And c = 1 Then
result(r, c) = -Math.Sin(MyRadians)
ElseIf r = 1 And c = 0 Then
result(r, c) = Math.Sin(MyRadians)
ElseIf r = 1 And c = 1 Then
result(r, c) = Math.Cos(MyRadians)
ElseIf r = 2 And c = 2 Then
result(r, c) = 1
ElseIf r = 3 And c = 3 Then
result(r, c) = 1
Else
result(r, c) = 0
End If
Next
Next
Return result
End Function
Here is my code for declaring the point, getting the rotation matrix, and then multiplying them to get the new point coordinates from each axis (I'm including the 1 in the W position of the tuple):
'Rotation around the X Axis
Dim MyPoint = New Tuple(Of Double, Double, Double, Double)(0, 1, 0, 1)
Dim MyRotatedMatrix(,) As Double = MyMath.XRotation(45)
MyPoint = MyMath.MultiplyMatrixByTuple(MyRotatedMatrix, MyPoint)
MyPoint = New Tuple(Of Double, Double, Double, Double)(0, 1, 0, 1)
MyRotatedMatrix = MyMath.XRotation(90)
MyPoint = MyMath.MultiplyMatrixByTuple(MyRotatedMatrix, MyPoint)
'Rotation around the Y Axis
Dim MyPoint = New Tuple(Of Double, Double, Double, Double)(0, 0, 1, 1)
Dim MyRotatedMatrix(,) As Double = MyMath.YRotation(45)
MyPoint = MyMath.MultiplyMatrixByTuple(MyRotatedMatrix, MyPoint)
MyPoint = New Tuple(Of Double, Double, Double, Double)(0, 0, 1, 1)
MyRotatedMatrix = MyMath.YRotation(90)
MyPoint = MyMath.MultiplyMatrixByTuple(MyRotatedMatrix, MyPoint)
'Rotation around the Z Axis
Dim MyPoint = New Tuple(Of Double, Double, Double, Double)(0, 1, 0, 1)
Dim MyRotatedMatrix(,) As Double = MyMath.ZRotation(45)
MyPoint = MyMath.MultiplyMatrixByTuple(MyRotatedMatrix, MyPoint)
MyPoint = New Tuple(Of Double, Double, Double, Double)(0, 1, 0, 1)
MyRotatedMatrix = MyMath.ZRotation(90)
MyPoint = MyMath.MultiplyMatrixByTuple(MyRotatedMatrix, MyPoint)
For rotating around each axis, here are my results:
Rotating around X Axis by 45 degrees: MyPoint(0, 0.707106781186548, 0.707106781186547, 1) Correct, according to the book
Rotating around X Axis by 90 degrees: MyPoint(0, 6.12303176911189E-17, 1, 1) Incorrect - The book shows MyPoint(0, 0, 1, 1)
Rotating around Y Axis by 45 degrees: MyPoint(0.707106781186547, 0, 0.707106781186548, 1) Correct, according to the book
Rotating around Y Axis by 90 degrees: MyPoint(1, 0, 6.12303176911189E-17, 1) Incorrect - The book shows MyPoint(1, 0, 1, 1)
Rotating around Z Axis by 45 degrees: MyPoint(-0.707106781186547, 0.707106781186548, 0, 1) Correct, according to the book
Rotating around Z Axis by 90 degrees: MyPoint(-1, 6.12303176911189E-17, 0, 1) Incorrect - The books shows MyPoint(-1, 0, 0, 1)
The error is consistent, as far as the value that should be zero, I just can't figure out where I'm going wrong.