-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRhombus.cpp
More file actions
119 lines (102 loc) · 3.32 KB
/
Rhombus.cpp
File metadata and controls
119 lines (102 loc) · 3.32 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
#include "Rhombus.h"
#include <cassert>
#include<math.h>
using namespace std;
Rhombus::Rhombus(int rhomDiagonal, const string &desc) : Shape("Rhombus", desc), diagonal(rhomDiagonal) {
if (diagonal % 2 == 0)
++diagonal;
assert(diagonal > 0);
}
Rhombus::~Rhombus() = default;
const Rhombus &Rhombus::operator=(const Rhombus &rhs) {
if (this == &rhs) return *this;
Shape::operator=(rhs);
diagonal = rhs.diagonal;
return *this;
}
int Rhombus::getDiagonal() const {
return diagonal;
}
string Rhombus::toString() const {
string temp{Shape::toString()};
temp.append("\nB. box width: " + to_string(bBoxWidth()));
temp.append("\nB. box height: " + to_string(bBoxHeight()));
temp.append("\nScr area: " + to_string(scrArea()));
temp.append("\nGeo area: " + doubleToString(geoArea(), 2));
temp.append("\nScr perimeter: " + to_string(scrPerimeter()));
temp.append("\nGeo perimeter: " + doubleToString(geoPerimeter(), 2));
return temp;
}
void Rhombus::scale(int n) {
if (n != 0) {
if (n % 2 == 0) // n is even
{
if (getDiagonal() + n > 0)
diagonal += n;
} else // n is odd, find next even value for n
{
if (n > 0) // if positive increase n by one
{
++n;
if (getDiagonal() + n > 0)
diagonal += n;
} else // if negative reduce n by one
{
--n;
if (getDiagonal() + n > 0)
diagonal += n;
}
}
}
}
double Rhombus::geoArea() const {
return getDiagonal() * static_cast<double>(getDiagonal()) / 2;
}
double Rhombus::geoPerimeter() const {
return 2 * sqrt(2) * getDiagonal();
}
int Rhombus::scrArea() const {
int n{getDiagonal() / 2}; // n = floor(diagonal / 2), diagonal positive
return 2 * n * (n + 1) + 1;
}
int Rhombus::scrPerimeter() const {
return 2 * (getDiagonal() - 1);
}
int Rhombus::bBoxHeight() const {
return getDiagonal();
}
int Rhombus::bBoxWidth() const {
return getDiagonal();
}
void Rhombus::draw(Canvas &canvas, int c, int r, char fg, char bg) const {
c += diagonal / 2; // fixes position, aligns leftmost char to c, instead of c as center.
for (int h = 0; h < diagonal; h++) // for each row from 0 to diagonal - 1
{
if (h <= diagonal / 2) // upper half increases
{
for (int w = 0; w <= h; w++) {
canvas.putChar(fg, c + w, r + h); // write to right side
canvas.putChar(fg, c - w, r + h); // write to left side
}
}
if (h > diagonal / 2) // lower half decreases
{
for (int w = diagonal - 1 - h; w >= 0; w--) {
canvas.putChar(fg, c + w, r + h);
canvas.putChar(fg, c - w, r + h);
}
}
}
}
ostream &operator<<(ostream &ostr, const Rhombus &rhs) {
const Shape *shapePtr = &rhs;
ostr << *shapePtr;
ostr.precision(2);
ostr << "\nB. box width: " << rhs.bBoxWidth()
<< "\nB. box height: " << rhs.bBoxHeight()
<< "\nScr area: " << rhs.scrArea()
<< "\nGeo area: " << fixed << rhs.geoArea()
<< "\nScr Perimeter: " << rhs.scrPerimeter()
<< "\nGeo Perimeter: " << rhs.geoPerimeter();
return ostr;
}