This repository was archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRectangle.cpp
More file actions
96 lines (75 loc) · 2.96 KB
/
CRectangle.cpp
File metadata and controls
96 lines (75 loc) · 2.96 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
#include "CRectangle.h"
CRectangle::CRectangle(float x, float y, float width, float height, const CColor &col, int lt, bool isFilled) :
CShape2D(col, lt, isFilled), bottomLeftCorner(x, y), width(width), height(height) {}
std::ostream &CRectangle::print(std::ostream &os) const {
CShape2D::print(os);
std::cout << "bottomLeftCorner: [" << bottomLeftCorner << "]; width: " << width << "; height: " << height << "; ";
return os;
}
const CPoint2D &CRectangle::getBottomLeftCorner() const {
return bottomLeftCorner;
}
void CRectangle::setBottomLeftCorner(const CPoint2D &bottomLeftCorner) {
CRectangle::bottomLeftCorner = bottomLeftCorner;
}
float CRectangle::getWidth() const {
return width;
}
void CRectangle::setWidth(float width) {
CRectangle::width = width;
}
float CRectangle::getHeight() const {
return height;
}
void CRectangle::setHeight(float height) {
CRectangle::height = height;
}
void CRectangle::scale(float factor) {
height *= factor;
width *= factor;
}
void CRectangle::move(const CPoint2D &loc) {
bottomLeftCorner.add(loc);
}
float CRectangle::area() const {
return width * height;
}
void CRectangle::render(CCanvas &ref) const {
const CPoint2D &bottomLeft = bottomLeftCorner;
const CPoint2D topRight(bottomLeft.getX() + width - 1, bottomLeft.getY() + height - 1);
const CPoint2D topLeft(topRight.getX() - (topRight.getX() - bottomLeft.getX()), topRight.getY());
const CPoint2D bottomRight(topRight.getX(), topRight.getY() - (topRight.getY() - bottomLeft.getY()));
int x = bottomLeft.getX();
int y = bottomLeft.getY();
int w = width - 1;
int h = height - 1;
for (int i = 0; i < w; i++) {
ref.writeChecked(x + i, y, colorString + CCanvas::horizontal + COLOR_RESET);
ref.writeChecked(x + w - i, y + h, colorString + CCanvas::horizontal + COLOR_RESET);
}
for (int i = 0; i < h; i++) {
ref.writeChecked(x, y + i, colorString + CCanvas::vertical + COLOR_RESET);
ref.writeChecked(x + w, y + h - i, colorString + CCanvas::vertical + COLOR_RESET);
}
if (isFilled) {
for (int yL = 1; yL < h; yL++) {
for (int xL = 1; xL < w; xL++) {
ref.writeChecked(x + xL, y + yL, colorString + CCanvas::fill + COLOR_RESET);
}
}
}
ref.writeChecked(bottomLeft.getX(), bottomLeft.getY(), colorString + CCanvas::corner + COLOR_RESET);
ref.writeChecked(bottomRight.getX(), bottomRight.getY(), colorString + CCanvas::corner + COLOR_RESET);
ref.writeChecked(topLeft.getX(), topLeft.getY(), colorString + CCanvas::corner + COLOR_RESET);
ref.writeChecked(topRight.getX(), topRight.getY(), colorString + CCanvas::corner + COLOR_RESET);
}
std::istream &CRectangle::read(std::istream &is) {
CShape2D::read(is);
std::cout << "Bottom left corner:\n";
std::cin >> bottomLeftCorner;
std::cout << "Width: ";
std::cin >> width;
std::cout << "Height: ";
std::cin >> height;
return is;
}