//Line-clipping
#include"stdio.h"
#include"math.h"
#include"graphics.h"
#define ROUND(a) (int)(a + 0.5)
#define TOP 1
#define BOT 2
#define RT 4
#define LT 8
void lineclip(int x1,int y1,int x2,int y2);
void ddaline(int xa, int ya, int xb, int yb);
int xmin,xmax,ymin,ymax;
void main ()
{
int cho,x1,y1,x2,y2;
char ch;
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
clrscr();
do{
printf("\nENTER X-COORD. & Y-COORD. OF STARTING POINT OF LINE: ");
scanf("%d %d",&x1,&y1);
putpixel(x1,y1,3); //3 IS COLOR CODE OF CYAN
printf("\nENTER X-COORD. & Y-COORD. OF END POINT OF LINE: ");
scanf("%d %d",&x2,&y2);
putpixel(x2,y2,3);
printf("\nENTER X-min & Y-min OF CLIPPING WINDOW: ");
scanf("%d %d",&xmin,&ymin);
putpixel(xmin,ymin,5); //5 IS COLOR CODE OF RED
printf("\nENTER X-max & Y-max OF CLIPPING WINDOW: ");
scanf("%d %d",&xmax,&ymax);
putpixel(xmax,ymax,5);
clrscr();
ddaline(x1, y1, x2, y2);
delay(500);
ddaline(xmin, ymin, xmax, ymin);
delay(500);
ddaline(xmax, ymin, xmax, ymax);
delay(500);
ddaline(xmax, ymax, xmin, ymax);
delay(500);
ddaline(xmin, ymax, xmin, ymin);
delay(500);
getch();
lineclip(x1,y1,x2,y2);
getch();
printf("\nIF YOU WISH TO CONTINUE PRESS Y/y , ELSE ANY OTHER CHAR TO EXIT: ");
scanf(" %c",&ch);
}while(ch == 'y' || ch == 'Y');
getch();
closegraph();
}
////////////////////////////////////////////////////////////////////////////
void ddaline(int xa, int ya, int xb, int yb)
{
int dx = xb - xa, dy = yb - ya, steps, k ;
float xinc,yinc,x = xa,y = ya;
if (abs (dx) > abs (dy) )
steps = abs(dx);
else
steps = abs(dy);
xinc = dx/(float) steps;
yinc = dy/(float) steps;
putpixel( ROUND(x), ROUND(y),RED);
for(k = 0; k < steps ; k++)
{
x += xinc;
y += yinc;
putpixel( ROUND(x), ROUND(y),BROWN);
}
}
/////////////////////////////////////////////////////////////////////////
int findcode(int x, int y)
{
int code = 0;
if(y > ymax)
code |= TOP;
if(y < ymin)
code |= BOT;
if(x > xmax)
code |= RT;
if(x < xmin)
code |= LT;
return code;
}
/////////////////////////////////////////////////////////////////////////
void lineclip(int x1,int y1,int x2,int y2)
{
int code1,code2,t;
int done = 0,accept = 0;
float m,c;
m = (y2 - y1) / (x2 - x1);
c = y1 - m * x1;
while(!done)
{
code1 = findcode(x1,y1);
code2 = findcode(x2,y2);
if( (code1 | code2) == 0 )
{ accept = 1; done = 1; }
else
{
if( (code1 & code2) != 0 )
{ done = 1; }
else
{
if( code1 == 0 )
{
t = x1; x1 = x2; x2 = t;
t = y1; y1 = y2; y2 = t;
t = code1; code1 = code2; code2 = t;
}
if( (code1 & TOP) != 0)
{ y1 = ymax; x1 = (y1 - c) / m; }
else if( (code1 & BOT) != 0 )
{ y1 = ymin; x1 = (y1 - c) / m; }
else if( (code1 & LT) != 0)
{ x1 = xmin; y1 = m * x1 + c; }
else if( (code1 & RT) != 0)
{ x1 = xmax; y1 = m * x1 + c; }
}
}
}
clrscr();
if(accept)
ddaline(x1, y1, x2, y2);
ddaline(xmin, ymin, xmax, ymin);
delay(500);
ddaline(xmax, ymin, xmax, ymax);
delay(500);
ddaline(xmax, ymax, xmin, ymax);
delay(500);
ddaline(xmin, ymax, xmin, ymin);
delay(500);
}
4 comments:
Hi guys, the header files for every programs can be taken according to operating systems...so be careful about that...because u have to decide that which header files u need for which operating system.
this is for 3d...?
hey....this is for 3d?
@him barot ...its for 2d only...some of the concepts has been changed for 3d which i haven't mentioned here.
Post a Comment