Worksheet 3.1

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

WORKSHEET

3.1
Name– Vatsal Sharma UID-19BCS1762

Branch- CSE Section/group- 5-B

Semester- 5TH Date - 7/11/2021

Subject Name- CG lab

1. Aim/Overview of the practical

a) To display 4-bit region code for endpoints of a line and check


whether a line is completely on the screen or off the screen

b) To clip a line intersecting at one point with a given window


using Cohen Sutherland Line Clipping algorithm.

c) To clip a line intersecting at Two or more points with a given


window using the Cohen Sutherland Line Clipping algorithm

2. Task to be done

Using Turbo c++/dev c++ and graphics.h library

3. Steps of

Experiment a)

#include<iostream>

using namespace

std;

const int INSIDE = 0; //

0000 const int LEFT = 1;

// 0001 const int RIGHT =

2; // 0010
const int BOTTOM = 4; //

0100 const int TOP = 8; //

1000

rectangle const int x_max =

10;

const int y_max =

8;

const int x_min =

4;

const int y_min =

4;

point(x, y) int computeCode(double x, double y)

inside int code = INSIDE;

if (x < x_min)

rectangle code |= LEFT;

else if (x > x_max)

rectangle code |= RIGHT;

if (y < y_min)

rectangle code |= BOTTOM;


else if (y > y_max)

rectangle code |= TOP;

return code;

void cohenSutherlandClip(double x1, double

y1,double x2, double y2)

int code1 = omputeCode(x1,y1);

int code2 = omputeCode(x2,y2);

window bool accept = false;

while (true) {

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

rectangle accept = true;

break;
}

else if (code1 & code2) {

region break;

else {

int ode_out;

double x, y;
if (code1 != 0)

code_out=code1

else

code_out = code2;

if (code_out & TOP) {

x = x1 + (x2 - x1) * (y_max

- y1) / (y2 - y1); y = y_max;

else if (code_out & BOTTOM) {

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

y1); y = y_min;

else if (code_out & RIGHT) {

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

x1); x = x_max;

else if (code_out & LEFT) {

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

x1); x = x_min;

if (code_out ==

code1) { x1 = x;

y1 = y;
code1 = computeCode(x1, y1);

else {

x2=x ;

y2= y;

code2 = computeCode(x2, y2);

if (accept) {

cout << "Line accepted from " << x1 << ", "

<< y1 << " to " << x2 << ", " << y2 << endl;

else

cout << "Line rejected" << endl;

}
int main()

cohenSutherlandClip(5, 5, 7, 7);

cohenSutherlandClip(7, 9, 11, 4);

cohenSutherlandClip(1, 5, 4, 1);
return 0;

b)

#include <iostream>

Foundation class CohenSutherLandAlgo


{

private:

double x1,y1,x2,y2;

double x_max,y_max,x_min,y_min; const int INSIDE = 0; // 0000


const int LEFT = 1; // 0001

const int RIGHT = 2; //

0010 const int BOTTOM =

4; // 0100 const int TOP

= 8; // 1000

public:

CohenSutherLandAlgo()

x1 = 0.0;

x2 = 0.0;

y1 = 0.0;

y2 = 0.0;

void getCoordinates();

void getClippingRectangle();

int generateCode(double x, double

y); void cohenSutherland();

};

void CohenSutherLandAlgo::getCoordinates()

std::cout << "\nEnter Co-ordinates of P1(X1,Y1) of Line

Segment : "; std::cout << "\Enter X1 Co-ordinate : ";


std::cin >> x1;

std::cout << "\Enter Y1 Co-ordinate

: "; std::cin >> y1;

std::cout << "\nEnter Co-ordinates of P2(X2,Y2) of Line

Segment : "; std::cout << "\Enter X2 Co-ordinate : ";

std::cin >> x2;

std::cout << "\Enter Y2 Co-ordinate

: "; std::cin >> y2;

void CohenSutherLandAlgo::getClippingRectangle()

std::cout << "\nEnter the Co-ordinates of Interested

Rectangle."; std::cout << "\nEnter the X_MAX : ";

std::cin >> x_max;

std::cout << "\nEnter the Y_MAX

: "; std::cin >> y_max;

std::cout << "\nEnter the X_MIN

: "; std::cin >> x_min;

std::cout << "\nEnter the Y_MIN

: "; std::cin >> y_min;

int CohenSutherLandAlgo::generateCode(double x, double y)

{
int code = INSIDE; // intially initializing as being

inside if (x < x_min) // lies to the left of rectangle

code |= LEFT;
else if (x > x_max) // lies to the right of

rectangle code |= RIGHT;

if (y < y_min) // lies below the

rectangle code |= BOTTOM;

else if (y > y_max) // lies above the

rectangle code |= TOP;

return code;

void CohenSutherLandAlgo::cohenSutherland()

int code1 = generateCode(x1, y1); // Compute region codes

for P1. int code2 = generateCode(x2, y2); // Compute region

codes for P2.

bool accept = false;

window. while (true)

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

rectangle. accept = true;

break;
}

else if (code1 & code2)

region. break;

else
{

int code_out;

double x, y;

if (code1 != 0)

code_out =

code1; else

code_out = code2;

y = y1 + slope * (x - x1)

x = x1 + (1 / slope) * (y - y1)

if (code_out & TOP)

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

y1); y = y_max;

else if (code_out & BOTTOM)

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

y1); y = y_min;

else if (code_out & RIGHT)

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

x1); x = x_max;

else if (code_out & LEFT)


{

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

x1); x = x_min;

}
if (code_out == code1)

x1= x;

y1= y;

code1 = generateCode(x1, y1);

else

x2= x;

y2= y;

code2 = generateCode(x2, y2);

if (accept)

std::cout <<"Line accepted from " <<"("<< x1 << ", "

<< y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" <<
std::endl;

else

std::cout << "Line rejected" << std::endl;


}
int main()

CohenSutherLandAlgo

c; c.getCoordinates();

c.getClippingRectangl

e();

c.cohenSutherland();

return 0;

c)

#includ<iostream.h>

#include <conio.h>

#includ<graphics.h>

#include <dos.h>

class data

int gd, gmode, x, y,

xmin,ymin,ymax,xmax; int a1,a2;

float x1,

y1,x2,y2,x3,y3; int xs,

ys, xe, ye;

float

maxx,maxy;

public:

void getdata
();

void find ();

void clip ();

void display (float,

float,float,float);

void checkonof (int);

void showbit (int);

};

void data :: getdata ()

cout<<"Enter the minimum and maximum coordinate of window (x,

y) "; cin >>xmin>>ymin>>xmax>>ymax;

cout<<"Enter the end points of the line to be

clipped"; cin >>xs>>ys>>xe>>ye;

display (xs, ys, xe,ye);

void data :: display (float, xs, float, ys,float xe, float ye)

int gd=DETECT;

initgraph (&gd,&gmode,

""); maxx=getmaxx();

maxy=getmaxy();

line

(maxx/2,0,maxx/2,maxy);

line (0,
maxy/2,maxx,maxy/2);

rectangle

(maxx/2+xmin,maxy/2-ymax,maxx/2+xmax,maxy/2-ymin); line

(maxx/2+xs,maxy/2-ys,maxx/2+xe,maxy/2-ye);

getch();

void data :: find ()

a1=0

a2=0

if ((ys-ymax)>0)

a1+=8;

if

((ymin-ys)>

0) a1+=4;

if

((xs-xmax)

>0) a1+=2;

if

((xmin-xs)>0)

a1+=1;

if

((ye-ymax)>
0) a2+=8;

if

((ymin-ye)>0

) a2+=4;

if

((xe-xmax)

>0) a2+=2;

if

((xmin-xe)

>0) a2+=1;

cout<<"\nThe area code of Ist point is ";


showbit (a1); getch ();
cout <<"\nThe area code of 2nd point

is "; showbit (a2);

getch ();

void data :: showbit (int n)

int i,k, and;

for (i=3;i>=0;i--)

and

=1<<i; k = n?

k ==0?cout<<"0": cout<<"1\"";

void data ::clip()


{

int

j=a1&a2; if

(j==0)

cout<<"\nLine is perfect candidate for

clipping"; if (a1==0)

else

checkonof(a1

);

x2=x1;y2=y1;

if (a2=0)

x3=xe; y3=ye;

else

checkonof

(a2); x3=x1;

y3=y1;

}
xs=x2;

ys=y2;xe=x3;ye=y3;

cout << endl;

display (xs,ys,xe,ye);

cout<<"Line after

clipping"; getch ()

else if ((a1==0) && (a2=0))

cout <<"\n Line is in the visible region"; getch ();


}

void data :: checkonof (int i)

int j, k,l,m;

1=i&1;

x1=0;y1=

0; if

(1==1)

x1=xmin;

y1=ys+ ((x1-xs)/ (xe-xs))*(ye-ys);

j=i&8

; if
(j>0)

y1=ymax;

x1=xs+(y1-ys)/(ye-ys))*(xe-xs);

k=i & 4;

if

(k==1)

{
y1=ymin;

x1=xs+((y1-ys)/(ye-ys))*(xe-xs);

m= i&2;

if

(m==1)

x1=xmax;

y1=ys+ ((x1-xs)/ (xe-xs))*(ye-ys);

main ()

data s;

clrscr()

s.getdata();

s.find();
getch();

closegraph

(); return ();

OUTPU

T:- A)
B)
Learning Outcomes:

1.learn how to use graphics library

2.learn how to make clipping

concept

3.learn how to intersect at Two or more points with the given


window

You might also like