Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)

Batch: C1 Roll No.: 16010121001

Experiment No. 04

TITLE: Write a program to demonstrate the LINE CLIPPING algorithm

AIM:

Visit Vlab and Explore it


https://cse18-iiith.vlabs.ac.in/exp/clipping-line/

Write a program to demonstrate the LINE CLIPPING algorithm


a. Cohen-Sutherland-algorithm
b. Mid-Point Subdivision Line Clipping Algorithm
c. Liang-Barsky Line Clipping Algorithm
______________________________________________________________________
Expected OUTCOME of Experiment:

CO3: Implement Clipping,3D Geometric Transformations and 3D viewing


_____________________________________________________________________
Books/ Journals/ Websites referred:
_____________________________________________________________________
Algorithm:

a) Cohen-Sutherland Algorithm

Step1:Calculate positions of both endpoints of the line


Step2:Perform OR operation on both of these end-points
Step3:If the OR operation gives 0000
Then
line is considered to be visible
else
Perform AND operation on both endpoints
If And ≠ 0000
then the line is invisible
else
And=0000
Line is considered the clipped case.
Step4:If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)
(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(x-X1)
where X = Xwmin
where Xwminis the minimum value of X co-ordinate of window
(b) If bit 2 is "1" line intersect with right boundary

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
y3=y1+m(X-X1)
where X = Xwmax
where X more is maximum value of X co-ordinate of the window
(c) If bit 3 is "1" line intersects with bottom boundary
X3=X1+(y-y1)/m
where y = ywmin
ywmin is the minimum value of Y co-ordinate of the window
(d) If bit 4 is "1" line intersects with the top boundary
X3=X1+(y-y1)/m
where y = ywmax
ywmax is the maximum value of Y co-ordinate of the window
b) Mid-Point Subdivision Line Clipping Algorithm

Step1: Calculate the position of both endpoints of the line


Step2: Perform OR operation on both of these endpoints
Step3: If the OR operation gives 0000
then
Line is guaranteed to be visible
else
Perform AND operation on both endpoints.
If AND ≠ 0000
then the line is invisible
else
AND=6000
then the line is clipped case.
Step4: For the line to be clipped. Find midpoint
Xm=(x1+x2)/2
Ym=(y1+y2)/2
Xmis midpoint of X coordinate.
Ymis midpoint of Y coordinate.
Step5: Check each midpoint, whether it nearest to the boundary of a window or not.
Step6: If the line is totally visible or totally rejected not found then repeat step 1 to
5.
Step7: Stop algorithm.
c) Liang-Barsky Line Clipping Algorithm

1. Set tmin=0, tmax=1.


2. Calculate the values of t (t(left), t(right), t(top), t(bottom)),
(i) If t < tmin ignore that and move to the next edge.
(ii) else separate the t values as entering or exiting values using the inner product.
(iii) If t is entering value, set tmin = t; if t is existing value, set tmax = t.
3. If tmin < tmax, draw a line from (x1 + tmin(x2-x1), y1 + tmin(y2-y1)) to (x1 +
tmax(x2-x1), y1 + tmax(y2-y1))
4. If the line crosses over the window, (x1 + tmin(x2-x1), y1 + tmin(y2-y1)) and
(x1 + tmax(x2-x1), y1 + tmax(y2-y1)) are the intersection point of line and
edge.

Implementation details:

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
A) COHEN – SOUTHERLAND ALGO
#include <GL/glut.h>

// TBRL
int left = 1, right = 2, top = 8, bottom = 4;
int xmin = 100, xmax = 400, ymin = 100, ymax = 400;
int line_co_ordinate[4], i;
float x_start = 200, y_start = 250, x_end = 450, y_end = 450;
int c1, c2;
int flag = 1, clip_flag = 0;

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

int getCode(int x, int y) {


int code = 0;
if (x < xmin)
code = code | left;
if (x > xmax)
code = code | right;
if (y < ymin)
code = code | bottom;
if (y > ymax)
code = code | top;
return code;
}

void clip() {
int c;
float x, y;
float m = (y_end - y_start) / (x_end - x_start);

if (c1)
c = c1;
else
c = c2;

if (c & left) {
x = xmin;
y = y_start + m * (xmin - x_start);
}
if (c & right) {
x = xmax;
y = m * (xmax - x_start) + y_start;
}

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
if (c & top) {
y = ymax;
x = ((ymax - y_start) / m) + x_start;
}
if (c & bottom) {
y = ymin;
x = ((ymin - y_start) / m) + x_start;
}

if (c == c1) {
x_start = x;
y_start = y;
} else {
x_end = x;
y_end = y;
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(4);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();

if (flag == 1) {
glBegin(GL_LINES);
glVertex2f(x_start, y_start);
glVertex2f(x_end, y_end);
glEnd();
}

while (1 & clip_flag == 1) {


c1 = getCode(x_start, y_start);
c2 = getCode(x_end, y_end);

if ((c1 | c2) == 0)
break;
else if ((c1 & c2) != 0) {
flag = 0;
break;
} else {
clip();
}
}
glFlush();

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
}

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


if (ch == 'c') {
clip_flag = 1;
glutPostRedisplay();
}
}

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


if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && i < 4) {
line_co_ordinate[i] = x;
i = i + 1;
line_co_ordinate[i] = 500 - y;
i++;
}
if (i == 4) {
i = 0;
x_start = line_co_ordinate[0];
y_start = line_co_ordinate[1];
x_end = line_co_ordinate[2];
y_end = line_co_ordinate[3];
display();
}
}

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


glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Cohen-Sutherland");
myInit();
glutDisplayFunc(display);
glutKeyboardFunc(Key);
glutMouseFunc(mouse_input);
glutMainLoop();
return 0;
}

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

B) Mid-Point Subdivision Line Clipping Algorithm

#include <GL/glut.h>
#include <stdio.h>
float xmin = 50, xmax = 300, ymin = 50, ymax = 300;
float x_coord1, y_coord1, x_coord2, y_coord2;
void drawRectangle() {
glColor3f(1.0, 0.0, 0.0); // Red color
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
}
void drawLine(float x1, float y1, float x2, float y2, float r, float g, float b) {
glColor3f(r, g, b);
glBegin(GL_LINES);
glVertex2f(x1, y1);

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
glVertex2f(x2, y2);
glEnd();
}
int clipLine() {
float dx = x_coord2 - x_coord1;
float dy = y_coord2 - y_coord1;
float t1 = 0, t2 = 1;
float p[4] = { -dx, dx, -dy, dy };
float q[4] = { x_coord1 - xmin, xmax - x_coord1, y_coord1 - ymin, ymax -
y_coord1 };
for (int i = 0; i < 4; i++) {
if (p[i] == 0) {
if (q[i] < 0) {
return 0; // Line is parallel to clipping boundary and outside
}
} else {
float t = q[i] / p[i];
if (p[i] < 0) {
if (t > t2) {
return 0; // Line is outside
} else if (t > t1) {
t1 = t;
}
} else {
if (t < t1) {
return 0; // Line is outside
} else if (t < t2) {
t2 = t;
}
}
}
}
// Calculate the new clipped coordinates
float x1_clip = x_coord1 + t1 * dx;
float y1_clip = y_coord1 + t1 * dy;
float x2_clip = x_coord1 + t2 * dx;
float y2_clip = y_coord1 + t2 * dy;
drawRectangle(); // Draw the clipping rectangle
drawLine(x_coord1, y_coord1, x_coord2, y_coord2, 0.0, 0.0, 0.0); // Draw the
original line (in black)
drawLine(x1_clip, y1_clip, x2_clip, y2_clip, 0.0, 0.0, 1.0); // Draw the clipped
line (in blue)
return 1;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);

int clipped = clipLine();


if (!clipped) {
drawRectangle(); // Draw the clipping rectangle

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
drawLine(x_coord1, y_coord1, x_coord2, y_coord2, 0.0, 0.0, 0.0); // Draw the
original line (in black)
}
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char** argv) {
printf("Enter line coordinates (x1 y1 x2 y2): ");
scanf("%f %f %f %f", &x_coord1, &y_coord1, &x_coord2, &y_coord2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Liang-Barsky Line Clipping");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutMainLoop();
return 0;
}

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

C) Liang-Barsky Line Clipping Algorithm

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

float xmin = 50, xmax = 300, ymin = 50, ymax = 300;


float x_coord1, y_coord1, x_coord2, y_coord2;
void drawRectangle() {
glColor3f(1.0, 0.0, 0.0); // Red color
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
}
void drawClippedLine(float x1, float y1, float x2, float y2) {
glColor3f(0.0, 0.0, 1.0); // Blue color
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void LiangBarskyLineClip() {
float dx = x_coord2 - x_coord1;
float dy = y_coord2 - y_coord1;
float t1 = 0, t2 = 1;
float p[4] = { -dx, dx, -dy, dy };
float q[4] = { x_coord1 - xmin, xmax - x_coord1, y_coord1 - ymin, ymax - y_coord1
};
for (int i = 0; i < 4; i++) {
if (p[i] == 0) {
if (q[i] < 0) {
return; // Line is parallel to clipping boundary and outside

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
}
} else {
float t = q[i] / p[i];
if (p[i] < 0) {
if (t > t2) {
return; // Line is outside
} else if (t > t1) {
t1 = t;
}
} else {
if (t < t1) {
return; // Line is outside
} else if (t < t2) {
t2 = t;
}
}
}
}
// Calculate the new clipped coordinates
float x1_clip = x_coord1 + t1 * dx;
float y1_clip = y_coord1 + t1 * dy;
float x2_clip = x_coord1 + t2 * dx;
float y2_clip = y_coord1 + t2 * dy;
drawRectangle(); // Draw the clipping rectangle
drawClippedLine(x1_clip, y1_clip, x2_clip, y2_clip); // Draw the clipped line
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the line before clipping (in red)


glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(x_coord1, y_coord1);
glVertex2f(x_coord2, y_coord2);
glEnd();
LiangBarskyLineClip(); // Perform clipping and draw the clipped line (in blue)
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char** argv) {
printf("Enter line coordinates (x1 y1 x2 y2): ");
scanf("%f %f %f %f", &x_coord1, &y_coord1, &x_coord2, &y_coord2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 800);

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
glutCreateWindow("Liang-Barsky Line Clipping");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutMainLoop();
return 0;
}

Output(s) (final edited screen shot):

A) Cohen-Sutherland-algorithm
Before Clipping

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

B) Mid-Point Subdivision Line Clipping Algorithm

C) Liang-Barsky Line Clipping Algorithm

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Screenshots from VLab(if any):

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Conclusion and discussion (Comparative ): The choice of the best algorithm


depends on your specific requirements and the characteristics of your application. If
you have a rectangular clipping window and want a simple solution, Cohen-Sutherland
may be the best choice. Liang-Barsky is a versatile option suitable for a wide range of
cases. Mid-Point Subdivision provides flexibility for more complex clipping regions
but may require more computational resources in some scenarios.

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Date:
Signature of faculty in-charge

Post Lab
What is Turtle in CG, Demonstrate use of Turtle by implementing it?
Ans:
In computer graphics (CG), "Turtle Graphics" is a popular method of drawing pictures
using a virtual "turtle" that moves around the screen. The turtle can be instructed to
move forward, turn left or right, and draw lines as it moves.
It's a great way to introduce programming and geometry concepts to beginners.
Python provides a built-in module called turtle that allows you to create drawings using
Turtle Graphics.
The turtle graphics system provides a way to create drawings using a series of
commands. Common turtle commands include:
a) forward(distance): Move the turtle forward by a specified distance, leaving a
line behind.
b) backward(distance): Move the turtle backward by a specified distance, leaving
a line behind.
c) left(angle): Turn the turtle left by a specified angle.
d) right(angle): Turn the turtle right by a specified angle.
e) penup(): Lift the pen (stop drawing).
f) pendown(): Lower the pen (start drawing).
g) pencolor(color): Set the pen color to a specified color.
h) pensize(size): Set the pen size to a specified size.

Here's a simple example to demonstrate the use of the Turtle module by implementing
a basic drawing:
CODE:
import turtle
# Create a turtle object
t = turtle.Turtle()
turtle.bgcolor("lightblue") # Set the background color
# Set the turtle's properties
t.penup()
t.goto(0, -100) # Move to the center of the screen
t.pendown()
t.pensize(2)
t.speed(1)
# Draw the head (a yellow circle)
t.color("yellow")
t.begin_fill()
t.circle(100)
t.end_fill()
# Hide the turtle
t.hideturtle()
# Close the turtle graphics window on click
turtle.exitonclick()

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

OUTPUT

Department of Computer Engineering


Page No CG Sem V/ July - Dec 2023

You might also like