-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.cpp
More file actions
160 lines (132 loc) · 2.61 KB
/
point.cpp
File metadata and controls
160 lines (132 loc) · 2.61 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include<iostream>
#include<cmath>
#include"point.h"
namespace geom
{
point::point() : d_x(0.0), d_y(0.0)
{}
point::point(double x, double y) : d_x(x), d_y(y)
{}
point point::cartesian(double x, double y)
{
return point(x,y);
}
point point::polar(double rho, double theta)
{
return point(rho*std::cos(theta),rho*std::sin(theta));
}
double point::x() const
{
return d_x;
}
double point::y() const
{
return d_y;
}
double point::distance(const point& p) const
{
double dx = p.d_x-d_x, dy = p.d_y-d_y;
return std::sqrt(dx*dx + dy*dy);
}
//determinant |p1p2,p1p3|
double point::determinant(const point& p1, const point& p2, const point& p3)
{
return (p2.d_x-p1.d_x)*(p3.d_y-p1.d_y) - (p2.d_y-p1.d_y)*(p3.d_x-p1.d_x);
}
//p above (p1,p2) <=> determinant(p1,p2,p)>0
bool point::above(const point& p1, const point& p2) const
{
return determinant(p1,p2,*this) > 0.0;
}
bool point::below(const point& p1, const point& p2) const
{
return determinant(p1,p2,*this) < 0.0;
}
//[p1,q1] cuts [p2,q2] <=> p1 and q1 are on opposite sides of (p2,q2)
//and p2 and q2 are on opposite sides of (p1,q1)
bool point::intersection(const point& p1, const point& q1,
const point& p2, const point& q2)
{
return (p1.above(p2,q2) == q1.below(p2,q2) &&
p2.above(p1,q1) == q2.below(p1,q1));
}
void point::moveTo(double x, double y)
{
d_x = x; d_y = y;
}
void point::move(double dx, double dy)
{
d_x += dx; d_y += dy;
}
point& point::operator*=(double val)
{
d_x *= val; d_y *= val;
return *this;
}
point operator/(double val, const point& p)
{
point res(p);
res /= val;
return res;
}
point& point::operator/=(double val)
{
d_x /= val; d_y /= val;
return *this;
}
point point::operator/(double val) const
{
point res(*this);
res /= val;
return res;
}
point& point::operator+=(const point& p)
{
d_x += p.d_x; d_y += p.d_y;
return *this;
}
point point::operator+(const point& p) const
{
point res(*this);
res += p;
return res;
}
bool point::operator==(const point& p) const
{
return (d_x == p.d_x) && (d_y == p.d_y);
}
bool point::operator!=(const point& p) const
{
return (d_x != p.d_x) || (d_y != p.d_y);
}
void point::print(std::ostream& ost) const
{
ost<<'('<<d_x<<','<<d_y<<')';
}
void point::read(std::istream& ist)
{
char c;
ist>>c;
if (c == '(')
{
double x;
ist>>x>>c;
if (c == ',')
{
double y;
ist>>y>>c;
if (c == ')') { d_x = x; d_y = y; }
}
}
}
std::ostream& operator<<(std::ostream& ost, const point& p)
{
p.print(ost);
return ost;
}
std::istream& operator>>(std::istream& ist, point& p)
{
p.read(ist);
return ist;
}
}