-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshapes.cpp
171 lines (140 loc) · 4.55 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// implementation of the class shapes
#include "shapes.h"
#include <math.h>
// vertices = {x1,y1,x2,y2,x3,y3};
int BasicShapes::DrawTriangle(GLfloat *vertices)
{
// glColor3f(1,1,0);
// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
// draw a triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);
return 1; // successful
}
// vertices = {x1,y1,x2,y2,x3,y3,x4,y4};
int BasicShapes::DrawQuad(GLfloat *vertices)
{
// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
// draw a polygon
glDrawArrays(GL_QUADS, 0, 4);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);
return 1; // sucessful
}
int BasicShapes::DrawQuadBorder(GLfloat *vertices)
{
// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
// draw a polygon
glDrawArrays(GL_LINE_LOOP, 0, 4);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);
return 1; // sucessful
}
// cx, cy = center x, y coordinates
// r = radius
// num_segments = number of segments it form to complete the circle (more segment better circle)
int BasicShapes::DrawCircle(float cx, float cy, float r, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
return 1;
}
// cx, cy = center x, y coordinates
// r = radius
// num_segments = number of segments it form to complete the circle (more segment better circle)
int BasicShapes::DrawCircleFilled(float cx, float cy, float r, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_POLYGON);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
return 1;
}
// cx, cy = center x, y coordinates
// r = radius
// start_angle = starting angle
// arc_angle = total angle for the arc
// num_segments = number of segments it form to complete the circle (more segment better circle)
int BasicShapes::DrawArc(float cx, float cy, float r, float start_angle, float arc_angle, int num_segments)
{
float theta = arc_angle / float(num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open
float tangetial_factor = tanf(theta);
float radial_factor = cosf(theta);
float x = r * cosf(start_angle); //we now start at the start angle
float y = r * sinf(start_angle);
glBegin(GL_LINE_STRIP); //since the arc is not a closed curve, this is a strip now
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);
float tx = -y;
float ty = x;
x += tx * tangetial_factor;
y += ty * tangetial_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
return 1;
}
// cx, cy = center x, y coordinates
// r = radius
// start_angle = starting angle
// arc_angle = total angle for the arc
// num_segments = number of segments it form to complete the circle (more segment better circle)
int BasicShapes::DrawArcFilled(float cx, float cy, float r, float start_angle, float arc_angle, int num_segments)
{
float theta = arc_angle / float(num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open
float tangetial_factor = tanf(theta);
float radial_factor = cosf(theta);
float x = r * cosf(start_angle); //we now start at the start angle
float y = r * sinf(start_angle);
glBegin(GL_POLYGON);
glVertex2f(cx, cy);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);
float tx = -y;
float ty = x;
x += tx * tangetial_factor;
y += ty * tangetial_factor;
x *= radial_factor;
y *= radial_factor;
}
glVertex2f(cx, cy);
glEnd();
return 1;
}