-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle.cpp
51 lines (43 loc) · 883 Bytes
/
triangle.cpp
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
#include "triangle.h"
#include "vector3.h"
Triangle::Triangle()
: vertexA(Vector3()), vertexB(Vector3()), vertexC(Vector3())
{}
Triangle::Triangle(Vector3 a, Vector3 b, Vector3 c)
: vertexA(a), vertexB(b), vertexC(c)
{
init();
}
void Triangle::init()
{
edgeA = vertexB - vertexA;
edgeB = vertexC - vertexA;
calculateNormal();
calculateCenter();
calculateRadius();
}
void Triangle::calculateNormal()
{
normal = edgeA.cross(edgeB).normal();
}
void Triangle::calculateCenter()
{
center = vertexA + vertexB + vertexC;
center.x /= 3;
center.y /= 3;
center.z /= 3;
}
void Triangle::calculateRadius()
{
double v1 = (vertexA - center).magnitude();
double v2 = (vertexB - center).magnitude();
double v3 = (vertexC - center).magnitude();
if(v1 > v2)
{
radius = v1 > v3 ? v1 : v3;
}
else
{
radius = v2 > v3 ? v2 : v3;
}
}