-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.cpp
executable file
·105 lines (83 loc) · 2.01 KB
/
shapes.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
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
105
#include "shapes.h"
double distanceSqr(const point2d& p1, const point2d& p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return dx * dx + dy * dy;
}
void point2d::add(vector2d& v, double s) {
x += v.x * s;
y += v.y * s;
}
vector2d make_unitVector(double a) {
float x, y;
sincosf(a, &y, &x);
return vector2d(x, y);
}
struct point2d closestPointFromLineToPoint(
const point2d& start, const vector2d& ray, const point2d& p) {
struct point2d closest;
double c1 = -ray.x * p.x - ray.y * p.y;
double c2 = start.y * ray.x - start.x * ray.y;
closest.x = -(ray.y * c2 + ray.x * c1);
closest.y = (-ray.y * c1 + ray.x * c2);
return closest;
}
float point3f::distanceSqr(const point3f& p1, const point3f& p2) {
vector3f v = p1 - p2;
return v.lengthSqr();
}
void drawAxisPart(float x, float y, float z) {
glColor3f(x, y, z);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(x, y, z);
glEnd();
}
void drawAxis(float s) {
// GLboolean b = glIsEnabled(GL_LIGHTING);
// if (b) {
// glDisable(GL_LIGHTING);
// }
// glPushMatrix();
// {
// drawAxisPart(s, 0, 0);
// drawAxisPart(0, s, 0);
// drawAxisPart(0, 0, s);
// }
// glPopMatrix();
// if (b) {
// glEnable(GL_LIGHTING);
// }
}
float clampAngle(float angle) {
float t = fmodf(angle, M_PI * 2);
if (angle > 0) {
return t;
} else {
return M_PI * 2 + t;
}
}
void vector3f::rotateX(float angle) {
float s, c;
sincosf(angle, &s, &c);
float a = y * c - z * s;
float b = y * s + z * c;
y = a;
z = b;
}
void vector3f::rotateY(float angle) {
float s, c;
sincosf(angle, &s, &c);
float a = x * c + z * s;
float b = -x * s + z * c;
x = a;
z = b;
}
void vector3f::rotateZ(float angle) {
float s, c;
sincosf(angle, &s, &c);
float a = x * c - y * s;
float b = x * s + y * c;
x = a;
y = b;
}