0% found this document useful (0 votes)
32 views72 pages

Computer GraphicsAssignment

The document is a lab report from Sylhet Engineering College's Computer Science & Engineering department, detailing various experiments in computer graphics using OpenGL and GLUT. It covers drawing basic shapes, implementing line drawing algorithms (DDA and Bresenham's), and line clipping techniques (Cohen-Sutherland). Each experiment includes objectives, code snippets, results, and conclusions, providing a comprehensive overview of graphics programming fundamentals.

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)
32 views72 pages

Computer GraphicsAssignment

The document is a lab report from Sylhet Engineering College's Computer Science & Engineering department, detailing various experiments in computer graphics using OpenGL and GLUT. It covers drawing basic shapes, implementing line drawing algorithms (DDA and Bresenham's), and line clipping techniques (Cohen-Sutherland). Each experiment includes objectives, code snippets, results, and conclusions, providing a comprehensive overview of graphics programming fundamentals.

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

Sylhet Engineering College

Department of Computer Science & Engineering

LAB REPORT

Course Code : CSE-802


Course Title : Computer Graphics (Sessional)
Report No. : 01
Topic(s) : Drawing different shapes.

Submission Date : 25-01-2024

Submitted by Submitted to

Name: Effat Jahan Muna Md Lysuzzaman

Reg. No: 2018331548 Lecturer (CSE)

Session: 2018-19 Sylhet Engineering College.


Experiment No. 01
Experiment Name : Basic Shapes

The objectives of this code are to:


1. Demonstrate the basic usage of OpenGL and GLUT libraries for graphics
programming.
2. Create a simple graphical application that draws a yellow star shape on a
black background.
3. Illustrate the initialization of the OpenGL environment, setting up the
display mode, and handling the main rendering loop.
4. Introduce fundamental OpenGL functions, such as glClearColor, glClear,
glFlush, and glBegin/glEnd for drawing shapes.
5. Showcase the definition of a 2D orthographic projection and the rendering
of a geometric shape using specified vertex coordinates.
6. Provide a starting point for understanding the structure of a minimal
OpenGL program.

Code:
#include <GL/glut.h>

// Function to draw a star shape

void drawStar() {

glBegin(GL_POLYGON);

glColor3f(1.0f, 1.0f, 0.0f); // Yellow color

glVertex2i(200, 380); // Top point

glVertex2i(220, 320); // Lower-right point


glVertex2i(280, 320); // Upper-right point

glVertex2i(240, 260); // Lower-left point

glVertex2i(260, 200); // Upper-left point

glVertex2i(200, 240); // Top-right point

glVertex2i(140, 200); // Lower-right point

glVertex2i(160, 260); // Upper-left point

glVertex2i(120, 320); // Lower-left point

glVertex2i(180, 320); // Upper-right point

glEnd();

// Function to handle all drawings

void myDisplay() {

glClear(GL_COLOR_BUFFER_BIT);

// Draw a black background

glClearColor(0.0, 0.0, 0.0, 1.0);

// Draw the star shape

drawStar();
// Send all output to display

glFlush();

// Function to initialize OpenGL settings

void myInit() {

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 400.0, 0.0, 400.0);

// Main function

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

// Initialize GLUT

glutInit(&argc, argv);

// Set display mode and window size

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 400);

// Create the window


glutCreateWindow("Star Shape");

// Set the display function

glutDisplayFunc(myDisplay);

// Additional initializations

myInit();

// Enter the GLUT event processing loop

glutMainLoop();

return 0;

Output:
Result: The code successfully creates a basic OpenGL application that displays
a yellow star shape on a black background.

Conclusion: This simple OpenGL code demonstrates the foundational


concepts of graphics programming. It shows the setup of a graphical
environment, the use of GLUT and OpenGL functions for rendering, and the
creation of a visually appealing shape. This code serves as a starting point for
beginners to understand the structure of OpenGL programs and how to draw
basic geometric shapes on the screen.
Rotation of basic shape:
Code:
#include <GL/glut.h>

#include <cmath>

float angle = 0.0f; // Initial rotation angle

// Function to draw a star

void drawStar() {

glBegin(GL_POLYGON);

glVertex2f(200, 380); // Top point

glVertex2f(220, 320); // Lower-right point

glVertex2f(280, 320); // Upper-right point

glVertex2f(240, 260); // Lower-left point

glVertex2f(260, 200); // Upper-left point

glVertex2f(200, 240); // Top-right point

glVertex2f(140, 200); // Lower-right point

glVertex2f(160, 260); // Upper-left point

glVertex2f(120, 320); // Lower-left point

glVertex2f(180, 320); // Upper-right point

glEnd();
}

// Function to handle all drawings

void display() {

glClear(GL_COLOR_BUFFER_BIT);

// Draw the star with the current rotation angle

glPushMatrix();

glTranslatef(200, 200, 0);

glRotatef(angle, 0, 0, 1);

glTranslatef(-200, -200, 0);

glColor3f(1.0f, 1.0f, 0.0f); // Yellow color

drawStar();

glPopMatrix();

glutSwapBuffers();

// Function to update the rotation angle and request a redraw

void update(int value) {

angle += 1.0f; // Update rotation angle

glutPostRedisplay();
glutTimerFunc(16, update, 0); // 60 FPS

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(400, 400);

glutCreateWindow("Rotating Star");

glClearColor(0.0, 0.0, 0.0, 1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 400.0, 0.0, 400.0);

glutDisplayFunc(display);

glutTimerFunc(25, update, 0); // Initial timer

glutMainLoop();

return 0;

}
Output:

Reflection of basic shape:


Code:
#include <GL/glut.h>

#include <cmath>

// Function to draw a rotated and reflected star

void draw_rotated_and_reflected_star(float angle) {

glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 0.0f); // Yellow color

// Original star coordinates

float original_star_coords[][2] = {

{200, 380}, {220, 320}, {280, 320},

{240, 260}, {260, 200}, {200, 240},

{140, 200}, {160, 260}, {120, 320},

{180, 320}

};

// Draw the rotated star

for (int i = 0; i < 10; ++i) {

float x = original_star_coords[i][0];

float y = original_star_coords[i][1];

float rotated_x = x * cos(angle) - y * sin(angle);

float rotated_y = x * sin(angle) + y * cos(angle);

glVertex2f(rotated_x, rotated_y);

// Draw the reflected star

for (int i = 0; i < 10; ++i) {

float x = original_star_coords[i][0];

float y = original_star_coords[i][1];

float reflected_x = -x;


float reflected_y = y;

glVertex2f(reflected_x, reflected_y);

glEnd();

// Function to handle all drawings

void myDisplay(void) {

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

// Draw the rotated and reflected star

draw_rotated_and_reflected_star(45.0f);

glFlush();

// Function to initialize the drivers

void myInit(void) {

glClearColor(0.0, 0.0, 0.0, 1.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(-400.0, 400.0, 0.0, 400.0);

// Driver Code

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(800, 400);

glutInitWindowPosition(0, 0);

glutCreateWindow("Rotated and Reflected Star");

glutDisplayFunc(myDisplay);

myInit();

glutMainLoop();

return 0;

}
Output:
Experiment No. 02
Experiment Name : DDA Line Drawing Algorithm

The objectives of this code are to: The objective of the DDA
(Digital Differential Analyzer) line drawing algorithm code is to implement a
simple and efficient method for drawing a line between two given points (x1, y1)
and (x2, y2) on a graphics display. The code uses the DDA algorithm to calculate
and plot intermediate points along the line, providing a smooth and accurate
representation of the line segment.

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

float x1, x2, y1, y2;

void display(void) {

float dy, dx, step, x, y, k, Xin, Yin;

dx = x2 - x1;

dy = y2 - y1;

if (abs(dx) > abs(dy)) {


step = abs(dx);

} else

step = abs(dy);
Xin = dx / step;
Yin = dy / step;
x = x1;
y = y1;
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();

for (k = 1; k <= step; k++) {


x = x + Xin;
y = y + Yin;

glBegin(GL_POINTS);

glVertex2i(x, y);
glEnd();

glFlush();

void myInit (void) {


glClearColor(0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 640.0, 0.0, 480.0);

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

printf(" Enter 1st co-ordinate : ");


scanf("%f %f", & x1,&y1);
printf(" Enter 2nd co-ordinate : ");
scanf("%f %f", & x2,&y2);

glutInit( & argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500, 400);

glutInitWindowPosition(100, 150);

glutCreateWindow("");

myInit ();

glutDisplayFunc(display);
glutMainLoop();

Output:
Result: The code implements a simple line drawing algorithm using OpenGL
and GLUT. It takes user input for two points `(x1, y1)` and `(x2, y2)` and draws a
line connecting these two points on the window using the specified algorithm.
The resulting line is displayed on a black background.

Conclusion: This code implements the Digital Differential Analyzer (DDA) line
drawing algorithm using OpenGL and GLUT. It takes user input for two points `(x1,
y1)` and `(x2, y2)`, representing the endpoints of a line. The code then calculates
and plots the points along the line using the DDA algorithm, resulting in a straight
line displayed on the screen.
Experiment No. 03
Experiment Name : Bresenham’s Line Drawing
Algorithm

The objectives of this code are to:


1. Implement Bresenham's Line Drawing Algorithm.
2. Use OpenGL and GLUT for graphics rendering.
3. Accept user input for two points `(x1, y1)` and `(x2, y2)` to define the line.
4. Display the line on a window with a resolution of 500x500 pixels.

Code:
#include <GL/glut.h>
#include <iostream>

int x1, y1, x2, y2;

void myInit() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}

void draw_pixel(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void draw_line(int x1, int x2, int y1, int y2) {


int dx, dy, i, e;
int incx, incy, inc1, inc2;
int x, y;
dx = x2 - x1;
dy = y2 - y1;

if (dx < 0) dx = -dx;


if (dy < 0) dy = -dy;

incx = 1;
if (x2 < x1) incx = -1;

incy = 1;
if (y2 < y1) incy = -1;

x = x1;
y = y1;

if (dx > dy) {


draw_pixel(x, y);
e = 2 * dy - dx;
inc1 = 2 * (dy - dx);
inc2 = 2 * dy;

for (i = 0; i < dx; i++) {


if (e >= 0) {
y += incy;
e += inc1;
} else {
e += inc2;
}
x += incx;
draw_pixel(x, y);
}
} else {
draw_pixel(x, y);
e = 2 * dx - dy;
inc1 = 2 * (dx - dy);
inc2 = 2 * dx;
for (i = 0; i < dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
} else {
e += inc2;
}
y += incy;
draw_pixel(x, y);
}
}
}

void myDisplay() {
draw_line(x1, x2, y1, y2);
glFlush();
}

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


std::cout << "Enter the Co-ordinates of starting point of line: ";
std::cin >> x1 >> y1;

std::cout << "Enter the Co-ordinates of ending point of line ";


std::cin >> x2 >> y2;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();

return 0;
}

Output:
}

Result: The provided code is a basic program for drawing a line using the
Bresenham's Line Drawing algorithm in OpenGL. When you run this program, it
will prompt you to enter the coordinates of two points. After entering the values,
a window should appear, and it will display a line drawn between those two
points using Bresenham's algorithm. The line will be drawn in a window with a
black background (specified by glClearColor(0.0, 0.0, 0.0, 0.0)), and the line itself
will be white.

Conclusion: The provided code is a simple OpenGL program that uses


Bresenham's Line Drawing algorithm to draw a line between two user-defined
points. The code sets up a window, takes input for the line coordinates, and
displays the line in the window using OpenGL graphics.

Experiment No. 04
Experiment Name : Cohen Sutherland Line Clipping
Algorithm
The objectives of this code are to:
1. Implement Cohen-Sutherland Line Clipping algorithm.

2. Clip a line segment against a rectangular window.

3. Display the original line and the clipped result using OpenGL.

4. Enable interactive clipping with the 'c' key.

Code:
#include <GL/glut.h>
#include <iostream>

#define SCREEN_WIDTH 500


#define SCREEN_HEIGHT 400

const GLint WIN_LEFT_BIT = 0x01;


const GLint WIN_RIGHT_BIT = 0x02;
const GLint WIN_BOTTOM_BIT = 0x04;
const GLint WIN_TOP_BIT = 0x08;

class Point {
public:
GLfloat x, y;

Point(GLfloat x, GLfloat y) : x(x), y(y) {}


};

void swapPoints(Point &p1, Point &p2) {


Point temp = p1;
p1 = p2;
p2 = temp;
}

void swapCodes(GLint &code1, GLint &code2) {


GLint temp = code1;
code1 = code2;
code2 = temp;
}

GLint inside(GLint code) {


return !code;
}

GLint accept(GLint code1, GLint code2) {


return !(code1 | code2);
}

GLint reject(GLint code1, GLint code2) {


return code1 & code2;
}

GLint encode(Point p, Point winMin, Point winMax) {


GLint code = 0x00;

if (p.x < winMin.x)


code |= WIN_LEFT_BIT;
if (p.x > winMax.x)
code |= WIN_RIGHT_BIT;
if (p.y < winMin.y)
code |= WIN_BOTTOM_BIT;
if (p.y > winMax.y)
code |= WIN_TOP_BIT;

return code;
}
GLint roundFloat(GLfloat a) {
return static_cast<GLint>(a + 0.5f);
}

void lineClip(Point p1, Point p2, Point winMin, Point winMax) {


GLint code1, code2;
GLint done = 0, plotLine = 0;
GLfloat m = (p1.x != p2.x) ? (p2.y - p1.y) / (p2.x - p1.x) : 0;

while (!done) {
code1 = encode(p1, winMin, winMax);
code2 = encode(p2, winMin, winMax);

if (accept(code1, code2)) {
done = 1;
plotLine = 1;
} else if (reject(code1, code2)) {
done = 1;
} else {
if (inside(code1)) {
swapPoints(p1, p2);
swapCodes(code1, code2);
}

if (code1 & WIN_LEFT_BIT) {


p1.y += (winMin.x - p1.x) * m;
p1.x = winMin.x;
} else if (code1 & WIN_RIGHT_BIT) {
p1.y += (winMax.x - p1.x) * m;
p1.x = winMax.x;
} else if (code1 & WIN_BOTTOM_BIT) {
if (p1.x != p2.x)
p1.x += (winMin.y - p1.y) / m;
p1.y = winMin.y;
} else if (code1 & WIN_TOP_BIT) {
if (p1.x != p2.x)
p1.x += (winMax.y - p1.y) / m;
p1.y = winMax.y;
}
}
}

if (plotLine) {
glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex2i(roundFloat(p1.x), roundFloat(p1.y));
glVertex2i(roundFloat(p2.x), roundFloat(p2.y));
glEnd();
glFlush();
}
}

void drawWindow(Point winMin, Point winMax) {


glColor3f(0, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(roundFloat(winMin.x), roundFloat(winMin.y));
glVertex2i(roundFloat(winMin.x), roundFloat(winMax.y));
glVertex2i(roundFloat(winMax.x), roundFloat(winMax.y));
glVertex2i(roundFloat(winMax.x), roundFloat(winMin.y));
glEnd();
glFlush();
}

void initClip() {
glClear(GL_COLOR_BUFFER_BIT);

Point winMin(60, 60);


Point winMax(470, 290);

drawWindow(winMin, winMax);

Point p1(50, 50);


Point p2(490, 310);
glColor3f(0, 0, 1);
glBegin(GL_LINES);
glVertex2i(roundFloat(p1.x), roundFloat(p1.y));
glVertex2i(roundFloat(p2.x), roundFloat(p2.y));
glEnd();

lineClip(p1, p2, winMin, winMax);


}

void display() {}

void reshape(int w, int h) {


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

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutCreateWindow("Line Clipping");
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);

glutDisplayFunc(display);
glutReshapeFunc(reshape);

initClip();

glutMainLoop();
return 0;
}

Output:
Result: The result of this code is an interactive demonstration of the Cohen-
Sutherland Line Clipping algorithm using OpenGL. The program prompts the user
to enter the coordinates of two points representing a line segment. After entering
the coordinates, it opens a window displaying both the original line and the
clipped result. The user can trigger the clipping process by pressing the 'c' key.
The clipped line is then displayed in the window.

Conclusion: The code implements the Cohen-Sutherland Line Clipping


algorithm using OpenGL to clip a line segment against a rectangular window. It
allows interactive clipping with the 'c' key, displaying both the original line and
the clipped result.

Experiment No. 05
Experiment Name : Sutherland-Hodgman polygon
clipping algorithm

The objectives of this code are to:


1. Implement Sutherland-Hodgman Polygon Clipping algorithm.

2. Clip a polygon against a rectangular window.

3. Allow user input to define the vertices of the polygon.

4. Enable interactive clipping with the 'c' key.

5. Display the original polygon and the clipped result using OpenGL.

Code:
#include <iostream>

#include <cmath>

#include <GL/glut.h>

#include <vector>

class Point {

public:

int x;

int y;

Point(int x, int y) : x(x), y(y) {}


};

std::vector<Point> subjectPolygon;

std::vector<Point> clipPolygon;

void drawPolygon(const std::vector<Point>& polygon) {

glBegin(GL_LINE_LOOP);

for (const Point& point : polygon) {

glVertex2i(point.x, point.y);

glEnd();

void drawClipWindow(int xmin, int ymin, int xmax, int ymax) {

glBegin(GL_LINE_LOOP);

glVertex2i(xmin, ymin);

glVertex2i(xmin, ymax);

glVertex2i(xmax, ymax);

glVertex2i(xmax, ymin);

glEnd();

}
void drawClippedPolygon(const std::vector<Point>& clippedPolygon) {

glBegin(GL_LINE_LOOP);

for (const Point& point : clippedPolygon) {

glVertex2i(point.x, point.y);

glEnd();

std::vector<Point> clipPolygonFunction(const std::vector<Point>&


subjectPolygon, const std::vector<Point>& clipPolygon) {

std::vector<Point> clippedPolygon = subjectPolygon;

for (int i = 0; i < 4; ++i) {

std::vector<Point> clipEdge = {clipPolygon[i], clipPolygon[(i + 1) % 4]};

std::vector<Point> inputPolygon = clippedPolygon;

clippedPolygon.clear();

Point s = inputPolygon.back();

for (const Point& p : inputPolygon) {

if (inside(p, clipEdge)) {
if (!inside(s, clipEdge)) {

clippedPolygon.push_back(intersect(s, p, clipEdge));

clippedPolygon.push_back(p);

} else if (inside(s, clipEdge)) {

clippedPolygon.push_back(intersect(s, p, clipEdge));

s = p;

return clippedPolygon;

bool inside(const Point& point, const std::vector<Point>& clipEdge) {

return (clipEdge[1].x - clipEdge[0].x) * (point.y - clipEdge[0].y) >

(clipEdge[1].y - clipEdge[0].y) * (point.x - clipEdge[0].x);

Point intersect(const Point& s, const Point& p, const std::vector<Point>&


clipEdge) {

float m = (p.y - s.y) / (p.x - s.x != 0 ? p.x - s.x : 1); // Handle division by zero
float c = s.y - m * s.x;

if (clipEdge[1].x - clipEdge[0].x != 0) {

int x = clipEdge[1].x;

int y = m * x + c;

return Point(x, y);

} else {

int y = clipEdge[1].y;

int x = (y - c) / m;

return Point(x, y);

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

drawPolygon(subjectPolygon);

drawClipWindow(minX(), minY(), maxX(), maxY());

std::vector<Point> clippedPolygon = clipPolygonFunction(subjectPolygon,


clipPolygon);
glColor3f(1.0, 0.0, 0.0);

drawClippedPolygon(clippedPolygon);

glFlush();

void init() {

glClearColor(0.0, 0.0, 0.0, 1.0);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0, 640, 0, 480);

void mouse(int button, int state, int x, int y) {

y = 480 - y;

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

Point temp(x, y);

subjectPolygon.push_back(temp);

void reshape(int w, int h) {

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);

gluOrtho2D(0, w, 0, h);

int minX() {

return std::min_element(clipPolygon.begin(), clipPolygon.end(), [](const Point&


a, const Point& b) {

return a.x < b.x;

})->x;

int minY() {

return std::min_element(clipPolygon.begin(), clipPolygon.end(), [](const Point&


a, const Point& b) {

return a.y < b.y;

})->y;

int maxX() {

return std::max_element(clipPolygon.begin(), clipPolygon.end(), [](const Point&


a, const Point& b) {

return a.x < b.x;

})->x;
}

int maxY() {

return std::max_element(clipPolygon.begin(), clipPolygon.end(), [](const Point&


a, const Point& b) {

return a.y < b.y;

})->y;

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(640, 480);

glutInitWindowPosition(100, 150);

glutCreateWindow("Polygon Clipping");

init();

glutDisplayFunc(display);

glutMouseFunc(mouse);

glutReshapeFunc(reshape);

glutMainLoop();
return 0;

Output:

Result: The code allows users to input polygon vertices, performs Sutherland-
Hodgman polygon clipping against a rectangular window, and displays both the
original polygon and the clipped result using OpenGL. Interactive clipping can be
triggered by pressing the 'c' key.

Conclusion: The code implements the Sutherland-Hodgman Polygon


Clipping algorithm using OpenGL. It allows the user to input the vertices of a
polygon and interactively clips the polygon against a rectangular window upon
pressing the 'c' key. The original polygon and the clipped result are displayed
using OpenGL.
Experiment No. 06
Experiment Name : Weiler Atherton polygon clipping
algorithm

The objectives of this code are to:


1. Accept user input for the vertices of the clipping window (cw) and the subject
polygon (sp).

2. Draw the original clipping window and subject polygon.

3. Implement the Weiler-Atherton Polygon Clipping algorithm to clip the subject


polygon against the clipping window.

4. Display the clipped result.

Code:
#include&lt;iostream&gt;

#include&lt;graphics.h&gt;

#include&lt;utility&gt;

using namespace std;

struct vertex

float x;

float y;
};

vertex cw[40], sp[40];

int n_cw, n_sp;

void draw_poly(vertex vlist[], int n)

for (int i=0; i&lt;n; i++)

line(vlist[i].x, vlist[i].y, vlist[(i+1)%n].x, vlist[(i+1)%n].y);

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&lt;0)

return 0; //for out

return 1; //for in

}
void intersection_lineseg(float &amp;x, float &amp;y, int x1, int y1, int x2, int y2,
int xa, int

ya, int xb, int yb)

x = -1;

y = -1;

if (x2==x1 &amp;&amp; xb==xa)

return;

else if (x2==x1)

float m2 = (yb-ya) / (float) (xb-xa);

x = x1;

y = ya - m2*(xa-x1);

else if (xb==xa)

float m1 = (y2-y1) / (float) (x2-x1);

x = xa;

y = y1 + m1*(xa-x1);

}
else

float m1 = (y2-y1) / (float) (x2-x1);

float m2 = (yb-ya) / (float) (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&gt;=x2 &amp;&amp; (x&lt;x2||x&gt;x1)) || (x2&gt;=x1 &amp;&amp;


(x&gt;x2||x&lt;x1)) || (y1&gt;=y2 &amp;&amp;

(y&lt;y2||y&gt;y1)) || (y2&gt;=y1 &amp;&amp; (y&gt;y2||y&lt;y1))

|| (xa&gt;=xb &amp;&amp; (x&lt;xb||x&gt;xa)) || (xb&gt;=xa &amp;&amp;


(x&gt;xb||x&lt;xa)) || (ya&gt;=yb &amp;&amp;

(y&lt;yb||y&gt;ya)) || (yb&gt;=ya &amp;&amp; (y&gt;yb||y&lt;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

int e=-1;

//for new cw array

int kc=-1; //first vertex gets added last in array

for (int i=0; i&lt;n_cw; i++)

vertex tempi[20][2]; //for ordering intersection points, 2nd column&#39;s x is

for tag

int ti = -1;

for (int j=0; j&lt;n_sp; j++)

intersection_lineseg(x, y, cw[i].x, cw[i].y, cw[(i+1)%n_cw].x,

cw[(i+1)%n_cw].,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 &amp;&amp; p2==0)

tempi[ti][1].x = 1;

else

tempi[ti][1].x = 0;

if (ti!=-1)

if (cw[(i+1)%n_cw].x &gt; cw[i].x) //sort intersection points

//increasing x sort

int min_idx;

for (int k=0; k&lt;ti; k++)

{
min_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].x &lt; 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;

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 &lt; cw[i].x)

//decreasing x sort

int max_idx;
for (int k=0; k&lt;ti; k++)

max_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].x &gt; 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 &gt; cw[i].y)


{

//increasing y sort

int min_idx;

for (int k=0; k&lt;ti; k++)

min_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].y &lt; 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;

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&lt;ti; k++)

max_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].y &gt; 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&lt;=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;

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 array

for (int i=0; i&lt;n_sp; i++)

vertex tempi[20][2]; //for ordering intersection points, 2nd column&#39;s x is

for tag

int ti = -1;

for (int j=0; j&lt;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 &amp;&amp; p2==0) {

tempi[ti][1].x = 0;

else {

tempi[ti][1].x = 1;

if (ti!=-1)

if (sp[(i+1)%n_sp].x &gt; sp[i].x) //sort intersection points

//increasing x sort

int min_idx;

for (int k=0; k&lt;ti; k++)

min_idx = k;
for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].x &lt; 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;

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 &lt; sp[i].x)

//decreasing x sort

int max_idx;
for (int k=0; k&lt;ti; k++)

max_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].x &gt; 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 &gt; sp[i].y)


{

//increasing y sort

int min_idx;

for (int k=0; k&lt;ti; k++)

min_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].y &lt; 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;

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&lt;ti; k++)

max_idx = k;

for (int m=k+1; m&lt;ti+1; m++)

if (tempi[m][0].y &gt; 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&lt;=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;

if (tag_sp[ks]==1) {

e++;

entry_list[e] = ks;

trav_sp[ks] = 0;

}
}

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;

n_cw = kc+1;

n_sp = ks+1;

//traversal

for (int i=0; i&lt;=e; i++)

bool done = false;

int j = entry_list[i];

while(!done)

if (trav_sp[j] == 1)
done = true;

else if (tag_sp[j] == 1 || tag_sp[j] == -1)

line(tempsp[j].x, tempsp[j].y, tempsp[(j+1)%n_sp].x,

tempsp[(j+1)%n_sp].y);

trav_sp[j] = 1;

j++;

else if (tag_sp[j] == 0)

trav_sp[j] = 1;

//swap

for (int k=0; k&lt;n_cw; k++) //find location to switch to

if (tempcw[k].x==tempsp[j].x &amp;&amp; tempcw[k].y==tempsp[j].y)

j = k;

break;

}
swap(tempcw, tempsp);

swap(tag_cw, tag_sp);

swap(trav_cw, trav_sp);

int n = n_cw;

n_cw = n_sp;

n_sp = n_cw;

int main()

int gd=DETECT, gm=0;

initgraph(&amp;gd, &amp;gm, &quot;C:\\TURBOC3\\BGI&quot;);

cout&lt;&lt;&quot;Enter no. of vertices in clipping window&quot;&lt;&lt;endl;

cin&gt;&gt;n_cw;

cout&lt;&lt;&quot;Enter vertices (x,y) clockwise&quot;&lt;&lt;endl;

for (int i=0; i&lt;n_cw; i++)

cin&gt;&gt;cw[i].x&gt;&gt;cw[i].y;
draw_poly(cw, n_cw);

cout&lt;&lt;&quot;Enter no. of vertices in subject polygon&quot;&lt;&lt;endl;

cin&gt;&gt;n_sp;

cout&lt;&lt;&quot;Enter vertices (x,y) clockwise&quot;&lt;&lt;endl;

for (int i=0; i&lt;n_sp; i++)

cin&gt;&gt;sp[i].x&gt;&gt;sp[i].y;

draw_poly(sp, n_sp);

char ch;

cout&lt;&lt;&quot;Press a key to clip&quot;&lt;&lt;endl;

cin&gt;&gt;ch;

cleardevice();

draw_poly(cw, n_cw);

wa_clip();

getch();

closegraph();

return 0;

}
Output:

Result: The code implements the Weiler-Atherton Polygon Clipping algorithm


to clip a subject polygon against a clipping window. The result is the display of the
clipped subject polygon within the boundaries of the clipping window .

Conclusion: The code uses the Weiler-Atherton Polygon Clipping algorithm


to clip a subject polygon against a clipping window in C++ using the graphics.h
library. It takes user input for the vertices of both the clipping window and the
subject polygon, displays the original polygons, performs the clipping, and shows
the resulting clipped polygon.
Experiment No. 07
Experiment Name : Bresenham Circle drawing
algorithm
The objectives of this code are to:
1. Draw two concentric circles using Bresenham's circle drawing algorithm.

2. Utilize OpenGL and GLUT for graphics rendering.

Code:
#include <iostream>

#include <cmath>

#include <GL/glut.h>

// Center of the circle = (320, 240)

int xc = 320, yc = 240;

void plot_point(int x, int y) {

glBegin(GL_POINTS);

glVertex2i(xc + x, yc + y);

glVertex2i(xc + x, yc - y);

glVertex2i(xc + y, yc + x);

glVertex2i(xc + y, yc - x);

glVertex2i(xc - x, yc - y);
glVertex2i(xc - y, yc - x);

glVertex2i(xc - x, yc + y);

glVertex2i(xc - y, yc + x);

glEnd();

void bresenham_circle(int r) {

int x = 0, y = r;

float pk = (5.0 / 4.0) - r;

plot_point(x, y);

while (x < y) {

x = x + 1;

if (pk < 0)

pk = pk + 2 * x + 1;

else {

y = y - 1;

pk = pk + 2 * (x - y) + 1;

}
plot_point(x, y);

glFlush();

void concentric_circles(int radius1, int radius2) {

glClear(GL_COLOR_BUFFER_BIT);

bresenham_circle(radius1);

bresenham_circle(radius2);

glFlush();

void display() {

// Function left blank intentionally.

void Init() {

glClearColor(1.0, 1.0, 1.0, 0);

glColor3f(0.0, 0.0, 0.0);


gluOrtho2D(0, 640, 0, 480);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowPosition(0, 0);

glutInitWindowSize(640, 480);

glutCreateWindow("bresenham_circle");

Init();

int radius1, radius2;

std::cout << "Enter the radius of the first circle: ";

std::cin >> radius1;

std::cout << "Enter the radius of the second circle: ";

std::cin >> radius2;

glutDisplayFunc(display);

glutIdleFunc([&]() { concentric_circles(radius1, radius2); });

glutMainLoop();

return 0;
Output:

Enter the radius of the first circle: 200

Enter the radius of the second circle: 100

Result: The code draws two concentric circles using Bresenham's circle
drawing algorithm, displayed using OpenGL and GLUT.

Conclusion: The code uses Bresenham's circle drawing algorithm to draw


two concentric circles using OpenGL and GLUT for graphics rendering.
Experiment No. 08
Experiment Name : Mid-Point circle drawing algorithm
The objectives of this code are to:
The objective of this code is to implement the Midpoint Circle Drawing Algorithm
using OpenGL and GLUT, allowing the user to input the center coordinates and
radius of a circle. The points on the circle are plotted to form the circle on the
screen.

Code:
#include <iostream>

#include <cmath>

#include <GL/glut.h>

int pntX1, pntY1, r;

void plot(int x, int y) {

glBegin(GL_POINTS);

glVertex2i(x + pntX1, y + pntY1);

glEnd();

void midPointCircleAlgo() {
int x = 0;

int y = r;

float decision = 5.0 / 4.0 - r;

plot(x, y);

while (y > x) {

if (decision < 0) {

x += 1;

decision += 2 * x + 1;

} else {

y -= 1;

x += 1;

decision += 2 * (x - y) + 1;

plot(x, y);

plot(x, -y);

plot(-x, y);

plot(-x, -y);

plot(y, x);

plot(-y, x);
plot(y, -x);

plot(-y, -x);

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0); // White color for points

midPointCircleAlgo();

glFlush();

void myInit() {

glClearColor(0.0, 0.0, 0.0, 0.0); // Black background

gluOrtho2D(0.0, 640.0, 0.0, 480.0);

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

std::cout << "Enter the coordinates of the center:\n" << std::endl;


std::cout << "X-coordinate : ";

std::cin >> pntX1;

std::cout << "\nY-coordinate : ";

std::cin >> pntY1;

std::cout << "\nEnter radius : ";

std::cin >> r;

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(640, 480);

glutInitWindowPosition(100, 150);

glutCreateWindow("Midpoint Circle Algorithm");

glutDisplayFunc(display);

myInit();

glutMainLoop();

return 0;

}
Output:
X-coordinate: 40
Y-coordinate: 50
Enter radius: 50

Result: The result of this code is an OpenGL window displaying a circle drawn
using the Midpoint Circle Drawing Algorithm. The user is prompted to enter the
coordinates of the center and the radius of the circle. Once the input is provided,
the circle is drawn on the screen, and the window remains open until closed by
the user. The circle is represented by points plotted using OpenGL's `GL_POINTS`.

Conclusion: The code successfully implements the Midpoint Circle Drawing


Algorithm using OpenGL and GLUT. It takes user input for the center coordinates
and radius of a circle and then uses the algorithm to calculate and plot the points
along the circumference of the circle. The resulting circle is displayed on the
screen. The implementation provides a visual representation of the circle using
the specified algorithm.

You might also like