0% found this document useful (0 votes)
147 views9 pages

Assignment: Course Code: 422 Course Title: Computer Graphics Lab

The document contains code for drawing a half moon and an emoji using OpenGL graphics functions. The half moon code uses a for loop and trigonometric functions to draw two circles representing the moon. The emoji code draws colored circles of different sizes in different positions to create a smiling face emoji using translations and a circle drawing function. It initializes the display, sets colors and drawing modes, and contains a main loop to continuously redraw the graphics.

Uploaded by

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

Assignment: Course Code: 422 Course Title: Computer Graphics Lab

The document contains code for drawing a half moon and an emoji using OpenGL graphics functions. The half moon code uses a for loop and trigonometric functions to draw two circles representing the moon. The emoji code draws colored circles of different sizes in different positions to create a smiling face emoji using translations and a circle drawing function. It initializes the display, sets colors and drawing modes, and contains a main loop to continuously redraw the graphics.

Uploaded by

Tayaba Rahaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment

Course Code: 422


Course Title: Computer
Graphics Lab

Submitted To:
Name: Mst. Israt Jahan
Lecture
Department of
CSE
Daffodil International University

Submitted By:
Name: Monjur Hasan Milton

ID:171-15-9220
Section: O-7
Moon Code:
#include <GL/glut.h>
#include <math.h>

void init(void) {
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 550, 0, 450);
}

void display() {
float theta;
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
for (i = 0; i < 360; i++) {
theta = i * 3.142 / 180;
glVertex2f(275 + 200 * cos(theta), 225 + 200 * sin(theta));
}
glEnd();

glColor3f(0, 0, 0);
glBegin(GL_POLYGON);
for (i = 0; i < 360; i++) {
theta = i * 3.142 / 180;
glVertex2f(350 + 200 * cos(theta), 300 + 200 * sin(theta));
}
glEnd();

glFlush();
}

int main(int argc, char* argv[]) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(550, 450);
glutCreateWindow("Half Moon");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Emoji Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
static float tx = 0.0;
static float ty = 0.0;

void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glOrtho(-15,15,-15,15,-15,5);
}
void circle(GLfloat rx,GLfloat ry,GLfloat cx,GLfloat
cy)//radius_x,radius_y,certre_position_x,centre_position_y
{

glBegin(GL_TRIANGLE_FAN);
glVertex2f(cx,cy);

for(int i=0;i<=100;i++)

{
float angle = 2 * 3.1416f * i/100;

float x = rx * cosf(angle);
float y = ry * sinf(angle);
glVertex2f((x+cx),(y+cy));
}
glEnd();
}

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 1.0f, 0.0f);


glPushMatrix();
glTranslatef(2,8,0);
circle(5,5,0,0);
glPopMatrix();

glColor3f(1.0f, 1.0f, 1.0f);


glPushMatrix();
glTranslatef(0,9,0);
circle(1.8,1.7,0,0);
glPopMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(0,8.8,0);
circle(1,1,0,0);
glPopMatrix();

glColor3f(1.0f, 1.0f, 1.0f);


glPushMatrix();
glTranslatef(4,9,0);
circle(1.8,1.7,0,0);
glPopMatrix();

glColor3f(0.0f, 0.0f, 0.0f);


glPushMatrix();
glTranslatef(4,8.8,0);
circle(1,1,0,0);
glPopMatrix();

glColor3f(1.0f, 0.0f, 0.0f);


glPushMatrix();
glTranslatef(1.8,5,0);
circle(1,1,0,0);
glPopMatrix();
glFlush();
}
int main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(200, 200);
glutCreateWindow("Emoji drawing");
init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}

You might also like