World's most popular travel blog for travel bloggers.

# Write a program in C/C++ using OpenGL to draw a triangle of blue colour and inside that draw a circle of orange colour.

, ,
#include <Windows.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void drawPoints(void)
{
double x1 = 100, x2 = 500, x3 = 300, y1 = 100, y2 = 100, y3 = 398;

int midX = (x1 + x2 + x3) / 3;
int midY = (y1 + y2 + y3) / 3;

/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);

glColor3ub(255, 255, 0);
/* Plot the points */
glBegin(GL_LINES);

glLineWidth(2.5);
/* Plot the first point */
glVertex3f(x1, y1, 0);
glVertex3f(x2, y2, 0);

glBegin(GL_LINES);

glLineWidth(2.5);
glVertex3f(x3, y3, 0);
glVertex3f(x1, y1, 0);

glBegin(GL_LINES);

glLineWidth(2.5);
glVertex3f(x2, y2, 0);
glVertex3f(x3, y3, 0);

glColor3ub(255, 0, 0);
double twicePi = 2.0 * 3.142;
int x = midX, y = midY, i, radius = 95;
glBegin(GL_LINES); //BEGIN CIRCLE
glVertex2f(midX, midY); // center of circle
for (i = 0; i <= 1000; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 200))), (y + (radius * sin(i * twicePi / 200)))
);
}

glEnd();

glFlush();
}

void circle()
{

double x1 = 100, x2 = 500, x3 = 300, y1 = 100, y2 = 100, y3 = 398;

double midX = (x1 + x2 + x3) / 3;
double midY = (y1 + y2 + y3) / 3;

/* Clears buffers to preset values */
//glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POINTS);
//glPointSize(4.5);
glVertex2d(midX, midY);

glEnd();

glFlush();
}
void Init()
{
/* Set clear color to white */
glClearColor(0.0, 0.0, 0.0, 0);
/* Set fill color to black */
glColor3f(1.0, 1.0, 0.0);
/* glViewport(0 , 0 , 640 , 480); */
/* glMatrixMode(GL_PROJECTION); */
/* glLoadIdentity(); */
gluOrtho2D(0, 640, 0, 480);
}
void main(int argc, char **argv)
{

glutInit(&argc, argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0, 0);
glutInitWindowSize(640, 480);
/* Create the window with title "DDA_Line" */
glutCreateWindow("Triangle");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
glutDisplayFunc(drawPoints);
//glutDisplayFunc(circle);
/* Keep displaying untill the program is closed */
glutMainLoop();
}

#### Post a Comment

Let us know your responses and feedback