-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog8.cpp
More file actions
137 lines (123 loc) · 2.59 KB
/
prog8.cpp
File metadata and controls
137 lines (123 loc) · 2.59 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
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
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#define PI 3.1416
struct point
{
GLfloat x, y;
};
float theta = 0;
point controlPts[4];
int numControlPts = 4, numBezierPts = 20, n = numControlPts - 1;
int *ncrValues = new int[numControlPts];
int factorial(int n)
{
if (n <= 1)
return (1);
else
n = n * factorial(n - 1);
return n;
}
void computenCr()
{
for (int r = 0; r <= n; r++)
ncrValues[r] = factorial(n) / (factorial(n - r) * factorial(r));
}
void computeBezierPoints(float t, point *bezierPt)
{
float bernsteinPolynomial;
bezierPt->x = 0;
bezierPt->y = 0;
for (int i = 0; i < numControlPts; i++)
{
bernsteinPolynomial = ncrValues[i] * pow(t, i) * pow(1 - t, n - i);
bezierPt->x += bernsteinPolynomial * controlPts[i].x;
bezierPt->y += bernsteinPolynomial * controlPts[i].y;
}
}
void bezier()
{
point bezierPt;
float t;
computenCr();
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= numBezierPts; i++)
{
t = float(i) / float(numBezierPts);
computeBezierPoints(t, &bezierPt);
glVertex2f(bezierPt.x, bezierPt.y);
}
glEnd();
}
void update()
{
float temp = 25 * sin(theta * PI / 180.0);
controlPts[1].x += temp;
controlPts[1].y += temp;
controlPts[2].x -= temp;
controlPts[2].y -= temp;
controlPts[3].x -= temp;
controlPts[3].y += temp;
theta += 0.2;
}
void drawFlagBlock(float r, float g, float b)
{
glColor3f(r, g, b);
for (int i = 0; i < 50; i++)
{
glTranslatef(0, -0.8, 0);
bezier();
}
}
void drawPole()
{
glLineWidth(5);
glColor3f(0.7, 0.5, 0.3);
glBegin(GL_LINES);
glVertex2f(100, 400);
glVertex2f(100, 40);
glEnd();
}
void drawFlag()
{
glPushMatrix();
glPointSize(5);
drawFlagBlock(1, 0.4, 0.2);
drawFlagBlock(1, 1, 1);
drawFlagBlock(0, 1, 0);
glPopMatrix();
}
void initControlPts()
{
controlPts[0] = {100, 400};
controlPts[1] = {150, 450};
controlPts[2] = {250, 350};
controlPts[3] = {300, 400};
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
initControlPts();
update();
drawFlag();
drawPole();
glutPostRedisplay();
glutSwapBuffers();
}
void init()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("Bezier Curve");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glutDisplayFunc(display);
glutMainLoop();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
init();
}