You are on page 1of 2

C:\Users\madar\Documents\Visual Studio 2010\Projects\prgm8\prgm8\prg8.

cpp 1
1 #include<GL/glut.h>
2 #include<stdio.h>
3 #include<math.h>
4 #define PI 3.1416
5 float theta = 0;
6 struct point
7 {
8 GLfloat x, y, z;
9 };
10 int factorial(int n)
11 {
12 if (n<=1)
13 return(1);
14 else
15 n=n*factorial(n-1);
16 return n;
17 }
18 void computeNcR(int n, int *hold_ncr_values)
19 {
20 int r;
21 for(r=0; r<=n; r++) //start from nC0, then nC1, nC2, nC3 till nCn
22 {
23 hold_ncr_values[r] = factorial(n) / (factorial(n-r) * factorial(r));
24 }
25 }
26 void computeBezierPoints(float t, point *actual_bezier_point, int number_of_control_points,
27 point *control_points_array, int *hold_ncr_values)
28 {
29 int i, n = number_of_control_points-1;
30 float bernstein_polynomial;
31 actual_bezier_point -> x = 0;
32 actual_bezier_point -> y = 0;
33 actual_bezier_point -> z = 0;
34 for(i=0; i < number_of_control_points; i++)
35 {
36 bernstein_polynomial = hold_ncr_values[i] * pow(t, i) * pow( 1-t, n-i);
37 actual_bezier_point -> x += bernstein_polynomial * control_points_array[i].x;
38 actual_bezier_point -> y += bernstein_polynomial * control_points_array[i].y;
39 actual_bezier_point -> z += bernstein_polynomial * control_points_array[i].z;
40 }
41 }
42 void bezier(point *control_points_array, int number_of_control_points, int
43 number_of_bezier_points)
44 {
45 point actual_bezier_point;
46 float t;
47 int *hold_ncr_values, i;
48 hold_ncr_values = new int[number_of_control_points]; // to hold the nCr values
49 computeNcR(number_of_control_points-1, hold_ncr_values); // call nCr function to calculate nCr values
50
51 glBegin(GL_LINE_STRIP);
52 for(i=0; i<=number_of_bezier_points; i++)
53 {
54 t=float(i)/float(number_of_bezier_points);
55 computeBezierPoints(t, &actual_bezier_point, number_of_control_points,
56 control_points_array, hold_ncr_values);
57 glVertex2f(actual_bezier_point.x, actual_bezier_point.y);
58 }
59 glEnd();
60 delete[] hold_ncr_values;
61 }
62 void display()
63 {
64 glClear(GL_COLOR_BUFFER_BIT);
65 int number_of_control_points = 4, number_of_bezier_points = 20;
66 point control_points_array[4] = {{100, 400, 0}, {150, 450, 0}, {250, 350, 0},{300, 400,
67 0}};
68 control_points_array[1].x += 50*sin(theta * PI/180.0);
69 control_points_array[1].y += 25*sin(theta * PI/180.0);
70 control_points_array[2].x -= 50*sin((theta+30) * PI/180.0);
71 control_points_array[2].y -= 50*sin((theta+30) * PI/180.0);
72 control_points_array[3].x -= 25*sin((theta-30) * PI/180.0);
73 control_points_array[3].y += sin((theta-30) * PI/180.0);
74 theta += 2;
C:\Users\madar\Documents\Visual Studio 2010\Projects\prgm8\prgm8\prg8.cpp 2
75 glPushMatrix();
76 glPointSize(5);
77 glColor3f(1, 0.4, 0.2); //Indian flag: Saffron color code
78 for(int i=0; i<50; i++)
79 {
80 glTranslatef(0, -0.8, 0 );
81 bezier(control_points_array, number_of_control_points, number_of_bezier_points);
82 }
83 glColor3f(1, 1, 1); //Indian flag: white color code
84 for(int i=0; i<50; i++)
85 {
86 glTranslatef(0, -0.8, 0);
87 bezier(control_points_array, number_of_control_points, number_of_bezier_points);
88 }
89 glColor3f(0, 1, 0); //Indian flag: green color code
90 for(int i=0; i<50; i++)
91 {
92 glTranslatef(0, -0.8, 0);
93 bezier(control_points_array, number_of_control_points, number_of_bezier_points);
94 }
95 glPopMatrix();
96 glLineWidth(5);
97 glColor3f(0.7, 0.5,0.3); //pole colour
98 glBegin(GL_LINES);
99 glVertex2f(100,400);
100 glVertex2f(100,40);
101 glEnd();
102 glutPostRedisplay();
103 glutSwapBuffers();
104 }
105 void init()
106 {
107 glMatrixMode(GL_PROJECTION);
108 glLoadIdentity();
109 gluOrtho2D(0,500,0,500);
110 }
111 int main(int argc, char **argv)
112 {
113 glutInit(&argc, argv);
114 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
115 glutInitWindowPosition(0, 0);
116 glutInitWindowSize(500,500);
117 glutCreateWindow("Bezier Curve - updated");
118 init();
119 glutDisplayFunc(display);
120 glutMainLoop();
121 }

You might also like