-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpoint2d.cpp
More file actions
executable file
·101 lines (83 loc) · 2.35 KB
/
point2d.cpp
File metadata and controls
executable file
·101 lines (83 loc) · 2.35 KB
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
class matrix;
#include "point2d.h"
#include "matrix.h"
#include <cmath>
point2d::point2d(double p_x, double p_y) :x(p_x), y(p_y){
}
point2d::point2d(double p_v) : x(p_v), y(p_v){
}
point2d &point2d::operator+=(const point2d &p_p){
this->x += p_p.x;
this->y += p_p.y;
return *this;
}
point2d &point2d::operator*=(const point2d &p_p){
this->x *= p_p.x;
this->y *= p_p.y;
return *this;
}
point2d &point2d::operator-=(const point2d &p_p){
this->x -= p_p.x;
this->y -= p_p.y;
return *this;
}
point2d operator+(const point2d &p_a, const point2d &p_b){
point2d ret = p_a;
ret += p_b;
return ret;
}
point2d operator*(const point2d &p_a, const point2d &p_b){
point2d ret = p_a;
ret *= p_b;
return ret;
}
point2d operator-(const point2d &p_a, const point2d &p_b){
point2d ret = p_a;
ret -= p_b;
return ret;
}
point2d operator+(const point2d &p_a, double p_b){
point2d ret = p_a;
ret.x += p_b; ret.y += p_b;
return ret;
}
point2d operator-(const point2d &p_a, double p_b){
point2d ret = p_a;
ret.x -= p_b; ret.y -= p_b;
return ret;
}
point2d operator*(const point2d &p_a, double p_b){
point2d ret = p_a;
ret.x *= p_b; ret.y *= p_b;
return ret;
}
point2d operator/(const point2d &p_a, double p_b){
point2d ret = p_a;
ret.x /= p_b; ret.y /= p_b;
return ret;
}
point2d operator/(double p_a, const point2d &p_b){
point2d ret = p_b;
ret.x = p_a / ret.x; ret.y = p_a / ret.y; return ret;
}
void point2d::normalize(){
double len = this->length();
this->x /= len;
this->y /= len;
}
double point2d::length(){
return sqrt(x*x + y*y);
}
double distance(const point2d &p_a, const point2d &p_b){
return sqrt((p_a.y - p_b.y)*(p_a.y - p_b.y) + (p_a.x - p_b.x)*(p_a.x - p_b.x));
}
point3d barycentric(const point2d &p_a, const point2d &p_b, const point2d &p_c, const point2d &p_r){
double l1 = ((p_b.y - p_c.y)*(p_r.x - p_c.x) + (p_c.x - p_b.x)*(p_r.y - p_c.y)) / ((p_b.y - p_c.y)*(p_a.x - p_c.x) + (p_c.x - p_b.x)*(p_a.y - p_c.y));
double l2 = ((p_c.y - p_a.y)*(p_r.x - p_c.x) + (p_a.x - p_c.x)*(p_r.y - p_c.y)) / ((p_c.y - p_a.y)*(p_b.x - p_c.x) + (p_a.x - p_c.x)*(p_b.y - p_c.y));
double l3 = 1 - l1 - l2;
return point3d(l1, l2, l3);
}
std::ostream &operator<<(std::ostream &p_out, const point2d &p_p){
p_out << "(" << p_p.x << ", " << p_p.y << ")";
return p_out;
}