-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cpp
More file actions
76 lines (62 loc) · 2.01 KB
/
Triangle.cpp
File metadata and controls
76 lines (62 loc) · 2.01 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
#include "Triangle.h"
#include <iostream>
using namespace std;
Triangle::Triangle(int triangleHeight, int triangleBase, const std::string &gn, const string &desc) : Shape(gn, desc), height(triangleHeight), base(triangleBase) {
}
Triangle::~Triangle() = default;
const Triangle &Triangle::operator=(const Triangle &rhs) {
if (this == &rhs) return *this;
Shape::operator=(rhs);
height = rhs.height;
base = rhs.base;
return *this;
}
int Triangle::getHeight() const {
return height;
}
void Triangle::setHeight(int h) {
if (h > 0)
height = h;
else
cerr << "Error. Height has to be greater than zero." << endl;
}
int Triangle::getBase() const {
return base;
}
std::string Triangle::toString() const {
string temp{Shape::toString()};
temp.append("\nBound. box W: " + to_string(bBoxWidth()));
temp.append("\nBound. box H: " + 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;
}
double Triangle::geoArea() const {
return getHeight() * static_cast<double>(getBase()) / 2;
}
int Triangle::bBoxWidth() const {
return getBase();
}
int Triangle::bBoxHeight() const {
return getHeight();
}
void Triangle::setBase(int b) {
if (b > 0)
base = b;
else
cerr << "Error. Base has to be greater than zero." << endl;
}
std::ostream &operator<<(std::ostream &ostr, const Triangle &rhs) {
const Shape *shapePtr = &rhs;
ostr << *shapePtr;
ostr.precision(2);
ostr << "\nBound. box W: " << rhs.bBoxWidth()
<< "\nBound. box H: " << rhs.bBoxHeight()
<< "\nScr area: " << rhs.scrArea()
<< "\nGeo area: " << fixed << rhs.geoArea()
<< "\nScr Perimeter: " << rhs.scrPerimeter()
<< "\nGeo Perimeter: " << rhs.geoPerimeter();
return ostr;
}