Tuesday, October 27, 2020

Draw a circle inside a triangle using OpenGJ




// C program to demonstrate

// drawing a circle using

// OpenGL

#include<stdio.h>

#include<GL/glut.h>

#include<math.h>

#define pi 3.142857


// function to initialize

void myInit (void)

{

// making background color black as first

// 3 arguments all are 0.4

glClearColor(0.4, 0.4, 0.4, 1.0);


// making picture color green (in RGB mode), as middle argument is 1.0

//glColor3f(0.0, 1.0, 0.0);


// breadth of picture boundary is 1 pixel

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();


// setting window dimension in X- and Y- direction

gluOrtho2D(0.0,800.0, 0.0,600.0);

}


void display (void)

{

glClear(GL_COLOR_BUFFER_BIT);


glBegin(GL_LINES);

//set color to draw the triangle

glColor3f(1.0,0.5,0.0);

//first horizontal side of the triangle

glVertex2i(100,50);

glVertex2i(700,50);

//left side of the triangle

glVertex2i(100,50);

glVertex2i(400,500);

//third vertex

glVertex2i(400,500);

glVertex2i(700,50);


glEnd();


glBegin(GL_POINTS);

glColor3f(0.0, 1.0, 0.0);

float x, y, i;


// iterate y up to 2*pi, i.e., 360 degree

// with small increment in angle as

// glVertex2i just draws a point on specified co-ordinate

for ( i = 0; i < (2 * pi); i += 0.001)

{

// let 200 is radius of circle and as,

// circle is defined as x=r*cos(i) and y=r*sin(i)

x = 400+ 100 * cos(i);

y = 200+ 100 * sin(i);


glVertex2i(x, y);

}

glEnd();


glFlush();

}


int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);


// giving window size in X- and Y- direction

glutInitWindowSize(800,600);

glutInitWindowPosition(0, 0);


// Giving name to window

glutCreateWindow("Drawing Green Circle inside Orange Triangle");

myInit();


glutDisplayFunc(display);

glutMainLoop();

}



Output:



 

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows