-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.hh
98 lines (77 loc) · 2.24 KB
/
Point.hh
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
/*
Author: dok
Desc: Basic Point class
*/
#ifndef Point_hh
#define Point_hh
#include "Vector.hh"
#include <iostream>
using std::ostream;
class Point
{
private:
float c[3]; // three components of point coordinates (x,y,z)
public:
/*-------- Constructors -------------*/
Point(float x, float y, float z){ // construct from coordinates
c[0] = x; c[1] = y; c[2] = z;
}
Point(const Vector& v){ // construct from Vector
c[0] = v[0]; c[1] = v[1]; c[2] = v[2];
}
Point(const Point& p){ // copy consructor
c[0] = p.x(); c[1] = p.y(); c[2] = p.z();
}
Point(){ // default constructor
c[0] = 0; c[1] = 0; c[2] = 0;
}
/*-------------- Accessors --------------*/
float x() const{return c[0];}
float y() const{return c[1];}
float z() const{return c[2];}
/*-------------- Operators -------------*/
float operator[](int i) const{ // [] const operator
return c[i%3];
}
float& operator[](int i){ // [] operator
return c[i%3];
}
Point operator+(const Vector& v){ // + operator (add vector)
return Point(c[0]+v.x(), c[1]+v.y(), c[2]+v.z());
}
Point operator*(const Vector& v){ // * operator (multiply by vector (wtf)
return Point(c[0]*v.x(), c[1]*v.y(), c[2]*v.z());
}
Point& operator+=(const Point& p){ // += operator for point
c[0] += p.x();
c[1] += p.y();
c[2] += p.z();
return *this;
}
Point& operator+=(const Vector& v){ // += operator for vector
c[0] += v.x();
c[1] += v.y();
c[2] += v.z();
return *this;
}
Point& operator-=(const Point& p){ // -= operator for point
c[0] -= p.x();
c[1] -= p.y();
c[2] -= p.z();
return *this;
}
Point& operator-=(const Vector& v){ // -= operator for vector
c[0] -= v.x();
c[1] -= v.y();
c[2] -= v.z();
return *this;
}
friend Vector operator-(const Point& p1, const Point& p2){ // vector between 2 points
return Vector(p1.x() - p2.x(), p1.y() - p2.y(), p1.z() - p2.z());
}
friend ostream& operator<<(ostream& os, const Point& p){ // stream to console
os << "(" << p.x() << "," << p.y() << "," << p.z() << ")";
return os;
}
};
#endif