-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.h
82 lines (65 loc) · 1.82 KB
/
Point.h
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
//
// Created by rkindela on 12-04-2018.
//
#ifndef PMTOOL_POINT_H
#define PMTOOL_POINT_H
# include <cmath>
#include <iostream>
template <class T>
struct Point {
T x, y, z;
explicit Point(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z){};
template <class R>
explicit Point(const Point<R>& rp): x(rp.x), y(rp.y), z(rp.z){};
float distance(const Point<T>& p)
{
return sqrt((p.x-x)*(p.x-x) + (p.y-y)*(p.y-y) + (p.z-z)*(p.z-z));
}
Point<T> multiply(float value)
{
return Point<T>(int(x*value), int(y*value), int(z*value));
}
T squareDistance(const Point<T>& p)
{
return ((p.x-x)*(p.x-x) + (p.y-y)*(p.y-y) + (p.z-z)*(p.z-z));
}
Point<T> operator+(Point<T> pointb)
{
return Point<T>(x + pointb.x, y + pointb.y, z+pointb.z);
}
Point<T> operator-(Point<T> pointb)
{
return Point<T>(x - pointb.x, y - pointb.y, z-pointb.z);
}
Point<T>& operator=(const Point<T> pointb)
{
x = pointb.x;
y = pointb.y;
z = pointb.z;
return *this;
}
template <class S>
friend std::ostream& operator<<(std::ostream& os, const Point<S>& p);
template <class S>
friend std::ostream& operator<<(std::ostream& os, const Point<S>* p);
};
template<class T>
std::ostream& operator<<(std::ostream& os, const Point<T>& p)
{
if (p.z == 0)
os<<"("<<p.x<<", "<<p.y<<")";
else
os<<"("<<p.x<<", "<<p.y<<", "<<p.z<<")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os, const Point<T>* p)
{
if (p->z == 0)
os<<"("<<p->x<<", "<<p->y<<")";
else
os<<"("<<p->x<<", "<<p->y<<", "<<p->z<<")";
return os;
}
typedef Point<int> POINT;
#endif //PMTOOL_POINT_H