-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.cpp
More file actions
193 lines (161 loc) · 4.18 KB
/
Rectangle.cpp
File metadata and controls
193 lines (161 loc) · 4.18 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
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
186
187
188
189
190
191
192
193
// ===========================================================================
// Rectangle.cpp
// ===========================================================================
#include "Rectangle.h"
namespace Rectangles {
// c'tor(s)
Rectangle::Rectangle(double x, double y, double width, double height)
{
m_x = x;
m_y = y;
if (width >= 0.0) {
m_width = width;
}
else {
m_width = 0.0;
}
if (height >= 0.0) {
m_height = height;
}
else {
m_height = 0.0;
}
}
// note constructor chaining
Rectangle::Rectangle()
: Rectangle(0.0, 0.0, 0.0, 0.0)
{}
// getter/setter
double Rectangle::getX() { return m_x; }
double Rectangle::getY() { return m_y; }
double Rectangle::getWidth() { return m_width; }
double Rectangle::getHeight() { return m_height; }
void Rectangle::setX(double x)
{
m_x = x;
}
void Rectangle::setY(double y)
{
m_y = y;
}
void Rectangle::setWidth(double width)
{
if (width >= 0.0) {
m_width = width;
}
else {
std::cout << "Expected positive value for 'width': " << width << std::endl;
}
}
void Rectangle::setHeight(double height)
{
if (height >= 0.0) {
m_height = height;
}
else {
std::cout << "Expected positive value for 'height: " << height << std::endl;
}
}
// methods
void Rectangle::moveTo(double x, double y)
{
m_x += x;
m_y += y;
}
bool Rectangle::equals(Rectangle other)
{
if (m_x != other.m_x) {
return false;
}
else if (m_y != other.m_y) {
return false;
}
else if (m_width != other.m_width) {
return false;
}
else if (m_height != other.m_height) {
return false;
}
else
{
return true;
}
}
double Rectangle::circumference()
{
return 2.0 * (m_width + m_height);
}
double Rectangle::area()
{
return m_width * m_height;
}
bool Rectangle::isSquare()
{
bool result = (m_width == m_height);
return result;
}
Point Rectangle::center()
{
double x = m_x + m_width / 2.0;
double y = m_y - m_height / 2.0;
Point result(x, y);
return result;
}
double Rectangle::diagonal()
{
return std::sqrt(
m_width * m_width + m_height * m_height
);
}
Rectangle Rectangle::intersection(Rectangle rect)
{
if (m_y + m_height < rect.m_y) {
Rectangle empty;
return empty;
}
else if (m_y > rect.m_y + rect.m_height) {
Rectangle empty;
return empty;
}
if (m_x + m_width < rect.m_x) {
Rectangle empty;
return empty;
}
else if (rect.m_x + rect.m_width < m_x) {
Rectangle empty;
return empty;
}
double left, width;
if (m_x < rect.m_x) {
left = rect.m_x;
width = m_x + m_width - left;
}
else {
left = m_x;
width = rect.m_x + rect.m_width - left;
}
double top, height;
if (m_y < rect.m_y) {
top = rect.m_y;
height = m_y + m_height - top;
}
else {
top = m_y;
height = rect.m_y + rect.m_height - top;
}
Rectangle result(left, top, width, height);
return result;
}
void Rectangle::print()
{
std::cout << "Rectangle: (" << getX() << ',' << getY() << "),(Width="
<< getWidth() << ", Height=" << getHeight() << ") " << std::endl;
std::cout
<< "[Area=" << area()
<< ", Circumference=" << circumference()
<< ", IsSquare=" << isSquare() << ']' << std::endl;
}
}
// ===========================================================================
// End-of-File
// ===========================================================================