-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.cpp
More file actions
104 lines (86 loc) · 3.5 KB
/
triangle.cpp
File metadata and controls
104 lines (86 loc) · 3.5 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
#include "triangle.hpp"
#include <raylib.h>
#include <raymath.h>
#include <iostream>
#include <math.h>
Triangle::Triangle(int x, int y, float triangleSize, Color triangleColor, bool triangleShowCentroid)
{
originalPosition = {(float)x, (float)y};
position = {0, 0}; // set to original position at end of constructor
originalAngle = -PI / 2.f; // starting angle. points towards +x axis
rotatedAngle = 0.f; // set to original rotation at end of constructor
size = triangleSize;
color = triangleColor;
showCentroid = triangleShowCentroid;
angleFromHeightVector = PI / 4.f; // determins the pointy-ness of the triangle
float height = 2 * size * cosf(angleFromHeightVector); // constraining element of the triangle
basisOrientation = {
{(0.0f * size), (1.0f * height)}, // POINT A
{(sinf(angleFromHeightVector) * size), (-cosf(angleFromHeightVector) * size)}, // POINT B
{(sinf(angleFromHeightVector) * -size), (cosf(angleFromHeightVector) * -size)}, // POINT C
};
orientation = basisOrientation;
vertices = basisOrientation;
// because of how the triangle is constructed, the centroid is always equal to the position
centroid = {(1 / 3.f) * (vertices.a.x + vertices.b.x + vertices.c.x),
(1 / 3.f) * (vertices.a.y + vertices.b.y + vertices.c.y)};
triangleDir = Vector2Normalize(Vector2Subtract(vertices.a, centroid));
SetTrianglePosition(originalPosition);
SetTriangleRotation(originalAngle);
};
void Triangle::Draw()
{
DrawTriangle(vertices.a, vertices.b, vertices.c, color);
if (showCentroid) // for centroid
{
DrawCircleLines((int)centroid.x, (int)centroid.y, size / 20.f + 1.f, WHITE);
DrawCircle((int)centroid.x, (int)centroid.y, size / 20.f, BLACK);
}
};
void Triangle::SetTrianglePosition(Vector2 newPos)
{
position = newPos;
centroid = newPos;
vertices.a = Vector2Add(newPos, orientation.a);
vertices.b = Vector2Add(newPos, orientation.b);
vertices.c = Vector2Add(newPos, orientation.c);
// centroid = {(1 / 3.f) * (vertices.a.x + vertices.b.x + vertices.c.x),
// (1 / 3.f) * (vertices.a.y + vertices.b.y + vertices.c.y)};
triangleDir = Vector2Normalize(Vector2Subtract(vertices.a, centroid));
};
void Triangle::MoveTriangle(float x, float y)
{
SetTrianglePosition(Vector2{position.x + x, position.y + y});
};
void Triangle::ResetTrianglePosition()
{
SetTrianglePosition(originalPosition);
}
void Triangle::SetTriangleRotation(float angle)
{
rotatedAngle = angle;
auto rotateFromBasis = [&](Vector2 const &basisVector, Vector2 &vector)
{
vector.x = basisVector.x * cosf(angle) - basisVector.y * sinf(angle);
vector.y = basisVector.x * sinf(angle) + basisVector.y * cosf(angle);
};
rotateFromBasis(basisOrientation.a, orientation.a);
rotateFromBasis(basisOrientation.b, orientation.b);
rotateFromBasis(basisOrientation.c, orientation.c);
SetTrianglePosition(position);
};
void Triangle::RotateTriangle(float angle)
{
rotatedAngle += angle;
SetTriangleRotation(rotatedAngle);
};
void Triangle::ResetTriangleRotation()
{
SetTriangleRotation(originalAngle);
};
void Triangle::RotateToVector(Vector2 vector)
{
vector = Vector2Normalize(Vector2Subtract(vector, centroid));
RotateTriangle(atan2(triangleDir.x * vector.y - vector.x * triangleDir.y,
triangleDir.x * vector.x + triangleDir.y * vector.y));
};