Saturday, August 25, 2007

point clipping algorithm in C

point clipping algorithm in C


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

void initgraph1() {
int gd, gm;

gd=DETECT;
initgraph(&gd,&gm,"");
}

void main() {
int xmin, xmax, ymin, ymax;
int x[20],y[20],n,i;
clrscr();
printf("\tProgram for point clipping\n");
printf("\nEnter xmin, xmax, ymin, ymax");

scanf("\n %d", &xmin);
scanf("\n %d", &xmax);
scanf("\n %d", &ymin);
scanf("\n %d", &ymax);
printf("\n Enter no of point to be clipped");
scanf("\n %d",&n);
for(i=0;i<n;i++){
printf("Enter x for point %d :",i+1);
scanf("%d",&x[i]);
printf("\nEnter y for point %d :",i+1);
scanf("%d",&y[i]);
}

initgraph1();
/*before clipping */
outtextxy(0,0, "BEFORE CLIPPING");
rectangle(xmin, ymin, xmax, ymax);
for(i=0;i<n;i++) {
putpixel(x[i],y[i],1);
}
outtextxy(50,50, "presss a key to continue");
getch();
/*after clipping */
cleardevice();
outtextxy(0,0, "AFTER CLIPPING");
rectangle(xmin, ymin, xmax, ymax);
for(i=0;i<n;i++) {
if ((x[i] >= xmin && x[i] <=xmax) ( y[i] >=ymin && y[i] <=ymax)) {
putpixel(x[i],y[i],1);

}
else {
/*clipping */

}
}

getch();
closegraph();

}

No comments: