/
Point.cpp
executable file
·167 lines (141 loc) · 3.62 KB
/
Point.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Point.h"
Point::Point()
{
//point
x = 0;
y = 0;
z = 0;
tag = 1; //elemen terakhir sebagai pelengkap, untuk keperluan transformasi
}
Point::Point(float xnew, float ynew, float znew){
//point
x = xnew;
y = ynew;
z = znew;
tag = 1; //elemen terakhir sebagai pelengkap, untuk keperluan transformasi
}
Point::Point(float xnew, float ynew, float znew, int tagnew){
//point
x = xnew;
y = ynew;
z = znew;
tag = 1; //elemen terakhir sebagai pelengkap, untuk keperluan transformasi
}
Point::~Point()
{
//dtor
}
void Point::transformation (float (&matrix)[4][4]){
//melakukan transformasi "matrix" terhadap "point"
vector<float> temp;
for (int i=0; i<4; i++){
float result;
result = x * matrix[0][i] + y * matrix[1][i] + z * matrix[2][i] + tag * matrix[3][i];
temp.push_back(result);
}
//pembagian dengan komponen terakhir vektor 3d
temp[0] = temp[0] / temp[3];
temp[1] = temp[1] / temp[3];
temp[2] = temp[2] / temp[3];
//Assign nilai point dengan nilai baru
x = temp[0];
y = temp[1];
z = temp[2];
tag = temp[3];
}
void Point::translation(float dx, float dy, float dz){
float matrix[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[i][j] = 0;
}
}
matrix[0][0]=1; //Transform_XAxis
matrix[1][1]=1; //Transform_YAxis
matrix[2][2]=1; //Transform_ZAxis
matrix[3][3]=1; //nilai akhir matrix
//nilai translasi
matrix[3][0]=dx;
matrix[3][1]=dy;
matrix[3][2]=dz;
transformation(matrix);
}
void Point::scale(float scaleX, float scaleY, float scaleZ){
float matrix[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[i][j] = 0;
}
}
matrix[0][0]=scaleX;
matrix[1][1]=scaleY;
matrix[2][2]=scaleZ;
matrix[3][3]=1; //nilai akhir matrix
transformation(matrix);
}
void Point::rotateX(float degree){
//ClockWise
float degreeRadian = degree * PI / 180.0;
float matrix[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[i][j] = 0;
}
}
matrix[0][0]=1;
matrix[3][3]=1;
matrix[1][1]=cos(degreeRadian);
matrix[1][2]=sin(degreeRadian);
matrix[2][1]=sin(degreeRadian)*(-1);
matrix[2][2]=cos(degreeRadian);
transformation(matrix);
}
void Point::rotateY(float degree){
//ClockWise
float degreeRadian = degree * PI / 180.0;
float matrix[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[i][j] = 0;
}
}
matrix[1][1]=1;
matrix[3][3]=1;
matrix[0][0]=cos(degreeRadian);
matrix[2][0]=sin(degreeRadian);
matrix[0][2]=sin(degreeRadian)*(-1);
matrix[2][2]=cos(degreeRadian);
transformation(matrix);
}
void Point::rotateZ(float degree){
//ClockWise
float degreeRadian = degree * PI / 180.0;
float matrix[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[i][j] = 0;
}
}
matrix[2][2]=1;
matrix[3][3]=1;
matrix[0][0]=cos(degreeRadian);
matrix[0][1]=sin(degreeRadian);
matrix[1][0]=sin(degreeRadian)*(-1);
matrix[1][1]=cos(degreeRadian);
transformation(matrix);
}
void Point::setX(float xnew) {
x = xnew;
}
void Point::setY(float ynew) {
y = ynew;
}
void Point::setZ(float znew) {
z = znew;
}
float Point::getDistance(Point p2) {
return sqrt(pow(p2.x - x, 2) + pow(p2.y - y, 2) + pow(p2.z - z, 2));
}
void Point::print() {
cout << "x:" << x << " y:" << y << " z:" << z << endl;
}