Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views48 pages

Copy of Lab Report CSE-802

The document is a lab report from Sylhet Engineering College's Computer Science & Engineering department, detailing various experiments in computer graphics. It includes objectives, source codes, and results for experiments such as drawing a star shape, translation, rotation, reflection, and line drawing algorithms. The report is structured with a table of contents and is submitted by a student to their lecturer.

Uploaded by

saikattalukdar98
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)
5 views48 pages

Copy of Lab Report CSE-802

The document is a lab report from Sylhet Engineering College's Computer Science & Engineering department, detailing various experiments in computer graphics. It includes objectives, source codes, and results for experiments such as drawing a star shape, translation, rotation, reflection, and line drawing algorithms. The report is structured with a table of contents and is submitted by a student to their lecturer.

Uploaded by

saikattalukdar98
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/ 48

SYLHET ENGINEERING COLLEGE

Department of Computer Science &


Engineering

LAB REPORT

Course No: CSE-802


Course Title: Computer Graphics(Sessional)
Report No: 01
Topics:
Date of Submission: 25-01-2024
Remarks:

Submitted By Submitted To

Name: Geerbani Pal Shashi Md. Lysuzzaman


Registration No: 2018331502 Lecturer
Session: 2018-19 Department of CSE
Sylhet Engineering College
1

Table of Contents

Experiment Experiment Name Page No


No

1 Draw the shape of a Star

2
Translation of Star Shape by taking user input of Tx & Ty

3 Rotations of Star Shape by taking user input of angle

4 Reflection of Star Shape

5
DDA Line Drawing Algorithm by taking user input

6
Bresenham Line Drawing Algorithm

7
Cohen Sutherland Line Clipping Algorithm

8
Sutherland Hodgman Polygon Clipping Algorithm

9 Weiler-Atherton Clipping Algorithm

10 Bresenham Circle Drawing Algorithm

11 Midpoint Circle Drawing Algorithm


2

Experiment No: 01
Experiment Name: Star Shape Drawing.
Objectives:
❖ To write an OpenGL programme that draws a star form.
❖ To get experience with the fundamental OpenGL functions and creating primitives.
❖ To comprehend the idea of giving vertices in a 2D space their coordinates.

Code:

#include <GL/gl.h>
#include <GL/glut.h>

void display(void)
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);

/* draw white polygon with corners at vertices */


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

glBegin(GL_POLYGON);

// Define the vertices of the polygon


glVertex3f(0.15f, 0.15f, 0.0f);
glVertex3f(0.0f, 0.45f, 0.0f);
glVertex3f(-0.15f, 0.15f, 0.0f);
glVertex3f(-0.45f, 0.0f, 0.0f);
glVertex3f(-0.15f, -0.15f, 0.0f);
glVertex3f(0.0f, -0.45f, 0.0f);
glVertex3f(0.15f, -0.15f, 0.0f);
glVertex3f(0.45f, 0.0f, 0.0f);

glEnd();

/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush();
}
3

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Star");
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}

Output:

Result and Discussion:


4

Experiment No: 02
Experiment Name: Translation of Star Shape by taking user input of Tx & Ty Source Code
Objectives:

Code:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;

float Tx = 0.0;
float Ty = 0.0;

void translation(void)
{

glBegin(GL_POLYGON);
glVertex2f(2,2);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(5,0);
glVertex2f(2,2);
glVertex2f(2,-2);
glEnd();
5

glBegin(GL_POLYGON);
glVertex2f(0,5);
glVertex2f(-2,2);
glVertex2f(2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-5,0);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0,-5);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glEnd();

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.22,0.99,0.11);
glLoadIdentity();
glTranslatef(Tx,Ty,0);
translation();
glFlush();

void reshape(int w, int h)


{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15,15,-15,15,-15,15);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
6

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);


glutInitWindowSize (640, 480);
glutInitWindowPosition (200, 200);
cout<<"Enter translation values for Tx and Ty: ";
cin>>Tx>>Ty;
glutCreateWindow ("Star translation 2018331502");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Output:

Result and Discussion:

Experiment No: 03
7

Experiment Name: Rotations of Star Shape by taking user input of angle source code
Objectives:

Code:

#include <windows.h>
#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed


void handleKeypress(unsigned char key, int x, int y)
{
switch (key)
{
case 27: //Escape key
exit(0);
}
}

//Initializes 3D rendering
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enable color
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Change the background to sky blue
}

//Called when the window is resized


void handleResize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
8

float _angle;
float _cameraAngle = 0.0f;

//Draws the 3D scene


void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -5.0f);

glPushMatrix();
glTranslatef(0.0f, -1.0f, 0.0f);
glRotatef(_angle, 0.0f, 0.0f, -1.0f);

glBegin(GL_POLYGON);
glVertex2f(0.2,0.2);
glVertex2f(0.2,-0.2);
glVertex2f(-0.2,-0.2);
glVertex2f(-0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.5,0.0);
glVertex2f(0.2,0.2);
glVertex2f(0.2,-0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.0,0.5);
glVertex2f(-0.2,0.2);
glVertex2f(0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-0.5,0.0);
glVertex2f(-0.2,-0.2);
glVertex2f(-0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.0,-0.5);
glVertex2f(0.2,-0.2);
glVertex2f(-0.2,-0.2);
glEnd();
9

glPopMatrix();

glutSwapBuffers();
}

void update(int value)


{
_angle += 2.0f;
if (_angle > 360)
{
_angle -= 360;
}

glutPostRedisplay(); ////Tell GLUT that the scene has changed


glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv)


{
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);

cout<<"Enter the rotation angle of the star: ";


cin>>_angle;
//Create the window
glutCreateWindow("Star Rotations 2018331502");
initRendering();

//Set handler functions


glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutTimerFunc(25, update, 0); //Add a timer

glutMainLoop();
return 0;
}
10

Output:

Result and Discussion:

Experiment No: 04
Experiment Name: Reflection of Star Shape source code
Objectives:

Code:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;

void reflections() {
glBegin(GL_POLYGON);
glVertex2f(3, 6); // Top right
glVertex2f(5, 6); // Top left
glVertex2f(5, 4); // Bottom left
glVertex2f(3, 4); // Bottom right
glEnd();

glBegin(GL_POLYGON);
glVertex2f(4, 7.5);
glVertex2f(5, 6);
glVertex2f(3, 6);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(5, 6);
glVertex2f(6.5, 5);
glVertex2f(5, 4);
glEnd();
11

glBegin(GL_POLYGON);
glVertex2f(5, 4);
glVertex2f(4, 2.5);
glVertex2f(3, 4);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(3, 4);
glVertex2f(1.5, 5);
glVertex2f(3, 6);
glEnd();

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();

reflections();

// Reflections in all quadrants


glPushMatrix();
glScalef(-1.0, 1.0, 1.0); // Reflect horizontally
glColor3f(1.0, 0.0, 0.0); // Red color
reflections();
glPopMatrix();

glPushMatrix();
glScalef(1.0, -1.0, 1.0); // Reflect vertically
glColor3f(0.0, 1.0, 0.0); // Green color
reflections();
glPopMatrix();

glPushMatrix();
glScalef(-1.0, -1.0, 1.0); // Reflect both horizontally and vertically
glColor3f(0.0, 0.0, 1.0); // Blue color
reflections();
glPopMatrix();

glFlush();
}
12

void reshape(int w, int h) {


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -10, 10);
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow("Star Reflection 2018331502");

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();

return 0;
}

Output:

Result and Discussion:

Experiment No: 05
Experiment Name: DDA Line Drawing Algorithm by taking user input source code
Objectives:

Code:
#include<stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
13

float x1,y1,x2,y2,m,i,j;
float dx,dy;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);

glEnd();

glColor3f (0.0, 1.0, 0.0);


glBegin(GL_POINTS);

if(m>0 && m<=1)


{
while(x1<=x2 && y1<=y2)
{
x1=x1+1;
y1=y1+m;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);

}
}
else if(m>1)
{
while(x1<=x2 && y1<=y2)
{
x1=x1+(1/m);
y1=y1+1;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}

else if(m>-1 && m<=0)


{
14

while(x1>=x2 && y1>=y2)


{
x1=x1-1;
y1=y1-m;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}
else if(m<-1)

while(x1>=x2 && y1>=y2)


{
x1=x1-(1/m);
y1=y1-1;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}

glEnd();

glFlush ();
}
void init (void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
15

int main(int argc, char** argv)


{

printf("Enter value of X1 :");


scanf("%f",&x1);
printf("Enter value of y1 :");
scanf("%f",&y1);
printf("Enter value of X2 :");
scanf("%f",&x2);
printf("Enter value of Y2 :");
scanf("%f",&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("DDA Line Drawing Algo 2018331502");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

Result and Discussion:


16

Experiment No: 06
Experiment Name: Bresenham Line Drawing Algorithm Source Code
Objectives:

Code:

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
float x1,y1,x2,y2,m,i,j,p;
int dx=0,dy=0;
void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);

glEnd();

glColor3f (0.0, 1.0, 0.0);


glBegin(GL_POINTS);
p=(2*dy)-dx;
for(i=x1,j=y1; i<=x2,j<=y2; )
{
if(p>=0)
{
i=i+1;
j=j+1;
if((i>x2)||(j>y2))
{
break;
}
printf("%0.2f %0.2f\n",i,j);
17

glVertex3f ((i/100), (j/100), 0.0);


p=p+(2*dy)-(2*dx);
}
else if(p<0)
{
i=i+1;
if((i>x2)||(j>y2))
{
break;
}
printf("%0.2f %0.2f\n",i,j);
glVertex3f ((i/100), (j/100), 0.0);
p=p+(2*dy);
}
}
glEnd();

glFlush ();
}
void init (void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)


{

printf("Enter first point: ");


scanf("%f %f",&x1,&y1);
printf("Enter second point: ");
18

scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("bresenham line drawing algo 2018331502");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

Result and Discussion:

Experiment No: 07
Experiment Name: Cohen Sutherland Line Clipping Algorithm Source Code
Objectives:

Code:

#include<windows.h>
#include<GL/glu.h>
#include<GL/glut.h>

GLfloat xMin=-0.5,xMax=0.5,yMin=-0.5,yMax=0.5;
GLfloat x1=-0.8,y1=-0.6,x2=0.7,y2=0.4;

int Left=1,Right=2,Bot=4,Top=8;
int C1,C2;
int Clip_Flag = 0, Flag = 1;;
19

int Get_Code(GLfloat x,GLfloat y)


{
int Code = 0;
if(x<xMin)
Code = Code | Left;
if(x>xMax)
Code = Code | Right;
if(y<yMin)
Code = Code | Bot;
if(y>yMax)
Code = Code | Top;
return Code;
}

void Clip()
{
int C;
GLfloat x,y;
if(C1)
C = C1;
else
C = C2;

if(C & Left)


{
x = xMin;
y = y1+(y2-y1)*((xMin-x1)/(x2-x1));
}
if(C & Right)
{
x = xMax;
y = y1+(y2-y1)*((xMax-x1)/(x2-x1));
}
20

if(C & Bot)


{
y = yMin;
x = x1+(x2-x1)*((yMin-y1)/(y2-y1));
}
if(C & Top)
{
y = yMax;
x = x1+(x2-x1)*((yMax-y1)/(y2-y1));
}

if(C == C1)
{
x1 = x;
y1 = y;
}
else
{
x2 = x;
y2 = y;
}
}

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1,1,1);
glBegin(GL_LINE_LOOP);
glVertex2f(xMin,yMin);
glVertex2f(xMax,yMin);
glVertex2f(xMax,yMax);
glVertex2f(xMin,yMax);
glEnd();
21

glColor3f(1,0,0);
if(Flag == 1)
{
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}

while(1 & Clip_Flag == 1)


{
C1 = Get_Code(x1,y1);
C2 = Get_Code(x2,y2);

if((C1|C2) == 0)
break;
else if((C1&C2)!=0)
{
Flag = 0;
break;
}
else
Clip();
}
glFlush();
}

void Key(unsigned char ch,int x,int y)


{
Clip_Flag = 1;
glutPostRedisplay();
}

int main(int argC,char *argV[])


{
22

glutInit(&argC,argV);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Cohen-Sutherland Algorithm 2018331502");
glutDisplayFunc(Draw);
glutKeyboardFunc(Key);
glutMainLoop();
return 0;
}
Output:

Result and Discussion:

Experiment No: 8
Experiment Name: Sutherland Hodgman Polygon Clipping Algorithm
Objectives:

Code:

#include<stdio.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<math.h>

typedef struct
{
float x;
float y;
} PT;
23

int n;

int i,j;

PT p1,p2,p[20],pp[20];

void left() //left clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].x<p1.x && p[i+1].x>=p1.x) //Case-1: outside to inside
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y; // save point of
intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;

if(p[i].x>=p1.x && p[i+1].x>=p1.x) //Case-2: inside to inside


{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}

if(p[i].x>=p1.x && p[i+1].x<p1.x) // Case-3: inside to outside


{
if(p[i+1].x-p[i].x!=0)
{
24

pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y; // only save point of


intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
}
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void right() // right clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].x>p2.x && p[i+1].x<=p2.x) //Case-1: outside to inside
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y; // save point of
intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
25

pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].x<=p2.x && p[i+1].x<=p2.x) // Case-2: inside to inside


{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}

if(p[i].x<=p2.x && p[i+1].x>p2.x) // Case-3: inside to outside


{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y; // only save
point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;

void top() // top clipper


{
i=0;
j=0;
26

for(i=0; i<n; i++)


{
if(p[i].y>p2.y && p[i+1].y<=p2.y) //Case-1: outside to inside
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x; // save
point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y<=p2.y && p[i+1].y<=p2.y) // Case-2: inside to inside


{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}

if(p[i].y<=p2.y && p[i+1].y>p2.y) // Case-3: inside to outside


{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x; // only save
point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
}
}
27

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void bottom() // bottom clipper


{
i=0;
j=0;

for(i=0; i<n; i++)


{
if(p[i].y<p1.y && p[i+1].y>=p1.y) // Case-1: outside to inside
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x; // save
point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y>=p1.y && p[i+1].y>=p1.y) // Case-2: inside to inside


{
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
28

if(p[i].y>=p1.y && p[i+1].y<p1.y) // Case-3: inside to outside


{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x; // only save
point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
}
}

for(i=0; i<j; i++)


{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void drawpolygon()
{
glColor3f(1.0,0.0,0.0);
for(i=0; i<n-1; i++)
{
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[i+1].x,p[i+1].y);
glEnd();
}
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[0].x,p[0].y);
glEnd();
}

void myMouse(int button, int state, int x, int y)


{
29

if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)


{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_LOOP);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
left();
right();
top();
bottom();
drawpolygon();
}
glFlush();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.4,1.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
drawpolygon();
glFlush();
}

void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); // clear screen usually black
gluOrtho2D(0,500,0,500);
}

int main(int argc, char**argv)


{
printf("Enter Window Coordinates:\n");
printf("Please Enter two Points:\n");
30

printf("Enter P1(x,y): ");


scanf("%f", &p1.x);
scanf("%f", &p1.y);

printf("Enter P2(x,y): ");


scanf("%f", &p2.x);
scanf("%f", &p2.y);

printf("\nEnter the no. of vertices: ");


scanf("%d", &n);

for(i=0; i<n; i++)


{
printf("\nEnter V%d(x%d,y%d): ", i+1, i+1, i+1);
scanf("%f", &p[i].x);
scanf("%f", &p[i].y);
}

p[i].x=p[0].x;
p[i].y=p[0].y;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Sutherland Hodgman Polygon Clipping Algorithm 2018331502");
init();

glutDisplayFunc(display);
glutMouseFunc(myMouse);
glFlush();
glutMainLoop();
return 0;

Output:

Result and Discussion:


31

Experiment No: 09
Experiment Name: Weiler-Atherton Clipping Algorithm
Objectives:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

typedef struct
{
float x;
float y;
} Vertex;

Vertex cw[40], sp[40];


int n_cw, n_sp;

void draw_poly(Vertex vlist[], int n)


{
glColor3f(1.0, 1.0, 1.0); // Set color to white
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++)
{
glVertex2f(vlist[i].x, vlist[i].y);
}
glEnd();
}

int in_out(float x, float y, int x1, int y1, int x2, int y2)
{
float p = (y - y1) * (x2 - x1) - (x - x1) * (y2 - y1);
if (p < 0)
return 0; // for out
return 1; // for in
}
32

void intersection_lineseg(float *x, float *y, int x1, int y1, int x2, int y2, int xa, int ya, int xb, int yb)
{
*x = -1;
*y = -1;
if (x2 == x1 && xb == xa)
return;
else if (x2 == x1)
{
float m2 = (float)(yb - ya) / (xb - xa);
*x = x1;
*y = ya - m2 * (xa - x1);
}
else if (xb == xa)
{
float m1 = (float)(y2 - y1) / (x2 - x1);
*x = xa;
*y = y1 + m1 * (xa - x1);
}
else
{
float m1 = (float)(y2 - y1) / (x2 - x1);
float m2 = (float)(yb - ya) / (xb - xa);
if (m1 == m2)
return;
*x = (ya - y1 + m1 * x1 - m2 * xa) / (m1 - m2);
*y = (m1 * m2 * (xa - x1) + m2 * y1 - m1 * ya) / (m2 - m1);
}

if ((x1 >= x2 && (*x < x2 || *x > x1)) || (x2 >= x1 && (*x > x2 || *x < x1)) || (y1 >= y2 && (*y < y2
|| *y > y1)) || (y2 >= y1 && (*y > y2 || *y < y1)) || (xa >= xb && (*x < xb || *x > xa)) || (xb >= xa &&
(*x > xb || *x < xa)) || (ya >= yb && (*y < yb || *y > ya)) || (yb >= ya && (*y > yb || *y < ya)))
{
*x = -1;
*y = -1;
}
}

void wa_clip()
{
Vertex tempcw[40], tempsp[40];
int tag_sp[40], tag_cw[40], trav_sp[40], trav_cw[40];
float x, y;
int entry_list[10]; // saves indexes only
33

int e = -1;

// for new cw array


int kc = -1; // first vertex gets added last in the array
for (int i = 0; i < n_cw; i++)
{
Vertex tempi[20][2]; // for ordering intersection points, the 2nd column's x is for tag
int ti = -1;
for (int j = 0; j < n_sp; j++)
{
intersection_lineseg(&x, &y, cw[i].x, cw[i].y, cw[(i + 1) % n_cw].x, cw[(i + 1) % n_cw].y,
sp[j].x, sp[j].y, sp[(j + 1) % n_sp].x, sp[(j + 1) % n_sp].y);
if (x == -1) // or y == -1
continue;
ti++;
tempi[ti][0].x = x;
tempi[ti][0].y = y;
int p1 = in_out(sp[j].x, sp[j].y, cw[i].x, cw[i].y, cw[(i + 1) % n_cw].x, cw[(i + 1) % n_cw].y);
int p2 = in_out(sp[(j + 1) % n_sp].x, sp[(j + 1) % n_sp].y, cw[i].x, cw[i].y, cw[(i + 1) %
n_cw].x, cw[(i + 1) % n_cw].y);
if (p1 == 1 && p2 == 0)
tempi[ti][1].x = 1;
else
tempi[ti][1].x = 0;
}
if (ti != -1)
{
if (cw[(i + 1) % n_cw].x > cw[i].x) // sort intersection points
{
// increasing x sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x < tempi[min_idx][0].x)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
34

tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (cw[(i + 1) % n_cw].x < cw[i].x)
{
// decreasing x sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x > tempi[max_idx][0].x)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (cw[(i + 1) % n_cw].y > cw[i].y)
{
// increasing y sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y < tempi[min_idx][0].y)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
35

temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else
{
// decreasing y sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y > tempi[max_idx][0].y)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}

for (int k = 0; k <= ti; k++) // put sorted intersection points in cw array
{
kc++;
tempcw[kc].x = tempi[k][0].x;
tempcw[kc].y = tempi[k][0].y;
tag_cw[kc] = tempi[k][1].x;
trav_cw[kc] = 0;
}
}

kc++;
tempcw[kc].x = cw[(i + 1) % n_cw].x;
36

tempcw[kc].y = cw[(i + 1) % n_cw].y;


tag_cw[kc] = -1;
trav_cw[kc] = 0;
}

// for new sp array


int ks = -1; // first vertex gets added last in the array
for (int i = 0; i < n_sp; i++)
{
Vertex tempi[20][2]; // for ordering intersection points, the 2nd column's x is for tag
int ti = -1;
for (int j = 0; j < n_cw; j++)
{
intersection_lineseg(&x, &y, cw[j].x, cw[j].y, cw[(j + 1) % n_cw].x, cw[(j + 1) % n_cw].y,
sp[i].x, sp[i].y, sp[(i + 1) % n_sp].x, sp[(i + 1) % n_sp].y);
if (x == -1) // or y == -1
continue;
ti++;
tempi[ti][0].x = x;
tempi[ti][0].y = y;
int p1 = in_out(sp[i].x, sp[i].y, cw[j].x, cw[j].y, cw[(j + 1) % n_cw].x, cw[(j + 1) % n_cw].y);
int p2 = in_out(sp[(i + 1) % n_sp].x, sp[(i + 1) % n_sp].y, cw[j].x, cw[j].y, cw[(j + 1) %
n_cw].x, cw[(j + 1) % n_cw].y);
if (p1 == 1 && p2 == 0)
tempi[ti][1].x = 0;
else
tempi[ti][1].x = 1;
}
if (ti != -1)
{
if (sp[(i + 1) % n_sp].x > sp[i].x) // sort intersection points
{
// increasing x sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x < tempi[min_idx][0].x)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
37

tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (sp[(i + 1) % n_sp].x < sp[i].x)
{
// decreasing x sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x > tempi[max_idx][0].x)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (sp[(i + 1) % n_sp].y > sp[i].y)
{
// increasing y sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y < tempi[min_idx][0].y)
min_idx = m;
}
38

float temp = tempi[min_idx][0].x;


tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else
{
// decreasing y sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y > tempi[max_idx][0].y)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}

for (int k = 0; k <= ti; k++) // put sorted intersection points in sp array
{
ks++;
tempsp[ks].x = tempi[k][0].x;
tempsp[ks].y = tempi[k][0].y;
tag_sp[ks] = tempi[k][1].x;
trav_sp[ks] = 0;
}
}
39

ks++;
tempsp[ks].x = sp[(i + 1) % n_sp].x;
tempsp[ks].y = sp[(i + 1) % n_sp].y;
tag_sp[ks] = -1;
trav_sp[ks] = 0;
}

kc++;
tempcw[kc].x = cw[0].x;
tempcw[kc].y = cw[0].y;
tag_cw[kc] = -1;
trav_cw[kc] = 0;

ks++;
tempsp[ks].x = sp[0].x;
tempsp[ks].y = sp[0].y;
tag_sp[ks] = -1;
trav_sp[ks] = 0;

n_cw = kc + 1;
n_sp = ks + 1;

// Traversal
for (int i = 0; i <= e; i++)
{
int done = 0;
int j = entry_list[i];
while (!done)
{
if (trav_sp[j] == 1)
done = 1;
else if (tag_sp[j] == 1 || tag_sp[j] == -1)
{
glBegin(GL_LINES);
glVertex2f(tempsp[j].x, tempsp[j].y);
glVertex2f(tempsp[(j + 1) % n_sp].x, tempsp[(j + 1) % n_sp].y);
glEnd();
trav_sp[j] = 1;
j++;
}
else if (tag_sp[j] == 0)
{
trav_sp[j] = 1;
40

// Swap
int k;
for (k = 0; k < n_cw; k++) // find location to switch to
{
if (tempcw[k].x == tempsp[j].x && tempcw[k].y == tempsp[j].y)
{
j = k;
break;
}
}

// Swap functions for array swap


Vertex temp;
int temptag;
int travtemp;
for (int t = 0; t < n_cw; t++)
{
// Swap tempcw[t] and tempsp[t]
temp = tempcw[t];
tempcw[t] = tempsp[t];
tempsp[t] = temp;

// Swap tag_cw[t] and tag_sp[t]


temptag = tag_cw[t];
tag_cw[t] = tag_sp[t];
tag_sp[t] = temptag;

// Swap trav_cw[t] and trav_sp[t]


travtemp = trav_cw[t];
trav_cw[t] = trav_sp[t];
trav_sp[t] = travtemp;
}

int n = n_cw;
n_cw = n_sp;
n_sp = n;
}
}
}
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
41

glColor3f(1.0, 1.0, 1.0);

// Draw the clipping window


draw_poly(cw, n_cw);

// Draw the subject polygon


draw_poly(sp, n_sp);

// Perform the Weiler-Atherton clipping algorithm


wa_clip();

glFlush();
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Weiler-Atherton Clipping 2018331502");

// Accept user input for the clipping window and subject polygon
printf("Enter no. of vertices in the clipping window: ");
scanf("%d", &n_cw);
printf("Enter vertices (x, y) clockwise for the clipping window:\n");
for (int i = 0; i < n_cw; i++)
{
scanf("%f %f", &cw[i].x, &cw[i].y);
}

printf("Enter no. of vertices in the subject polygon: ");


scanf("%d", &n_sp);
printf("Enter vertices (x, y) clockwise for the subject polygon:\n");
for (int i = 0; i < n_sp; i++)
{
scanf("%f %f", &sp[i].x, &sp[i].y);
}

glutDisplayFunc(display);
glutMainLoop();

return 0;
}
42

Output:

Result and Discussion:

Experiment No: 10
Experiment Name: Bresenham Circle Drawing Algorithm Source Code

Objectives:

Code:

#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;

int radius,centerX,centerY;

void drawCircle(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY + x);
glVertex2i(centerX - y, centerY - x);
glEnd();
}

void bresenhamCircle() {
int x = 0;
int y = radius;
int decision = 3 - 2 * radius;

while (x <= y) {
drawCircle(x, y);
if (decision < 0) {
decision += 4 * x + 6;
43

x++;
} else {
decision += 4 * (x - y) + 10;
x++;
y--;
}
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
bresenhamCircle();
glFlush();
}

void reshape(int width, int height) {


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

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


cout<<"Enter the radius of circle : ";
cin>>radius;
cout<<"Enter the center point of the circle : ";
cin>>centerX>>centerY;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 480);
glutInitWindowPosition(0,0);
glutCreateWindow("Bresenham Circle Drawing 2018331502");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(0.0, 0.0, 0.0, 0.0);
glutMainLoop();
return 0;
}

Output:
44

Result and Discussion:

Experiment No: 11
Experiment Name: Mid point Circle Drawing Algorithm
Objectives:

Code:
45

#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;

int radius ,centerX , centerY;

void drawCircle(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY + x);
glVertex2i(centerX - y, centerY - x);
glEnd();
}

void midpointCircle()
{
int x = 0;
int y = radius;
int decision = 1 - radius;

while (x <= y)
{
drawCircle(x, y);
if (decision < 0)
{
decision += 2 * x + 3;
x++;
}
else
{
decision += 2 * (x - y) + 5;
x++;
y--;
}
}
}

void display()
46

{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
midpointCircle();
glFlush();
}

void reshape(int width, int height)


{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)


{
cout<<"Enter the radius of circle : ";
cin>>radius;
cout<<"Enter the center point of the circle : ";
cin>>centerX>>centerY;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0,0);
glutCreateWindow("Midpoint Circle Drawing Algo 2018331502");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(0.0, 0.0, 0.0, 0.0);
glutMainLoop();
return 0;
}

Output:
47

Result & Discussion:

You might also like