/*- Line clipping using cohen sutherland algo -*/
/*------------------------------------------------------------*/

#include<stdio.h>
#include<graphics.h>
#include<conio.h>

typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
int calcode(float,float,float,float,float,float);
void lineclip(float x0,float y0,float x1,float y1,float xwmin,float ywmin,float xwmax,float ywmax )
// float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{

int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}
/*--------------------------------------------------------------------*/

int calcode (float x,float y,float xwmin,float ywmin,float xwmax,float ywmax)
// float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;

if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;

return(code);
}

/*-------------------------------------------------*/

main()
{

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd,gm;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);

printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);

line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();

}

/***************** COHEN-SUTHERLAND LINE CLIPPING ***************/

Home      |      About Us      |     Portfolio      |      Services      |      Courses      |     Career      |      Cost      |      Contact Us
Services
  SEO   
  Link Building
  Data Entry
  Web Design and development
  Web Application development
 

Graphic and multimedia

  E-Commerce Solutions
  Software development
  Web Marketing & Promoting
  Internet Marketing
  IT Outsourcing
  Multimedia/CD Presentation
  Synopsis Ideas
  Project Ideas
  Placement Papers
  Live Project Training
  Programming & Projects
  Faq
  Training @balujalabs
 
 
Method
 

Write a program to Implement CotenSuther Land clipping algorithm?

 

 

 

 

 

 

 

 

 

                                                                                                                                          

1 Write a program to draw a line using DDA line algorithm?
2 Write a program to draw a line using Bresonhams line drawing algorithm?
3 Write a program to draw a circle using Bresenham circle drawing algorithm?
4 Write a program to draw a circle using Midpoint circle drawing algorithm.
5 Write a program to draw a ellipse using Midpoint circle drawing algorithm.
6 Write a program to Implement Translation?
7 Write a program to Implement Rotation?
8 Write a program to Implement Shearing?
9 Write a program to Implement CotenSuther Land clipping algorithm?
10 Write a program to implement polygon clipping?