0% found this document useful (0 votes)
275 views2 pages

Pgm2 - Basic Geometric Operations On The 2D Object

The document describes a program to demonstrate basic geometric operations on 2D objects by subdividing an initial triangle recursively. It includes functions to display a triangle, subdivide a triangle by calculating midpoints of its sides, and initialize and run the graphics display.

Uploaded by

pogonop994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
275 views2 pages

Pgm2 - Basic Geometric Operations On The 2D Object

The document describes a program to demonstrate basic geometric operations on 2D objects by subdividing an initial triangle recursively. It includes functions to display a triangle, subdivide a triangle by calculating midpoints of its sides, and initialize and run the graphics display.

Uploaded by

pogonop994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Develop a program to demonstrate basic geometric operations on the 2D object

#include <stdio.h>
#include <GL/glut.h>
typedef float point2[2];

/* initial triangle */

point2 v[]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};


int n;

/* display one triangle */


void triangle( point2 a, point2 b, point2 c)
{
glBegin(GL_TRIANGLES);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}

void divide_triangle(point2 a, point2 b, point2 c, int m)


{

/* triangle subdivision using vertex numbers */

point2 v0, v1, v2;


int j;
if(m>0)
{
for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
divide_triangle(a, v0, v1, m-1);
divide_triangle(c, v1, v2, m-1);
divide_triangle(b, v2, v0, m-1);
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
divide_triangle(v[0], v[1], v[2], n);
glFlush();
}
void myinit()
{

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (1.0, 1.0, 1.0, 1.0);
glColor3f(0.0,0.0,0.0);
}

void main(int argc, char **argv)


{
printf(" No. of Subdivisions : ");
scanf("%d",&n);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutCreateWindow("Sierpinski Gasket 2D triangle");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

You might also like