Line Clipping Algorithm Soruce Code In C Program

To write a C program to implement line clipping.Source Code Algorithm Step By Step Procedure
1. Get the minimum and maximum coordinates of both window and view port.
2. Get two end points of a line.
3. If the given line lies within boundary display it
4. If the given line is neither inside nor outside the boundary, find point of intersection and clip the regions that lie outside the boundary.
5. Display the line after clipping.
Source Code Programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define LEFT_EDGE 0x1
#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8
#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))
#define FALSE 0
#define TRUE 1
struct point
{
int x;
int y;
}
p1,p2,tmp;
struct win
{
int x;
int y;
}
winmin,winmax;
unsigned char encode(struct point pt,struct win winmin,struct win winmax)
{
unsigned char code=0x00;
if(pt.x<winmin.x)
code=code|LEFT_EDGE;
if(pt.x>winmax.x)
code=code|RIGHT_EDGE;
if(pt.y<winmin.y)
code=code|BOTTOM_EDGE;
if(pt.y>winmax.y)
code=code|TOP_EDGE;
return(code);
}
void swappts(struct point *p1,struct point *p2)
{
tmp=*p1;
*p1=*p2;
*p2=tmp;
}
void swapcodes(unsigned char *c1,unsigned char *c2)
{
unsigned char tmp;
tmp=*c1;
*c1=*c2;
*c2=tmp;
}
void clipline(struct win winmin, struct win winmax,struct point p1,struct point p2)
{
unsigned char code1,code2;
int done=FALSE,draw=FALSE;
float m;
while(!done)
{
code1=encode(p1,winmin,winmax);
code2=encode(p2,winmin,winmax);
if(ACCEPT(code1,code2))
{
done=TRUE;
draw=TRUE;
}
else if(REJECT(code1,code2))
done=TRUE;
else
{
if(INSIDE(code1))
{
swappts(&p1,&p2);
swapcodes(&code1,&code2);
}
if(p2.x!=p1.x)
m=(p2.y-p1.y)/(p2.x-p1.x);
if(code1 &LEFT_EDGE)
{
p1.y+=(winmin.x-p1.x)*m;
p1.x=winmin.x;
}
else if(code1 &RIGHT_EDGE)
{
p1.y+=(winmax.x-p1.x)*m;
p1.x=winmax.x;
}
else if(code1 &BOTTOM_EDGE)
{
if(p2.x!=p1.x)
p1.x+=(winmin.y-p1.y)/m;
p1.y=winmin.y;
}
else if(code1 &TOP_EDGE)
{
if(p2.x!=p1.x)
p1.x+=(winmax.y-p1.y)/m;
p1.y=winmax.y;
}
}
}
if(draw)
line(p1.x,p1.y,p2.x,p2.y);
}
void main()
{
int c,gm,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the window minimum coordinates");
scanf("%d%d",&winmin.x,&winmin.y);
printf("Enter the window max coordinates");
scanf("%d%d",&winmax.x,&winmax.y);
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
printf("Enter the starting point");
scanf("%d%d",&p1.x,&p1.y);
printf("enter the end point");
scanf("%d%d",&p2.x,&p2.y);
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
line(p1.x,p1.y,p2.x,p2.y);
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
printf("\n1.line clipping 2.exit");
scanf("%d",&c);
switch(c)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
clipline(winmin,winmax,p1,p2);
break;
case 2:
exit(0);
}
getch();
}

Result Output Example

Enter the minimum coordinates
200
200

Enter the maximum coordinates
400
400
Enter the starting point
150
150

Enter the Ending point
300
300

1.Line clipping
2.Exit

Enter the minimum coordinates
200
200

Enter the maximum coordinates
400
400

Enter the starting point
250
250

Enter the Ending point
350
350

1.Line clipping
2.Exit
RESULT Thus the c program to implement line clipping was coded and executed successfully.

Post a Comment

0 Comments