-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect.cpp
187 lines (140 loc) · 4.42 KB
/
rect.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "graphics.h"
#include "rect.h"
#include <iostream>
using namespace std;
/********************* Dimensions Struct ********************/
dimensions::dimensions() : width(0), height(0) {}
dimensions::dimensions(double w, double h) : width(w), height(h) {}
ostream& operator << (ostream& outs, const dimensions &d) {
outs << "[" << d.width << ", " << d.height << "]";
return outs;
}
Rect::Rect() : Shape(), size({0, 0}) {
}
Rect::Rect(dimensions size) : Shape() {
setSize(size);
}
Rect::Rect(color fill) : Shape(fill), size({0, 0}) {
}
Rect::Rect(point2D center) : Shape(center), size({0, 0}) {
}
Rect::Rect(color fill, point2D center) : Shape(fill, center), size({0, 0}) {
}
Rect::Rect(double red, double green, double blue, double alpha) : Shape(red, green, blue, alpha), size({0, 0}) {
}
Rect::Rect(double x, double y) : Shape(x, y), size({0, 0}) {
}
Rect::Rect(double red, double green, double blue, double alpha, double x, double y) : Shape(red, green, blue, alpha, x, y), size({0, 0}) {
}
Rect::Rect(color fill, double x, double y) : Shape(fill, x, y), size({0, 0}) {
}
Rect::Rect(double red, double green, double blue, double alpha, point2D center) : Shape(red, green, blue, alpha, center), size({0, 0}) {
}
Rect::Rect(color fill, dimensions size) : Shape(fill) {
setSize(size);
}
Rect::Rect(point2D center, dimensions size) : Shape(center) {
setSize(size);
}
Rect::Rect(color fill, point2D center, dimensions size) : Shape(fill, center) {
setSize(size);
}
Rect::Rect(double red, double green, double blue, double alpha, dimensions size) : Shape(red, green, blue, alpha) {
setSize(size);
}
Rect::Rect(double x, double y, dimensions size) : Shape(x, y) {
setSize(size);
}
Rect::Rect(double red, double green, double blue, double alpha, double x, double y, dimensions size) : Shape(red, green, blue, alpha, x, y) {
setSize(size);
}
Rect::Rect(color fill, double x, double y, dimensions size) : Shape(fill, x, y) {
setSize(size);
}
Rect::Rect(double red, double green, double blue, double alpha, point2D center, dimensions size) : Shape(red, green, blue, alpha, center) {
setSize(size);
}
dimensions Rect::getSize() const {
return size;
}
double Rect::getWidth() const {
return size.width;
}
double Rect::getHeight() const {
return size.height;
}
double Rect::getLeftX() const {
return center.x - (size.width / 2.0);
}
double Rect::getRightX() const {
return center.x + (size.width / 2.0);
}
double Rect::getTopY() const {
return center.y - (size.height / 2.0);
}
double Rect::getBottomY() const {
return center.y + (size.height / 2.0);
}
double Rect::getSpeed() const {
return speed;
}
bool Rect::getFalling() const {
return falling;
}
void Rect::setSize(dimensions size) {
if (size.width >= 0 && size.height >= 0) {
this->size = size;
}
}
void Rect::setSize(double width, double height) {
setSize({width, height});
}
void Rect::setWidth(double width) {
setSize({width, size.height});
}
void Rect::setHeight(double height) {
setSize({size.width, height});
}
void Rect::setSpeed(int speed) {
this->speed = speed;
}
void Rect::setFalling(bool falling) {
this->falling = falling;
}
void Rect::changeSize(double deltaWidth, double deltaHeight) {
setSize({size.width + deltaWidth, size.height + deltaHeight});
}
void Rect::changeWidth(double delta) {
setSize({size.width + delta, size.height});
}
void Rect::changeHeight(double delta) {
setSize({size.width, size.height + delta});
}
bool Rect::isOverlapping(const Rect &r) const {
// There are only two cases when rectangles are *not* overlapping:
// 1. when one is to the left of the other
// 2. when one is above the other
if (r.getRightX() < this->getLeftX()) {
return false;
}
else if (r.getLeftX() > this->getRightX()) {
return false;
}
else if (r.getBottomY() < this->getTopY()) {
return false;
}
else if (r.getTopY() > this->getBottomY()) {
return false;
}
return true;
}
void Rect::draw() const {
// Don't forget to set the color to the fill field
glBegin(GL_QUADS);
glColor4f(this->getRed(), this->getGreen(), this->getBlue(), this->getOpacity());
glVertex3f(this->getLeftX(),this->getBottomY(),0.0);
glVertex3f(this->getLeftX(),this->getTopY(), 0.0);
glVertex3f(this->getRightX(),this->getTopY(), 0.0);
glVertex3f(this->getRightX(),this->getBottomY(), 0.0);
glEnd();
}