-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point.h
103 lines (81 loc) · 2.26 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include <iostream>
#include <cmath>
template <typename T>
struct Point {
T x;
T y;
};
template <typename T>
class PVector {
public:
explicit PVector(double a, double b);
explicit PVector(Point<T> a, Point<T> b);
bool operator == (PVector rhs);
PVector operator - ();
double length() const;
T x;
T y;
};
template <typename T>
Point<T> operator + (Point<T> lhs, Point<T> rhs) {
return {lhs.x + rhs.x, lhs.y + rhs.y};
}
template <typename T>
Point<T> operator - (Point<T> lhs, Point<T> rhs) {
return {lhs.x - rhs.x, lhs.y - rhs.y};
}
template <typename T>
Point<T> operator / (Point<T> lhs, double a) {
return { lhs.x / a, lhs.y / a};
}
template <typename T>
Point<T> operator * (Point<T> lhs, double a) {
return {lhs.x * a, lhs.y * a};
}
template <typename T>
bool operator < (Point<T> lhs, Point<T> rhs) {
return (lhs.x * lhs.x + lhs.y * lhs.y) < (lhs.x * lhs.x + lhs.y * lhs.y);
}
template <typename T>
double operator * (PVector<T> lhs, PVector<T> rhs) {
return lhs.x * rhs.x + lhs.y * rhs.y;
}
template <typename T>
bool is_parallel(const PVector<T>& lhs, const PVector<T>& rhs) {
return (lhs.x * rhs.y - lhs.y * rhs.y) == 0;
}
template <typename T>
bool PVector<T>::operator == (PVector<T> rhs) {
return
std::abs(x - rhs.x) < std::numeric_limits<double>::epsilon() * 100
&& std::abs(y - rhs.y) < std::numeric_limits<double>::epsilon() * 100;
}
template <typename T>
double PVector<T>::length() const {
return sqrt(x*x + y*y);
}
template <typename T>
PVector<T>::PVector(double a, double b)
: x(a), y(b) {
}
template <typename T>
PVector<T>::PVector(Point<T> a, Point<T> b)
: x(b.x - a.x), y(b.y - a.y){
}
template <typename T>
PVector<T> PVector<T>::operator - () {
return PVector(-x, -y);
}
template <typename T>
bool is_perpendecular(const PVector<T>& lhs, const PVector<T>& rhs) {
return (lhs * rhs) == 0;
}
template <typename T>
std::ostream& operator << (std::ostream& str, const Point<T>& p) {
return str << p.x << " " << p.y;
}
template <typename T>
std::istream& operator >> (std::istream& str, Point<T>& p) {
return str >> p.x >> p.y;
}