Indian Flag #Include #Include #Include #Define Typedef Struct

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

8.

Indian Flag
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1416
typedef struct point
{
GLfloat x, y, z;
};
void bino(int n, int *C)
{
int 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 computeBezPt(float u, point *pt1, int cPt, point *pt2, int *C)
{
int k, n=cPt-1;
float bFcn;
pt1 ->x =pt1 ->y = pt1->z=0.0;
for(k=0; k< cPt; k++)
{
bFcn = C[k] * pow(u, k) * pow( 1-u, n-k);
pt1 ->x += pt2[k].x * bFcn;
pt1 ->y += pt2[k].y * bFcn;
pt1 ->z += pt2[k].z * bFcn;
}
}
void bezier(point *pt1, int cPt, int bPt)
{
point bcPt;
float u;
int *C, k;
C= new int[cPt];
bino(cPt-1, C);
glBegin(GL_LINE_STRIP);
for(k=0; k<=bPt; k++)
{
u=float(k)/float(bPt);
computeBezPt(u, &bcPt, cPt, pt1, C);
glVertex2f(bcPt.x, bcPt.y);
}
glEnd();
delete[]C;
}
float theta = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int nCtrlPts = 4, nBCPts =20;
point ctrlPts[4] = {{100, 400, 0}, {150, 450, 0}, {250, 350, 0},
{300, 400, 0}};
ctrlPts[1].x +=50*sin(theta * PI/180.0);
ctrlPts[1].y +=25*sin(theta * PI/180.0);
ctrlPts[2].x -= 50*sin((theta+30) * PI/180.0);
ctrlPts[2].y -= 50*sin((theta+30) * PI/180.0);
ctrlPts[3].x -= 25*sin((theta) * PI/180.0);
ctrlPts[3].y += sin((theta-30) * PI/180.0);
theta+=0.2;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5);
glPushMatrix();
glLineWidth(5);
glColor3f(1, 0.4, 0.2); //Indian flag: Orange color code
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(1, 1, 1); //Indian flag: white color code
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(0, 1, 0); //Indian flag: green color code
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(100,400);
glVertex2f(100,40);
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500,500);
glutCreateWindow("Bezier Curve");
init();
glutDisplayFunc(display);
glutMainLoop();
}

/* This C++ code is an implementation of a program that draws a Bezier


curve using OpenGL and GLUT library. Let's go through the code step by step
to understand its functionality.

#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1416
typedef struct point
{
GLfloat x, y, z;
};
This section includes necessary header files for using OpenGL and some
standard C libraries. It also defines a constant PI and declares a
structure named point to represent a 3D point with x, y, and z coordinates.

void bino(int n, int *C)


{
int 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;
}
}
This function, bino, calculates binomial coefficients and stores them in an
array C. It uses the mathematical formula to calculate binomial
coefficients, which are required for computing the Bezier curve.

c
Copy code
void computeBezPt(float u, point *pt1, int cPt, point *pt2, int *C)
{
int k, n=cPt-1;
float bFcn;
pt1->x = pt1->y = pt1->z = 0.0;
for(k=0; k<cPt; k++)
{
bFcn = C[k] * pow(u, k) * pow(1-u, n-k);
pt1->x += pt2[k].x * bFcn;
pt1->y += pt2[k].y * bFcn;
pt1->z += pt2[k].z * bFcn;
}
}
This function, computeBezPt, computes a point on the Bezier curve for a
given parameter u. It takes the parameter u, an output point pt1, the
number of control points cPt, an array of control points pt2, and the
precomputed binomial coefficients C. It calculates the x, y, and z
coordinates of the point on the Bezier curve using the Bernstein polynomial
formula.

void bezier(point *pt1, int cPt, int bPt)


{
point bcPt;
float u;
int *C, k;
C = new int[cPt];
bino(cPt-1, C);
glBegin(GL_LINE_STRIP);
for(k=0; k<=bPt; k++)
{
u = float(k) / float(bPt);
computeBezPt(u, &bcPt, cPt, pt1, C);
glVertex2f(bcPt.x, bcPt.y);
}
glEnd();
delete[] C;
}
This function, bezier, draws the Bezier curve. It takes the control points
pt1, the number of control points cPt, and the number of points on the
curve bPt. Inside the function, it creates a temporary point bcPt to hold
the computed point on the curve. It then loops from 0 to bPt and calls the
computeBezPt function to calculate a point on the curve for each parameter
value u. The calculated point is then added to the OpenGL line strip using
glVertex2f. Finally, the function cleans up the dynamically allocated
memory for the binomial coefficients array.

float theta = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int nCtrlPts = 4, nBCPts = 20;
point ctrlPts[4] = {
{100, 400, 0},
{150, 450, 0},
{250, 350, 0},
{300, 400, 0}
};
// Adjust control points based on theta value
ctrlPts[1].x += 50*sin(theta * PI/180.0);
ctrlPts[1].y += 25*sin(theta * PI/180.0);
ctrlPts[2].x -= 50*sin((theta+30) * PI/180.0);
ctrlPts[2].y -= 50*sin((theta+30) * PI/180.0);
ctrlPts[3].x -= 25*sin((theta) * PI/180.0);
ctrlPts[3].y += sin((theta-30) * PI/180.0);
theta += 0.2;

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5);
glPushMatrix();
glLineWidth(5);
glColor3f(1, 0.4, 0.2); // Indian flag: Orange color code
for(int i=0; i<50; i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(1, 1, 1); // Indian flag: white color code
for(int i=0; i<50; i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(0, 1, 0); // Indian flag: green color code
for(int i=0; i<50; i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(100,400);
glVertex2f(100,40);
glEnd();

glutPostRedisplay();
glutSwapBuffers();
}
The display function is the main rendering function that is called by GLUT
to draw the scene. It begins by clearing the color buffer and initializes
variables. It sets up the control points for the Bezier curve in the
ctrlPts array. The control points are modified using trigonometric
functions based on the theta value to create an animation effect.

The code then proceeds to draw the Indian flag. It sets the color, line
width, and draws the Bezier curve multiple times to create the desired
effect. Three colors representing the Indian flag (orange, white, and
green) are used. The glTranslatef function is used to shift the position of
subsequent curves vertically.

After drawing the flag, a vertical line is drawn using glBegin(GL_LINES)


and glVertex2f to complete the flagpole.

Finally, the function calls glutPostRedisplay to mark the current window as


needing to be redisplayed and glutSwapBuffers to swap the front and back
buffers to display the scene.

void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
}
The init function sets up the initial projection matrix for the scene. It
uses glMatrixMode and glLoadIdentity to switch to the projection matrix
mode and load the identity matrix. Then, gluOrtho2D is used to define an
orthographic projection with the specified range for the x and y
coordinates.

void main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("Bezier Curve");
init();
glutDisplayFunc(display);
glutMainLoop();
}
The main function is the entry point of the program. It initializes GLUT
using glutInit, specifies the display mode, window position, and size using
the provided parameters. Then, it creates a window with the given title
using glutCreateWindow.
After that, it calls the init function to perform initialization tasks.
Then, it sets the display function as the callback for rendering the scene
using glutDisplayFunc.

Finally, it enters the GLUT event processing loop using glutMainLoop, which
keeps the program running and handles various events such as keyboard
input, mouse events, and screen refresh.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy