0% found this document useful (0 votes)
35 views10 pages

Line Clipping Algo

The document contains two algorithms for line clipping: the Cohen-Sutherland algorithm and the Liang-Barsky algorithm, both implemented in C. The Cohen-Sutherland algorithm uses a region code system to determine whether a line segment is inside, outside, or intersects a defined clipping rectangle. The Liang-Barsky algorithm employs parametric equations to efficiently calculate the intersection points of the line with the clipping boundaries.

Uploaded by

hetraj189
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)
35 views10 pages

Line Clipping Algo

The document contains two algorithms for line clipping: the Cohen-Sutherland algorithm and the Liang-Barsky algorithm, both implemented in C. The Cohen-Sutherland algorithm uses a region code system to determine whether a line segment is inside, outside, or intersects a defined clipping rectangle. The Liang-Barsky algorithm employs parametric equations to efficiently calculate the intersection points of the line with the clipping boundaries.

Uploaded by

hetraj189
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

Cohen Sutherland Line Clipping Algorithm.

#include <graphics.h>

#include <conio.h>

#include <stdio.h>

#define LEFT 1

#define RIGHT 2

#define BOTTOM 4

#define TOP 8

int xmin = 100, ymin = 100, xmax = 300, ymax = 300;

int computeCode(int x, int y) {

int code = 0;

if (x < xmin) code |= LEFT;

else if (x > xmax) code |= RIGHT;

if (y < ymin) code |= BOTTOM;

else if (y > ymax) code |= TOP;

return code;

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

int code1 = computeCode(x1, y1);

int code2 = computeCode(x2, y2);

int accept = 0;

while (1) {

if ((code1 == 0) && (code2 == 0)) {


accept = 1;

break;

} else if (code1 & code2) {

break;

} else {

int code_out;

int x, y;

if (code1 != 0)

code_out = code1;

else

code_out = code2;

if (code_out & TOP) {

x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);

y = ymax;

} else if (code_out & BOTTOM) {

x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);

y = ymin;

} else if (code_out & RIGHT) {

y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);

x = xmax;

} else if (code_out & LEFT) {

y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);

x = xmin;
}

if (code_out == code1) {

x1 = x;

y1 = y;

code1 = computeCode(x1, y1);

} else {

x2 = x;

y2 = y;

code2 = computeCode(x2, y2);

if (accept) {

setcolor(GREEN);

line(x1, y1, x2, y2);

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

// Draw the clipping window

rectangle(xmin, ymin, xmax, ymax);

// Original line
setcolor(RED);

int x1 = 50, y1 = 50, x2 = 350, y2 = 350;

line(x1, y1, x2, y2);

// Clipped line

cohenSutherlandClip(x1, y1, x2, y2);

getch();

closegraph();

return 0;

}
Liang-Barsky Algorithm

#include<stdio.h>

#include<graphics.h>

#include<math.h>

#include<dos.h>

void main()

int i,gd=DETECT,gm;

int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;

float t1,t2,p[4],q[4],temp;

x1=120;

y1=120;

x2=300;

y2=300;

xmin=100;

ymin=100;

xmax=250;
ymax=250;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

rectangle(xmin,ymin,xmax,ymax);

dx=x2-x1;

dy=y2-y1;

p[0]=-dx;

p[1]=dx;

p[2]=-dy;

p[3]=dy;

q[0]=x1-xmin;

q[1]=xmax-x1;

q[2]=y1-ymin;

q[3]=ymax-y1;

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

if(p[i]==0)
{

printf("line is parallel to one of the clipping boundary");

if(q[i]>=0)

if(i<2)

if(y1<ymin)

y1=ymin;

if(y2>ymax)

y2=ymax;

line(x1,y1,x2,y2);

if(i>1)
{

if(x1<xmin)

x1=xmin;

if(x2>xmax)

x2=xmax;

line(x1,y1,x2,y2);

t1=0;

t2=1;
for(i=0;i<4;i++)

temp=q[i]/p[i];

if(p[i]<0)

if(t1<=temp)

t1=temp;

else

if(t2>temp)

t2=temp;

if(t1<t2)

xx1 = x1 + t1 * p[1];

xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];

yy2 = y1 + t2 * p[3];

line(xx1,yy1,xx2,yy2);

delay(5000);

closegraph();

You might also like