-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.cpp
155 lines (115 loc) · 2.69 KB
/
shape.cpp
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
#include <iostream>
#include <cassert>
#include "shape.h"
#include "geom.h"
using namespace std;
bool Shape::containsU(const UnitF &u) const {
return contains(u.getX(), u.getY());
}
//
Circle::Circle(double x, double y, double r) {
_x = x;
_y = y;
_r = r;
}
bool Circle::contains(double x, double y) const {
return (Pif2(x - _x, y - _y) <= _r*_r);
}
double Circle::getX() const {
return _x;
}
double Circle::getY() const {
return _y;
}
double Circle::getR() const {
return _r;
}
//
Rectangle::Rectangle(double x0, double x1, double y0, double y1) {
_x0 = x0;
_x1 = x1;
_y0 = y0;
_y1 = y1;
}
bool Rectangle::contains(double x, double y) const {
return ( (x >= _x0) && (x <= _x1) && (y >= _y0) && (y <= _y1) );
}
//
Polygon::Polygon(vector<double> x, vector<double> y) {
assert(x.size() > 2);
assert(x.size() == y.size());
_x = x;
_y = y;
}
bool Polygon::contains(double x, double y) const {
bool sign = (orientedS(_x[_x.size() - 1], _y[_y.size() - 1], _x[0], _y[0], x, y) > 0);
for (unsigned int i = 0; i < _x.size() - 1; ++i) {
if ( (orientedS(_x[i], _y[i], _x[i + 1], _y[i + 1], x, y) > 0) != sign ) {
return false;
}
}
return true;
}
//
Sector::Sector(double x, double y, double r, double a1, double a2) {
_x = x;
_y = y;
_r = r;
_a1 = a1;
_a2 = a2;
}
bool Sector::contains(double x, double y) const {
double rSqr = Pif2(_x - x, _y - y);
double a = atan2(y - _y, x - _x);
return ( (rSqr <= _r*_r) && (a >= _a1) && (a <= _a2) );
}
//
ShapeSubtraction::ShapeSubtraction() {
_s1 = 0;
_s2 = 0;
}
ShapeSubtraction::~ShapeSubtraction() {
delete _s1;
delete _s2;
_s1 = 0;
_s2 = 0;
}
void ShapeSubtraction::set(Shape* s1, Shape* s2) {
delete _s1;
delete _s2;
_s1 = s1;
_s2 = s2;
}
bool ShapeSubtraction::contains(double x, double y) const {
return (_s1->contains(x, y) && !_s2->contains(x, y));
}
ShapeCommon::ShapeCommon() {
_s1 = 0;
_s2 = 0;
}
ShapeCommon::~ShapeCommon() {
delete _s1;
delete _s2;
_s1 = 0;
_s2 = 0;
}
void ShapeCommon::set(Shape *s1, Shape *s2) {
delete _s1;
delete _s2;
_s1 = s1;
_s2 = s2;
}
bool ShapeCommon::contains(double x, double y) const {
return (_s1->contains(x, y) && _s2->contains(x, y));
}
//
ShapeInvX::ShapeInvX(const Shape& s, const double width) : _s(s), _width(width) {
}
bool ShapeInvX::contains(double x, double y) const {
return _s.contains(_width - x, y);
}
ShapeInvY::ShapeInvY(const Shape& s, const double height) : _s(s), _height(height) {
}
bool ShapeInvY::contains(double x, double y) const {
return _s.contains(x, _height - y);
}