0% found this document useful (0 votes)
140 views30 pages

Uday - CG Lab PDF

The document contains details about an experiment conducted by Kunwar Uday Singh to implement the DDA and Bresenham's line drawing algorithms in OpenGL. It includes the theory behind both algorithms, code samples for implementing each algorithm, and sample output. The aim was to draw lines between two points using the DDA and Bresenham's algorithms and display them using OpenGL.

Uploaded by

neha
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)
140 views30 pages

Uday - CG Lab PDF

The document contains details about an experiment conducted by Kunwar Uday Singh to implement the DDA and Bresenham's line drawing algorithms in OpenGL. It includes the theory behind both algorithms, code samples for implementing each algorithm, and sample output. The aim was to draw lines between two points using the DDA and Bresenham's algorithms and display them using OpenGL.

Uploaded by

neha
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
You are on page 1/ 30

Name: Kunwar Uday Singh

Branch: CSE OGI (B2)


Roll no: R970216036

_____________________________________________________________
UNIVERSITY OF PETROLEUM & ENERGY STUDIES
School of Computer Science
Dehradun
2019-20
______________________________________________________________

Course : B.Tech (CSE- OGI)


Subject : Computer Graphics Lab
Subject Code : CSIT
Semester : VI
Batch : 2016-2020
Submitted by : Kunwar Uday Singh
Sap Id : 500051980
Roll No. : R970216036
Faculty : Mr. Rohit Srivastava (Assistant Professor)

0
INDEX

S.No. Objective Page No.

1 Introduction to OpenGL 1-1

2 Implement the DDA and Bresenham’s Line Drawing 2-6


Algorithm in OpenGL

3 Drawing Circle and Ellipse using Mid-point algorithm 7-12

4 Filling the objects using flood fill, boundary fill and scan 13-19
line fill algorithm

5, 6 Performing Clipping operation on line and polygon using 20-24


Cohen Sutherland and Sutherland Hodgeman algorithms
respectively

7, 8 Performing 2D TRANSFORMATIONS on objects 25-29


Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

EXPERIMENT 1

Aim: Introduction to OpenGL


Theory: Open Graphics Library (OpenGL) gives the programmer an interface with the
graphics hardware. It is a cross-language (language independent), cross-platform (platform
independent) API to render two- and three-dimensional objects into a frame-buffer. These
objects are described as sequences of vertices (that define geometric objects) or pixels (that
define images). OpenGL performs several processes on this data to convert it to pixels to form
the final desired image in the frame-buffer.
Basic features of OpenGL: -
- Drawing primitives
- Transformations
- Color
- Lighting
Basic libraries in OpenGL: -
- GLU (OpenGL Utility Library)
- GLUT (OpenGL Utility Toolkit) : Interfaces with different windowing systems and
Allows easy porting between windowing systems
- FreeGLUT
GLUT Callback functions: -
1. glutDisplayFunc(myDisplay): window contents need to be redrawn
2. glutReshapeFunc(myReshape): called when window is reshaped
3. glutMouseFunc(myMouse): called when mouse button is pressed
4. glutKeyboardFunc(mykeyboard): called when keyboard is pressed or released

Install OpenGL on Ubuntu:


- For installing OpenGL on ubuntu, execute the following command in terminal :
sudo apt-get install freeglut3-dev
- For working on Ubuntu operating system:
gcc –o targetname filename.c -lGL -lGLU –lglut //to compile
./targetname //to run
where filename.c is the name of the file with which this program is saved.

1
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

EXPERIMENT 2

Aim: Implement the DDA and Bresenham’s Line Drawing Algorithm in OpenGL.

Theory: Digital Differential Analyzer algorithm is a simple line generation algorithm.

Steps: To draw a line, you need two points between which you can draw a line.
DDA Line Drawing Algorithm
Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm.
Step 1 – Load the values of two end points (X0, Y0) and (X1, Y1).
Step 2 − Calculate the difference between two end points.
dx = X1 - X0; dy = Y1 - Y0
Step 3 − Based on the calculated difference in step-2, identify the number of steps to put
pixel. The largest among dx and dy is assigned to ‘steps’.
Step 4 − Calculate the increment in x coordinate and y coordinate.
Xincrement = dx / (float) steps; Yincrement = dy / (float) steps;
Step 5 – Add the Xincrement and Yincrement to the initial point.
Step 6 – Add the Xincrement and Yincrement to the result from step-5 and keep adding to the results
till the final point is reached.
Code:
#include<GL/freeglut.h>
#include<stdlib.h>
#include<stdio.h>
float x1,x2,y3,y2;
void display(void){
float dy,dx,step,x,y,k,Xin,Yin;
dx=x2-x1;
dy=y2-y3;
if(abs(dx)> abs(dy)){
step = abs(dx);
}
else
step = abs(dy);
Xin = dx/step;
Yin = dy/step;
x= x1;
y=y3;
glBegin(GL_POINTS);
glVertex2i(x,y);

2
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

glEnd();
for (k=1 ;k<=step;k++){
x= x + Xin;
y= y + Yin;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void init(void){
glClearColor(0.7,0.7,0.7,0.7);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
}
int main(int argc, char** argv) {
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y3);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y2 : ");
scanf("%f",&y2);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100,100);
glutCreateWindow ("DDA");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:

3
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

Bresenham’s Line Generation Algorithm


The big advantage of this algorithm is that, it uses only integer calculations. Moving across
the x axis in unit intervals and at each step choose between two different y coordinates.
Here is the Bresenham algorithm for slope m < 1 −
Step 1 − Input the 2 end-points of line (X0, Y0) & (X1, Y1), storing the left end-point in (X0,
Y0).
Step 2 − Plot the point (X0, Y0).
Step 3 − Calculate the value of constants dx, dy, 2dy, and (2dy – 2dx) and get the first value
for the decision parameter as – P0 = 2dy−dx
Step 4 − At each Xk, Yk along the line, starting at k = 0, perform the following test −
If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2dy
Otherwise, the next point to plot is (Xk+1, Yk+1) and Pk+1 = Pk+2dy-2dx
Step 5 − Repeat step 4 (dx – 1) times.
For m > 1, find out whether you need to increment x while incrementing y each time.
After solving, the equation for decision parameter Pk will be very similar, just the x and y in
the equation gets interchanged.

Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <GL/freeglut.h>
#include <stdio.h>

4
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

int x1, y1, x2, y2;


void myInit() {
//glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 0);
glColor3f(0.0, 0.0, 0.0);
gluOrtho2D(0, 640, 0, 480);
glMatrixMode(GL_PROJECTION);
}
void draw_pixel(int x, int y) {
glColor3f(0.5, 0, 1);
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;

5
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

draw_pixel(x, y);
}
}
}
void myDisplay() {
draw_line(x1, x2, y1, y2);
glFlush();
}
void main(int argc, char **argv) {
printf("Enter two end points of the line to be drawn:\n");
printf("\nEnter Point1 ( X1 , Y1):\n");
scanf("%d%d", &x1, &y1);
printf("\nEnter Point2 ( X2 , Y2):\n");
scanf("%d%d", &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}

Output:

6
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

EXPERIMENT 3

Aim: Drawing Circle and Ellipse using Mid-point algorithm.

Theory:
Mid-Point Circle Drawing Algorithm
This algorithm is used to calculate all the perimeter points of the circle in the first octant and
then print them along with their mirror points in the other octants. This will work only
because a circle is symmetric about its center.
Step 1 − Input radius r and circle center (Xc, Yc) and obtain the first point on the
circumference of the circle centered on the origin as
(X0, Y0) = (0, r)
Step 2 − Calculate the initial value of decision parameter as P0 = 5/4 – r
Step 3 − At each Xk position starting at k=0, perform the following test −
If PK < 0, then next point on circle (0, 0) is (XK+1, YK) and PK+1 = PK + 2XK+1 + 1
Else, PK+1 = PK + 2XK+1 + 1 – 2YK+1
Where, 2XK+1 = 2XK+2 and 2YK+1 = 2YK-2.
Step 4 − Determine the symmetry points in other seven octants.
Step 5 − Move each calculate pixel position (X, Y) onto the circular path centered on (Xc,
Yc) and plot the coordinate values.
X = X + XC, Y = Y + YC
Step 6 − Repeat step-3 through 5 until X >= Y.

Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <GL/freeglut.h>
#include <math.h>
#include <stdio.h>

int pntX1, pntY1, r;

void plot(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(x + pntX1, y + pntY1);
glEnd();
}

void myInit(void)
{

7
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

glClearColor(1.0, 1.0, 1.0, 0.0);


glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void midPointCircleAlgo()
{
int x = 0;
int y = r;
float decision = 5 / 4 - r;
plot(x, y);

while (y > x)
{
if (decision < 0)
{
x++;
decision += 2 * x + 1;
}
else
{
y--;
x++;
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 myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.0, 1.0);
glPointSize(1.0);
midPointCircleAlgo();
glFlush();
}

void main(int argc, char** argv)


{
printf("Enter coordinates of the center:\n");
scanf("%d%d", &pntX1, &pntY1);
printf("\nEnter radius:\n");
scanf("%d", &r);

8
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Line Drawing Alogrithms");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();

Output:

9
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

Mid-Point Ellipse Drawing Algorithm


The midpoint ellipse method is applied throughout the first quadrant in two parts.

Step 1 − Take the input and ellipse center and obtain the first point on an ellipse centered on
the origin as a (x,y0)= (0,ry).

Step 2 −Now calculate the initial decision parameter in region 1 as: p10=ry2+1/4rx2-rx2ry

Step 3 − At each xk position in region 1 perform the following task. If p1k<0 then the next
point along the ellipse centered on (0,0) is (xk+1,yk).
i.e. p1k+1=p1k+2ry2xk+1+ry2
Otherwise the next point along the circle is (xk+1,yk -1)
i.e. p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2

Step 4 − Now, again calculate the initial value in region 2 using the last point (x0,y0)
calculated in a region 1 as : p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2

Step 5 − At each yk position in region 2 starting at k =0 perform the following task.


If p2k<0 the next point along the ellipse centered on (0,0) is (xk , yk-1)
i.e. p2k+1=p2k-2rx2yk+1+rx2
Otherwise the next point along the circle will be (xk+1,yk -1)
i.e. p2k+1 =p2k+2ry2xk+1 -2rx2yk+1+rx2

Step 6 − Now determine the symmetric points in another three quadrants.

Step 7 − Plot the coordinate value as: x=x+xc , y=y+yc

Step 8 − Repeat the steps for region 1 until 2ry2x>=2rx2y.

Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <GL/freeglut.h>
#include <math.h>
#include <stdio.h>

float xc;
float yc;
float rx;
float ry;

void Init(void){
glClearColor(1.0, 1.0, 1.0, 0.0); //Background Color
glColor3f(0.0f, 0.0f, 0.0f); //Black Color
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0); //Coordinate System
}

10
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

void putpixel(int x, int y){


glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void axes(void){
glBegin(GL_LINES);
glVertex2i(-500, 0);
glVertex2i(500, 0);
glVertex2i(0, 500);
glVertex2i(0, -500);
glEnd();
}
void ellipse(){
int x, y, p;
x = 0;
y = ry;
p = (ry*ry) - (rx*rx*ry) + ((rx*rx) / 4);
while ((2 * x*ry*ry)<(2 * y*rx*rx))
{
putpixel(xc + x, yc - y);
putpixel(xc - x, yc + y);
putpixel(xc + x, yc + y);
putpixel(xc - x, yc - y);
if (p<0)
{
x = x + 1;
p = p + (2 * ry*ry*x) + (ry*ry);
}
else
{
x = x + 1;
y = y - 1;
p = p + (2 * ry*ry*x + ry*ry) - (2 * rx*rx*y);
}
}
p = ((float)x + 0.5)*((float)x + 0.5)*ry*ry + (y - 1)*(y - 1)*rx*rx - rx*rx*ry*ry;
while (y >= 0)
{
putpixel(xc + x, yc - y);
putpixel(xc - x, yc + y);
putpixel(xc + x, yc + y);
putpixel(xc - x, yc - y);
if (p>0)
{
y = y - 1;
p = p - (2 * rx*rx*y) + (rx*rx);
}
else
{
y = y - 1;
x = x + 1;
p = p + (2 * ry*ry*x) - (2 * rx*rx*y) - (rx*rx);
}
}
}

11
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.0, 1.0);
glPointSize(1.0);
ellipse();
glFlush();
}
int main(int argc, char** argv){
printf("Enter the coordinates of the centre of the ellipse: \n");
scanf("%f%f", &xc, &yc);
printf("Enter the radius of the x-axis and y-axis: \n");
scanf("%f%f", &rx, &ry);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Ellipse");
Init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}

Output:

12
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

EXPERIMENT 4

Aim: Filling the objects using flood fill, boundary fill and scan line fill algorithm.

Theory: Polygon filling techniques.


Flood Fill Algorithm
Instead of relying on the boundary of the object, it relies on the fill colour. In other words, it
replaces the interior colour of the object with the fill colour. When no more pixels of the
original interior colour exist, the algorithm is completed. Algorithm for 4-connected: -
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till
the boundary pixels reached.
Step 4 − Change the default color with the fill color at the seed point.
Step 5 − Recursively follow the procedure with four neighborhood points.
Step 6 − Exit

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

int ww = 640, wh = 480;


float bgCol[3] = { 1.0, 0.0, 0.0 };
float intCol[3] = { 0.0, 0.0, 0.0 };
float fillCol[3] = { 0.4, 0.0, 0.0 };
void setPixel(int pointx, int pointy, float f[3])
{
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(pointx, pointy);
glEnd();
glFlush();
}
void getPixel(int x, int y, float pixels[3])
{
glReadPixels(x, y, 1.0, 1.0, GL_RGB, GL_FLOAT, pixels);
}
void drawPolygon(int x1, int y1, int x2, int y2)
{
glColor3f(0.0, 0.0, 0.0);

13
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
void display()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawPolygon(150, 250, 200, 300);
glFlush();
}
void floodfill4(int x, int y, float oldcolor[3], float newcolor[3])
{
float color[3];
getPixel(x, y, color);
if (color[0] == oldcolor[0] && (color[1]) == oldcolor[1] && (color[2]) == oldcolor[2])
{
setPixel(x, y, newcolor);
floodfill4(x + 1, y, oldcolor, newcolor);
floodfill4(x - 1, y, oldcolor, newcolor);
floodfill4(x, y + 1, oldcolor, newcolor);
floodfill4(x, y - 1, oldcolor, newcolor);
}
}
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
int xi = x;
int yi = (wh - y);
floodfill4(xi, yi, intCol, fillCol);
}
}
void myinit()
{
glViewport(0, 0, ww, wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)ww, 0.0, (GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww, wh);
glutCreateWindow("Flood-Fill-Recursive");
glutDisplayFunc(display);
myinit();
glutMouseFunc(mouse);
glutMainLoop();
return 0;

14
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

Output:

Boundary Fill Algorithm

This algorithm picks a point inside an object and starts to fill until it hits the boundary of the
object. The color of the boundary and the color that we fill should be different for this
algorithm to work. In this algorithm, we assume that color of the boundary is same for the
entire object.

Step 1 − It takes an interior point(x, y), a fill color, and a boundary color as the input.

Step 2 − The algorithm starts by checking the color of (x, y). If it’s color is not equal to the fill
color and the boundary color, then it is painted with the fill color and the function is called for
all the neighbours of (x, y).

Step 3 −If a point is found to be of fill color or of boundary color, the function does not call
its neighbours and returns.

Step 4 −This process continues until all points up to the boundary color for the region have
been tested.
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include <time.h>

void delay(float ms){


clock_t goal = ms + clock();

15
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

while (goal>clock());
}

void init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 640, 0, 480);
}

void bound_it(int x, int y, float* fillColor, float* bc){


float color[3];
glReadPixels(x, y, 1.0, 1.0, GL_RGB, GL_FLOAT, color);
if ((color[0] != bc[0] || color[1] != bc[1] || color[2] != bc[2]) && (
color[0] != fillColor[0] || color[1] != fillColor[1] || color[2] != fillColor[2])){
glColor3f(fillColor[0], fillColor[1], fillColor[2]);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
bound_it(x + 1, y, fillColor, bc);
bound_it(x - 2, y, fillColor, bc);
bound_it(x, y + 2, fillColor, bc);
bound_it(x, y - 2, fillColor, bc);
}
}

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


y = 480 - y;
if (btn == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
float bCol[] = { 1, 0, 0 };
float color[] = { 0, 0, 1 };
//glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,intCol);
bound_it(x, y, color, bCol);
}
}
}

void world(){
glLineWidth(3);
glPointSize(2);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(150, 100);
glVertex2i(300, 300);
glVertex2i(450, 100);
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

16
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Boundary Fill");
glutDisplayFunc(world);
glutMouseFunc(mouse);
init();
glutMainLoop();
return 0;
}

Output:

Scan Line Algorithm


This algorithm works by intersecting scanline with polygon edges and fills the polygon between pairs
of intersections. The following steps depict how this algorithm works.
Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax. Name each
intersection point of the polygon. As per the figure shown above, they are named as p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0, p1), (p1, p2), and
(p2, p3).
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the alternate pairs.

Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h, k, r;
void scanline(int, int);
void circle(void);
void Axes(void);
void display(void)

17
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.0, 1.0);
glBegin(GL_POINTS);
circle();
glEnd();
glFlush();
}
void scanline(int ax, int by)
{
int varx;
for (varx = h + ax; varx >= (h - ax); varx--)
glVertex2s(varx, by);
}
void circle(void)
{
double XEnd, J;
int i, j;
XEnd = (r / 1.414);
for (i = 0; i <= XEnd; i++)
{
J = sqrt(r*r - i*i);
j = (int)(J);
scanline(i, j);
scanline(j, i);
scanline(j, -i);
scanline(i, -j);
}
Axes();
glVertex3s(h, k, -25);
}
void Axes(void)
{
int i;
glColor3f(0.0, 0.0, 0.0);
for (i = -100; i <= 100; i++)
{
glVertex2s(i, 0);
glVertex2s(0, i);
}
for (i = -2; i <= 2; i++)
{
glVertex2s(95 + i, 4 + i);
glVertex2s(95 - i, 4 + i);
}
for (i = 0; i <= 2; i++)
{
glVertex2s(4 + i, 95 + i);
glVertex2s(4 - i, 95 + i);
glVertex2s(4, 95 - i);
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);

18
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
printf("Enter the center of circle:\n");
scanf("%d %d", &h, &k);
printf("Enter the radius:\n");
scanf("%d", &r);
glutCreateWindow("Circle : Scanline Filling Method ");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

19
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

Experiment 5 & 6
Aim: Performing Clipping operation on line and polygon using Cohen Sutherland and
Sutherland Hodgeman algorithms respectively.
Cohen Sutherland

Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define outcode int

double xmin = 50, ymin = 50, xmax = 100, ymax = 100;// Windows boundaries
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300; // Viewport
const int RIGHT = 8; // bit codes for the right
const int LEFT = 2; //bit codes for the left
const int TOP = 4; // bit codes for the top
const int BOTTOM = 1; //bit codes for the bottom
outcode ComputeOutCode(double x, double y);

void CohenSutherlandLineClipAnddraw(double x0, double y0, double x1, double y1){


outcode outcode0, outcode1, outcodeOut;
int accept = 0, done = 0;
outcode0 = ComputeOutCode(x0, y0);
outcode1 = ComputeOutCode(x1, y1);
do{
if (!(outcode0 | outcode1)){
accept = 1;
done = 1;
}
else
if (outcode0 & outcode1)
done = 1;
else{
double x, y;
outcodeOut = outcode0 ? outcode0 : outcode1;
if (outcodeOut & TOP) {
x = x0 + (x1 - x0)*(ymax - y0) / (y1 - y0);
y = ymax;
}
else
if (outcodeOut & BOTTOM) {
x = x0 + (x1 - x0)*(ymin - y0) / (y1 - y0);
y = ymin;
}
else
if (outcodeOut & RIGHT){
y = y0 + (y1 - y0)*(xmax - x0) / (x1 - x0);
x = xmax;
}

20
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

else{
y = y0 + (y1 - y0)*(xmin - x0) / (x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0){
x0 = x;
y0 = y;
outcode0 = ComputeOutCode(x0, y0);
}
else{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(x1, y1); }
}
} while (!done);
if (accept){
double sx = (xvmax - xvmin) / (xmax – xmin);
double sy = (yvmax - yvmin) / (ymax - ymin);
double vx0 = xvmin + (x0 - xmin)*sx;
double vy0 = yvmin + (y0 - ymin)*sy;
double vx1 = xvmin + (x1 - xmin)*sx;
double vy1 = yvmin + (y1 - ymin)*sy;

glColor3f(1.0, 0.0, 0.0);


glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2d(vx0, vy0);
glVertex2d(vx1, vy1);
glEnd();
}
}
outcode ComputeOutCode(double x, double y){
outcode code = 0;
if (y>ymax) //above the clip window
code |= TOP;
if (y<ymin) //below the clip window
code |= BOTTOM;
if (x>xmax) //to the right of the clip window
code |= RIGHT;
if (x<xmin) //to the left of the clip window
code |= LEFT;
return code;
}
void display(){
double x0 = 120, y0 = 10, x1 = 40, y1 = 130;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0); // draw red color lines
glBegin(GL_LINES);
glVertex2d(x0, y0);
glVertex2d(x1, y1);

21
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

glVertex2d(60, 20);
glVertex2d(80, 120);
glEnd();
glColor3f(0.0, 0.0, 1.0); // draw a blue colored window
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherlandLineClipAnddraw(x0, y0, x1, y1);
CohenSutherlandLineClipAnddraw(60, 20, 80, 120);
glFlush();
}
void myinit(){
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("cohen Sutherland line clipping algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}

Output:

22
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

Sutherland Hodgeman

Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>

int xmin = -100;


int xmax = 100;
int ymin = -100;
int ymax = 100;
int xmi, xma, ymi, yma;
int cut;
void display(){
glColor3f(0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2i(xmin, ymin);
glVertex2i(xmax, ymin);
glVertex2i(xmax, ymax);
glVertex2i(xmin, ymax);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2i(xmi, ymi);
glVertex2i(xma, ymi);
glVertex2i(xma, yma);
glVertex2i(xmi, yma);
glEnd();
glutSwapBuffers();
}
void clip(){
while (xmi<xmin || xma>xmax || ymi<ymin || yma>ymax){
if (xmi<xmin){
cut = xmin - xmi;
xmi = xmi + cut;
}
else if (xma>xmax){
cut = xma - xmax;
xma = xma - cut;
}
else if (ymi<ymin){
cut = ymin - ymi;
ymi = ymi + cut;
}
else if (yma>ymax){
cut = yma - ymax;
yma = yma - cut;
}
glutPostRedisplay();
}
}

23
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

void mykey(unsigned char key, int x, int y){


if (key == 'c')
{
clip();
}
}
void init(void){

glClearColor(0.0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-300, 300, -300, 300);

}
int main(int argc, char** argv){
printf("Enter the points for left bottom of polygon: \n");
scanf("%d%d", &xmi, &ymi);
printf("\nEnter the points for right up of the polygon: \n");
scanf("%d%d", &xma, &yma);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Clipping");
glutDisplayFunc(display);
glutKeyboardFunc(mykey);
init();
glutMainLoop();
return 0;
}

Output:

24
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

EXPERIMENT 7 & 8

Aim: Performing 2D TRANSFORMATIONS on objects.


Theory: Transformations play a very important role in manipulating objects on screen. There
are three basic kinds of Transformations in Computer Graphics:
1. Translation
2. Rotation
3. Scaling
Translation: Translation refers to moving an object to a different position on screen.
Formula: X = x + tx, Y = y + ty; where tx and ty are translation coordinates
The OpenGL function is glTranslatef( tx, ty, tz );
Rotation : Rotation refers to rotating a point.
Formula: X = xcosA – ysinA, Y = xsinA + ycosA; A is the angle of rotation.
The above formula will rotate the point around the origin.
To rotate around a different point, the formula:
X = cx + (x-cx)*cosA - (y-cy)*sinA, Y = cx + (x-cx)*sinA + (y-cy)*cosA,
cx, cy is centre coordinates, A is the angle of rotation.
The OpenGL function is glRotatef (A, x, y, z).

Scaling : Scaling refers to zooming in and out an object in different scales across axes.
Formula: X = x*sx, Y = y*sy, sx, sy being scaling factors.
The OpenGL function is glScalef(float x, float y, float z)
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
using namespace std;

int pntX1, pntY1, choice = 0, edges;


vector<int> pntX;
vector<int> pntY;
int transX, transY;
double scaleX, scaleY;
double angle, angleRad;

25
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

double round(double d){


return floor(d + 0.5);
}
void drawPolygon(){
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < edges; i++)
{
glVertex2i(pntX[i], pntY[i]);
}
glEnd();
}
void drawPolygonTrans(int x, int y){
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
for (int i = 0; i < edges; i++)
{
glVertex2i(pntX[i] + x, pntY[i] + y);
}
glEnd();
}
void drawPolygonScale(double x, double y){
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
for (int i = 0; i < edges; i++){
glVertex2i(round(pntX[i] * x), round(pntY[i] * y));
}
glEnd();
}
void drawPolygonRotation(double angleRad){
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
for (int i = 0; i < edges; i++){
glVertex2i(round((pntX[i] * cos(angleRad)) - (pntY[i] * sin(angleRad))),
round((pntX[i] * sin(angleRad)) + (pntY[i] * cos(angleRad))));
}
glEnd();
}
void myInit(void){
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-640.0, 640.0, -480.0, 480.0);
}
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
if (choice == 1){
drawPolygon();
drawPolygonTrans(transX, transY);
}
else if (choice == 2){
drawPolygon();
drawPolygonScale(scaleX, scaleY);

26
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

}
else if (choice == 3){
drawPolygon();
drawPolygonRotation(angleRad);
}
glFlush();
}
void main(int argc, char** argv){
cout << "1. Translation" << endl;
cout << "2. Scaling" << endl;
cout << "3. Rotation" << endl;
cout << "4. Exit" << endl;
cout << "\n\nEnter your choice: " << endl;
cin >> choice;
if (choice == 6) {
return;
}
cout << "\n\nFor Polygon:\n" << endl;
cout << "Enter no of edges: "; cin >> edges;
for (int i = 0; i < edges; i++){
cout << "Enter co-ordinates for vertex " << i + 1 << " : "; cin >> pntX1 >> pntY1;
pntX.push_back(pntX1);
pntY.push_back(pntY1);
}
if (choice == 1){
cout << "Enter the translation factor for X and Y: "; cin >> transX >> transY;
}
else if (choice == 2){
cout << "Enter the scaling factor for X and Y: "; cin >> scaleX >> scaleY;
}
else if (choice == 3){
cout << "Enter the angle for rotation: "; cin >> angle;
angleRad = angle * 3.1416 / 180;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Extended Basic Transformations");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();

27
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036

Output:
Translation -

Scaling -

Rotation -

28

You might also like