-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cpp
More file actions
76 lines (65 loc) · 2.98 KB
/
Copy pathTriangle.cpp
File metadata and controls
76 lines (65 loc) · 2.98 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
//
// Created by raphael on 9/5/18.
//
#include "Triangle.h"
#include "SlopeLine.h"
#include "PointLine.h"
#include "math.h"
namespace rf {
Triangle::Triangle(Vertex one, Vertex two, Vertex three, bool calcPointsInside) {
setPointCount(3);
setPoint(0, one);
setPoint(1, two);
setPoint(2, three);
updateValues(calcPointsInside);
}
void Triangle::updateValues(bool calcPointsInside) {
Vertex ptOne = getPoint(0);
Vertex ptTwo = getPoint(1);
Vertex ptThree = getPoint(2);
if (ptOne.x == ptTwo.x || ptTwo.x == ptTwo.x) {
ptOne = getPoint(2);
ptTwo = getPoint(0);
ptThree = getPoint(1);
}
PointLine one = PointLine(ptOne, ptTwo);
PointLine two = PointLine(ptTwo, ptThree);
circumcenter = one.perpendicularBisector().intersect(two.perpendicularBisector());
circumradius = circumcenter.dist(ptOne);
if(calcPointsInside)
this->calcPointsInside();
}
void Triangle::calcPointsInside() {
Vertex ptOne = getPoint(0);
Vertex ptTwo = getPoint(1);
Vertex ptThree = getPoint(2);
Vertex minBound = Vertex(fmin(fmin(ptOne.x, ptTwo.x), ptThree.x), fmin(fmin(ptOne.y, ptTwo.y), ptThree.y));
Vertex maxBound = Vertex(fmax(fmax(ptOne.x, ptTwo.x), ptThree.x), fmax(fmax(ptOne.y, ptTwo.y), ptThree.y));
for (int x = static_cast<int>(minBound.x); x <= maxBound.x; x++) {
for (int y = static_cast<int>(minBound.y); y <= maxBound.y; y++) {
auto p = new Vertex(x, y);
if (abs(area() - Triangle(*p, ptOne, ptTwo, false).area() - Triangle(*p, ptOne, ptThree, false).area() - Triangle(*p, ptTwo, ptThree, false).area()) < 0.1)
pointsInside.push_back(p);
else
delete p;
}
}
}
bool Triangle::operator==(const Triangle &other) const {
if (getPoint(0) == other.getPoint(0) && getPoint(1) == other.getPoint(1) && getPoint(2) == other.getPoint(2))
return true;
if (getPoint(0) == other.getPoint(0) && getPoint(1) == other.getPoint(2) && getPoint(2) == other.getPoint(1))
return true;
if (getPoint(0) == other.getPoint(1) && getPoint(1) == other.getPoint(2) && getPoint(2) == other.getPoint(0))
return true;
if (getPoint(0) == other.getPoint(1) && getPoint(1) == other.getPoint(0) && getPoint(2) == other.getPoint(2))
return true;
if (getPoint(0) == other.getPoint(2) && getPoint(1) == other.getPoint(1) && getPoint(2) == other.getPoint(0))
return true;
return getPoint(0) == other.getPoint(2) && getPoint(1) == other.getPoint(0) && getPoint(2) == other.getPoint(1);
}
float Triangle::area() {
return abs((getPoint(0).x * (getPoint(1).y - getPoint(2).y) + getPoint(1).x * (getPoint(2).y - getPoint(0).y) +
getPoint(2).x * (getPoint(0).y - getPoint(1).y)) / 2.0f);
}
}