0% found this document useful (0 votes)
55 views

Assignment 2 NAME-Rutik Kishor Rokade Roll No - 205B060: Program

This program performs Cohen-Sutherland line clipping algorithm. It takes input of window boundaries and line endpoints, calculates the outcodes for each point, and iteratively clips the line segment if it lies outside the window by finding the intersection point with the window edge. It draws the original window rectangle and clipped line segment in red if accepted after clipping. The program outputs the clipped line segment between points (60, 50) and (20, 40) within the specified window boundaries.

Uploaded by

390 RUTIK ROKADE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Assignment 2 NAME-Rutik Kishor Rokade Roll No - 205B060: Program

This program performs Cohen-Sutherland line clipping algorithm. It takes input of window boundaries and line endpoints, calculates the outcodes for each point, and iteratively clips the line segment if it lies outside the window by finding the intersection point with the window edge. It draws the original window rectangle and clipped line segment in red if accepted after clipping. The program outputs the clipped line segment between points (60, 50) and (20, 40) within the specified window boundaries.

Uploaded by

390 RUTIK ROKADE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT 2

NAME- Rutik Kishor Rokade


Roll no – 205B060

PROGRAM:

#include<conio.h>

#include<iostream>

#include<graphics.h>

using namespace std;

static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xmin,ymin,xmax,ymax;

int getcode(int x,int y){

int code = 0;

//Perform Bitwise OR to get outcode

if(y > ymax) code |=TOP;

if(y < ymin) code |=BOTTOM;

if(x < xmin) code |=LEFT;

if(x > xmax) code |=RIGHT;

return code;

int main()

int gdriver = DETECT,gmode;

initgraph(&gdriver,&gmode," ");

setcolor(WHITE);

cout<<"Enter Enter windows min and max values: ";

cin>>xmin>>ymin>>xmax>>ymax;

rectangle(xmin,ymin,xmax,ymax);
int x1,y1,x2,y2;

cout<<"Enter the endpoints of the line: ";

cin>>x1>>y1>>x2>>y2;

line(x1,y1,x2,y2);

getch();

int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2);

int accept = 0; //decides if line is to be drawn

while(1){

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

//Both points inside. Accept line

if(outcode1==0 && outcode2==0)

accept = 1;

break;

//AND of both codes != 0.Line is outside. Reject line

else if((outcode1 & outcode2)!=0)

break;

else

int x,y;

int temp;

//Decide if point1 is inside, if not, calculate intersection

if(outcode1==0)

temp = outcode2;

else

temp = outcode1;

//Line clips top edge

if(temp & TOP){


x = x1+ (ymax-y1)/m;

y = ymax;

else if(temp & BOTTOM){ //Line clips bottom edge

x = x1+ (ymin-y1)/m;

y = ymin;

}else if(temp & LEFT){ //Line clips left edge

x = xmin;

y = y1+ m*(xmin-x1);

}else if(temp & RIGHT){ //Line clips right edge

x = xmax;

y = y1+ m*(xmax-x1);

//Check which point we had selected earlier as temp, and replace its co-ordinates

if(temp == outcode1){

x1 = x;

y1 = y;

outcode1 = getcode(x1,y1);

}else{

x2 = x;

y2 = y;

outcode2 = getcode(x2,y2);

cout<<"After clipping:";

if(accept)

cleardevice();

rectangle(xmin,ymin,xmax,ymax);

setcolor(RED);

line(x1,y1,x2,y2);
getch();

closegraph();

INPUT:

Enter Enter windows min and max values: 60

50

20

40

Enter the endpoints of the line: 55

55

77

--------------------------------

Process exited after 58.67 seconds with return value 0

Press any key t0 continue . . .

You might also like