CS2405 Anna University lab manual Cohen Sutherland 2D line clipping and Windowing - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Monday, June 20, 2011

CS2405 Anna University lab manual Cohen Sutherland 2D line clipping and Windowing


COHEN-SUTHERLAND CLIPPING

AIM:
       To implement Cohen-Sutherland clipping Algorithm.

FUNCTIONS USED:

Line()
      The function line () is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
        line (x1,y1,x2,y2)

initgraph().
       This function takes thee arguments and they are
         i).the video driver to be used (gd).
         ii).the graphics mode (gm).
         iii).the path name.

Syntax:
       Initgraph(gd,gm,path)

Setcolor().
       This function changes the drawing colour.

Syntax:
      Setcolor(value of the color)

Settextstyle().
The function settextstyle() is used to change the style of the text.

Syntax:
Settextstyle(font,direction,charsize)
 
Where font is the constant value or the font filename, direction is the number either 0 or 1, which makes the output to display in horizontal, or in vertical direction, charsize is the character size or magnification factor and it varies from 1 to 10.

Outtext().
This function display a text message on upper left of the screen

Syntax:
    Outtext(“message”);


ALGORITHM:

Step 1: Create a class sulc with functions drawwindow, drawline, setcode, visibility and
            reset endpoint.
Step 2: Using the function line set the parameters to draw window.
Step 3: Using the function defined in class sulc, setcode is used to save the line inside the
            window and to the line outside the window.
Step 4: Using the function visibility
            i).check the code to know the points inside or outside the window.
            ii).if the code value is zero the point is inside the window.
Step 5: Using the function reset end point
            i). if the code value for the line is outside the window.
            ii).reset the endpoint to the boundary of the window.
Step 6: Initialize the graphics functions
Step 7: Declare the variables x1, x2, y1, y2 of array type.
Step 8: Get the value of two endpoints x1, y1 and x2, y2 to draw the line.
Step 9: Using the object c, display the window before clipping.
Step 10: Using the function setcode, visibility display the clipped window only with lines  
              inside the window class was displayed after clipping.

















PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
typedef struct coord
{
int x,y;
char code[4];
}pt;
class sulc
{
public:
void drawwindow();
void drawline(pt p1,pt p2,int c1);
pt setcode(pt p);
int visibility(pt p1,pt p2);
pt resetendpt(pt p1,pt p2);
};
void sulc::drawwindow()
{
setcolor(WHITE);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void sulc::drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
pt sulc::setcode(pt p)
{
pt ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
 if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.y>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.y<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int sulc::visibility(pt p1,pt p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1'))
flag=0;}
if(flag==0)
return(1);
return(2);
}
pt sulc::resetendpt(pt p1,pt p2)
{
pt temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
else
return(p1);
}
void main()
{
int gd=DETECT,gm,v;
sulc c1;
pt p1,p2,ptemp;
initgraph(&gd,&gm,"");
int x1[10],y1[10],x2[10],y2[10];
cleardevice();
int i,n;
settextstyle(4,0,4);
outtext("cohen sutherland line clipping");
cout<<"\n\n enter the no.of lines:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\n enter end-point1(x1,y1):";
cin>>x1[i]>>y1[i];
cout<<"\n\n enter end-point2(x2,y2):";
cin>>x2[i]>>y2[i];
}
cleardevice();
settextstyle(0,0,3);
outtext("before clipping");
c1.drawwindow();
for(i=0;i<n;i++)
{
p1.x=x1[i];
p1.y=y1[i];
p2.x=x2[i];
p2.y=y2[i];
c1.drawline(p1,p2,15);
}
getch();
cleardevice();
settextstyle(0,0,3);
outtext("after clipping");
for(i=0;i<n;i++)
{
p2.x=x2[i];
p2.y=y2[i];
p1=c1.setcode(p1);
p2=c1.setcode(p2);
v=c1.visibility(p1,p2);
switch(v)
{
case 0:
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
case 1:
c1.drawwindow();
break;
case 2:
p1=c1.resetendpt(p1,p2);
p2=c1.resetendpt(p2,p1);
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
}
}
getch();
closegraph();
}









INPUT:

Enter the no.of lines:    1
Enter end-point1(x1,y1):30 40
Enter end-point1(x2,y2):300 400

OUTPUT:

Before clipping

 






After clipping









RESULT:
                         Thus the program is executed and verified.



No comments:

Post a Comment