-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformation.cs
138 lines (112 loc) · 5.8 KB
/
Transformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Graphics_2_Assignments
{
class Transformation
{
public static void RotatX(List<_3D_Point> L_Pts, float theta)//Rotate a list of 3D point using angle theta on the X axis
{
float th = (float)(Math.PI * theta / 180);//Similar to Graphics1 the correct theta is calculated using the given theta
for (int i = 0; i < L_Pts.Count; i++)
{
_3D_Point p = L_Pts[i];
float x_ = p.X;//X will remain the same
float y_ = (float)(p.Y * Math.Cos(th) - p.Z * Math.Sin(th));//Y changes based on its mathematical equation
float z_ = (float)(p.Y * Math.Sin(th) + p.Z * Math.Cos(th));//Z changes based on its mathematical equation
//The new values are placed in the 3D list
p.X = x_;
p.Y = y_;
p.Z = z_;
}
}
public static void RotatY(List<_3D_Point> L_Pts, float theta)
{
float th = (float)(Math.PI * theta / 180);
for (int i = 0; i < L_Pts.Count; i++)
{
_3D_Point p = L_Pts[i];
float x_ = (float)(p.Z * Math.Sin(th) + p.X * Math.Cos(th));
float y_ = p.Y;
float z_ = (float)(p.Z * Math.Cos(th) - p.X * Math.Sin(th));
p.X = x_;
p.Y = y_;
p.Z = z_;
}
}
public static void RotatZ(List<_3D_Point> L_Pts, float theta)
{
float th = (float)(Math.PI * theta / 180);
for (int i = 0; i < L_Pts.Count; i++)
{
_3D_Point p = L_Pts[i];
float x_ = (float)(p.X * Math.Cos(th) - p.Y * Math.Sin(th));
float y_ = (float)(p.X * Math.Sin(th) + p.Y * Math.Cos(th));
float z_ = p.Z;
p.X = x_;
p.Y = y_;
p.Z = z_;
}
}
public static void TranslateX(List<_3D_Point> L_Pts, float tx)//The X values of the given list are translted (moved), usually to start its plane from x,y,z of 0,0,0
{
for (int i = 0; i < L_Pts.Count; i++)
{
_3D_Point p = L_Pts[i];
p.X += tx;
}
}
public static void TranslateY(List<_3D_Point> L_Pts, float ty)
{
for (int i = 0; i < L_Pts.Count; i++)
{
_3D_Point p = L_Pts[i];
p.Y += ty;
}
}
public static void TranslateZ(List<_3D_Point> L_Pts, float tz)
{
for (int i = 0; i < L_Pts.Count; i++)
{
_3D_Point p = L_Pts[i];
p.Z += tz;
}
}
public static void Translate(List<_3D_Point> L_Pts, float xr, float yr, float zr)//Used to translate all
{
for (int i = 0; i < L_Pts.Count; i++)
{
L_Pts[i].X += xr;
L_Pts[i].Y += yr;
L_Pts[i].X += zr;
}
}
public static void RotateArbitrary(List<_3D_Point> L_Pts,
_3D_Point v1,
_3D_Point v2,
float ang)//The list of 3D points will rotate around 2 specific 3D point v1 and v2 using a given angle
{
Transformation.TranslateX(L_Pts, v1.X * -1);//The X values of the 3D list is translated to have the first 3D point start at 0,0,0
Transformation.TranslateY(L_Pts, v1.Y * -1);//The Y values of the 3D list is translated to have the first 3D point start at 0,0,0
Transformation.TranslateZ(L_Pts, v1.Z * -1);//The Z values of the 3D list is translated to have the first 3D point start at 0,0,0
float dx = v2.X - v1.X;//The difference between the X values of the second 3D point and the translated X of the first 3D point
float dy = v2.Y - v1.Y;//The difference between the Y values of the second 3D point and the translated Y of the first 3D point
float dz = v2.Z - v1.Z;//The difference between the Z values of the second 3D point and the translated Z of the first 3D point
float theta = (float)Math.Atan2(dy, dx);//Theta is calculated based on the proof in the project files
float phi = (float)Math.Atan2(Math.Sqrt(dx * dx + dy * dy), dz);//phi is calculated based on the proof in the project files
theta = (float)(theta * 180 / Math.PI);//Theta is then converted to radian value as per graphics 1
phi = (float)(phi * 180 / Math.PI);//Phi is also converted to radian value
//To rotate the given 3d list around V1 and V2, the following code lines are used based on the proof in the project file
Transformation.RotatZ(L_Pts, theta * -1);//Start inverse rotation around Z axis using theta
Transformation.RotatY(L_Pts, phi * -1);//Start inverse rotation around Y axis using phi
Transformation.RotatZ(L_Pts, ang);//Start rotation around Z axis using ang
Transformation.RotatY(L_Pts, phi * 1);//Start rotation around Y axis using phi
Transformation.RotatZ(L_Pts, theta * 1);//Start rotation around Z axis using theta
Transformation.TranslateZ(L_Pts, v1.Z * 1);//Returns the Z values of v1 to original place
Transformation.TranslateY(L_Pts, v1.Y * 1);//Returns the Y values of v1 to original place
Transformation.TranslateX(L_Pts, v1.X * 1);//Returns the X values of v1 to original place
}
}
}