-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.cpp
67 lines (52 loc) · 1.1 KB
/
Geometry.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
#include "Geometry.h"
// Implementation of Point and Ray classes.
Point Point::Infinite(){
return Point(1E20,1E20,1E20);
}
Point Point::cross(Point p){
return Point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
}
void Point::normalize(){
double l = this->length();
x /= l;
y /= l;
z /= l;
}
Point::Point(double xo, double yo, double zo){
x = xo;
y = yo;
z = zo;
}
Point Point::operator- (Point p){
return Point(x-p.x,y-p.y,z-p.z);
}
Point Point::operator*(double s){
return Point(x*s,y*s,z*s);
}
Point Point::operator+(Point p){
return Point(p.x+x,p.y+y,p.z+z);
}
bool Point::operator < (Point p){
return (this->length() < (p).length());
}
// Dot product
double Point::operator* (Point p){
return x*p.x+y*p.y+z*p.z;
}
double Point::length(){
return sqrt(x*x+y*y+z*z);
}
Ray::Ray(){
}
Ray::Ray(Point po, Point vo){
p = po;
v = vo;
v.normalize();
}
Ray Ray::reflect(Point normal, Point m){
Ray ret;
double tmp = -(normal*v);
Point vo = v+(normal*tmp*2.0);
Point p = Point(m.x,m.y,m.z);
return Ray(p,vo);
}