-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path08.BezierCurve.cpp
More file actions
150 lines (133 loc) · 3.36 KB
/
Copy path08.BezierCurve.cpp
File metadata and controls
150 lines (133 loc) · 3.36 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
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
8. Develop a menu driven program to animate a flag using Bezier Curve algorithm
*/
#include<stdio.h>
#include<math.h>
#include<glut.h>
#define PI 3.1416
GLsizei winWidth = 600, winHeight = 600;
GLfloat xMinWC = 0.0, xMaxWC = 130.0;
GLfloat yMinWC = 0.0, yMaxWC = 130.0;
struct Point3d
{
GLfloat x, y, z;
};
void bino(GLint n, GLint C[]);
void computeBezierPoint(GLfloat u, Point3d *bezierPoint, GLint nControlPoints, Point3d controlPoints[], GLint C[]);
void bezier(Point3d controlPoints[], GLint nControlPoints, GLint nBezierCurvePoints);
void display();
void reshape(GLint w, GLint h);
void menu(int op);
void bino(GLint n, GLint C[])
{
GLint k, j;
for (k = 0; k <= n; k++)
{
C[k] = 1;
for (j = n; j >= k + 1; j--)
C[k] *= j;
for (j = n - k; j >= 2; j--)
C[k] /= j;
}
}
void computeBezierPoint(GLfloat u, Point3d *bezierPoint, GLint nControlPoints, Point3d controlPoints[], GLint C[])
{
GLint k, n = nControlPoints - 1;
GLfloat bezBlendFcn;
bezierPoint->x = bezierPoint->y = bezierPoint->z = 0.0;
for (k = 0; k < nControlPoints; k++)
{
bezBlendFcn = C[k] * pow(u, k) * pow(1 - u, n - k);
bezierPoint->x += controlPoints[k].x * bezBlendFcn;
bezierPoint->y += controlPoints[k].y * bezBlendFcn;
bezierPoint->z += controlPoints[k].z * bezBlendFcn;
}
}
void bezier(Point3d controlPoints[], GLint nControlPoints, GLint nBezierCurvePoints)
{
Point3d bezierCurvePoint;
GLfloat u;
GLint *C, k;
C = new GLint[nControlPoints];
bino(nControlPoints - 1, C);
glBegin(GL_LINE_STRIP);
for (k = 0; k <= nBezierCurvePoints; k++)
{
u = GLfloat(k) / GLfloat(nBezierCurvePoints);
computeBezierPoint(u, &bezierCurvePoint, nControlPoints, controlPoints, C);
glVertex2f(bezierCurvePoint.x, bezierCurvePoint.y);
}
glEnd();
delete[] C;
}
void display()
{
GLint nCtrlPts = 4, nBezCurvePts = 20;
static float theta = 0;
Point3d ctrlPts[4] = {
{ 20, 100, 0 },
{ 30, 110, 0 },
{ 50, 90, 0 },
{ 60, 100, 0 } };
ctrlPts[1].x += 10 * sin(theta * PI / 180.0);
ctrlPts[1].y += 5 * sin(theta * PI / 180.0);
ctrlPts[2].x -= 10 * sin((theta + 30) * PI / 180.0);
ctrlPts[2].y -= 10 * sin((theta + 30) * PI / 180.0);
ctrlPts[3].x -= 4 * sin((theta)* PI / 180.0);
ctrlPts[3].y += sin((theta - 30) * PI / 180.0);
theta += 0.1;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5);
glPushMatrix();
glLineWidth(5);
for (int i = 0; i < 24; i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glPopMatrix();
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(20, 100);
glVertex2f(20, 40);
glEnd();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void reshape(GLint w, GLint h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xMinWC, xMaxWC, yMinWC, yMaxWC);
glClear(GL_COLOR_BUFFER_BIT);
}
void menu(int op)
{
if (op == 1)
glColor3f(1.0, 0.0, 0.0);
else if (op == 2)
glColor3f(0.0, 1.0, 0.0);
else if (op == 3)
glColor3f(0.0, 0.0, 1.0);
else if (op == 4)
exit(0);
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Bezier Curve");
glutCreateMenu(menu);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("Blue", 3);
glutAddMenuEntry("Quit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}