-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPointsAndPlanes.cpp
225 lines (185 loc) · 3.63 KB
/
PointsAndPlanes.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
struct Point3
{
double x,y,z;
Point3(double _x=0, double _y=0, double _z=0)
{
x=_x;
y=_y;
z=_z;
}
bool operator == (Point3 other)
{
return abs(x-other.x)<EPS && abs(y-other.y)<EPS && abs(z-other.z)<EPS;
}
bool operator != (Point3 other)
{
return abs(x-other.x)>EPS || abs(y-other.y)>EPS || abs(z-other.z)>EPS;
}
double length()
{
return sqrt(x*x + y*y + z*z);
}
Point3 operator * (double k)
{
return Point3(k*x, k*y, k*z);
}
Point3 normalize()
{
return (*this) * (1/length());
}
Point3 operator + (Point3 other)
{
return Point3(x+other.x, y+other.y, z+other.z);
}
Point3 operator - (Point3 other)
{
return Point3(x-other.x, y-other.y, z-other.z);
}
double dist(Point3 other)
{
return (*this-other).length();
}
double operator ^ (Point3 other)
{
return x*other.x + y*other.y + z*other.z;
}
Point3 operator * (Point3 other)
{
return Point3(y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x);
}
void display()
{
printf("Point3 (%lf , %lf , %lf)\n",x,y,z);
}
};
Point3 operator * (double k,Point3 p)
{
return Point3(k*p.x, k*p.y, k*p.z);
}
struct Point2
{
double x,y;
Point2(double _x=0, double _y=0)
{
x=_x;
y=_y;
}
double length()
{
return sqrt(x*x + y*y);
}
Point2 operator * (double k)
{
return Point2(k*x, k*y);
}
Point2 normalize()
{
return (*this) * (1/length());
}
Point2 operator + (Point2 other)
{
return Point2(x+other.x, y+other.y);
}
Point2 operator - (Point2 other)
{
return Point2(x-other.x, y-other.y);
}
double dist(Point2 other)
{
return (*this-other).length();
}
double operator ^ (Point2 other)
{
return x*other.x + y*other.y;
}
double operator * (Point2 other)
{
return x*other.y - y*other.x;
}
void display()
{
printf("Point2 (%lf , %lf)\n",x,y);
}
};
Point2 operator * (double k,Point2 p)
{
return Point2(k*p.x, k*p.y);
}
struct Plane
{
double a,b,c,d;
Point3 A,B,C;
Point3 N,O,X,Y;
Plane() {}
Plane(Point3 _AA, Point3 _BB, Point3 _CC)
{
A=_AA;
B=_BB;
C=_CC;
Point3 normal = (B-A)*(C-A);
N = normal.normalize();
a = N.x;
b = N.y;
c = N.z;
d = -(A^N);
O = Point3(-d*a, -d*b, -d*c);
if(A!=O) X = (A-O).normalize();
else X = (B-O).normalize();
Y = normal*X;
}
Plane(double _a,double _b,double _c,double _d)
{
double len = sqrt(_a*_a + _b*_b + _c*_c);
a = _a/len;
b = _b/len;
c = _c/len;
d = _d/len;
N=Point3(a,b,c);
if(a!=0)
{
A = Point3(-(d+0*b+0*c)/a,0,0);
B = Point3(-(d+1*b+0*c)/a,1,0);
C = Point3(-(d+0*b+1*c)/a,0,1);
}
else if(b!=0)
{
A = Point3(0,-(d+0*a+0*c)/b,0);
B = Point3(1,-(d+1*a+0*c)/b,0);
C = Point3(0,-(d+0*a+1*c)/b,1);
}
else
{
A = Point3(0,0,-(d+0*a+0*b)/c);
B = Point3(1,0,-(d+1*a+0*b)/c);
C = Point3(0,1,-(d+0*a+1*b)/c);
}
O = Point3(-d*a, -d*b, -d*c);
if(A!=O) X = (A-O).normalize();
else X = (B-O).normalize();
Y = N*X;
}
Point3 project(Point3 P)
{
return (P-(P^N)*N) + O;
}
Point2 project2D(Point3 P)
{
P = project(P);
return Point2(P^X,P^Y);
}
double distance(Point3 p)
{
return abs(a*p.x + b*p.y + c*p.z + d);
}
void display()
{
printf("Plane (%lf , %lf , %lf , %lf)\n",a,b,c,d);
printf("A : "); A.display();
printf("B : "); B.display();
printf("C : "); C.display();
printf("N : "); N.display();
printf("O : "); O.display();
printf("X : "); X.display();
printf("Y : "); Y.display();
}
};