-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygon.cpp
More file actions
71 lines (61 loc) · 1.64 KB
/
Polygon.cpp
File metadata and controls
71 lines (61 loc) · 1.64 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
#include "Polygon.h"
#include <cmath>
//using namespace std;
Polygon::Polygon(int id, ShapeType type)
:Shape(id, type)
{
}
void Polygon::draw(QPainter *shape)
{
QVector<QPoint> points;
for(int x = 0; x < int(getDimensions().size()); x+=2)
{
points.push_back(QPoint(getDimensions()[x], getDimensions()[x+1]));
}
shape->save(); //save original state of qpainter
shape->setPen(getPen());
shape->setBrush(getBrush());
shape->drawPolygon(points);
int xVal = points[0].x();
int yVal = points[0].y();
for(int x = 0; x < points.size(); x++)
{
if(points[x].x() < xVal)
{
xVal = points[x].x();
}
if(points[x].y() > yVal)
{
yVal = points[x].y();
}
}
yVal += 20;
shape->drawText(xVal, yVal, QString::fromStdString("Shape Id: ") + QString::number(getId()));
shape->restore(); //restore to the original state of qpainter
}
void Polygon::move(int dims_[])
{
int elementNum = getDimensions().size();
for(int i = 0; i < elementNum; i++)
{
getDimensions()[i] = dims_[i];
}
}
int Polygon::perimeter()
{
int perimeter = 0;
int elementNum = getDimensions().size();
for(int i = 0; i < elementNum-2; i+=2)
{
perimeter += (int)(sqrt(pow(getDimensions()[i+2] - getDimensions()[i], 2) + pow(getDimensions()[i+3] - getDimensions()[i+1], 2)) );
}
return perimeter;
}
int Polygon::area()
{
int numSides = (getDimensions().size()/2);
double theta = 360/2*numSides;
double apothem = (perimeter()/numSides/tan(theta));
int area = (int)(0.5 * apothem * perimeter());
return area;
}