Saturday, August 25, 2007

textclip / lineclip /polygon clip program in C

textclip / lineclip/ polygon clip program in C
================= =========== =========

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

#define MIN 100
#define MAX 300
#define STRINGS 3
#define Char_Width 8
#define Char_Height 10

void draw_basic();
void text_clipping_1 ();
void text_clipping_2 ();

struct textstring
{
int x;
int y;
char string[10];
}s[STRINGS];

void main()
{
int gd=0, gm;
initgraph ( &gd, &gm, "c:\\tc\\bgi" );
draw_basic(); text_clipping_1(); getch();
draw_basic(); text_clipping_2(); getch();
closegraph();
}
void draw_basic()
{
int i;
s[0].x=77; s[0].y=175; strcpy(s[0].string,"HELLO");
s[1].x=50; s[1].y=50; strcpy(s[1].string,"HELLO");
s[2].x=150; s[2].y=150; strcpy(s[2].string,"HELLO");
cleardevice();
rectangle(MIN,MIN,MAX,MAX);
for ( i=0; i<STRINGS; i++ )
outtextxy(s[i].x,s[i].y,s[i].string);
getch();
}
void text_clipping_1 ()
{
int i, length;
cleardevice();
rectangle(MIN,MIN,MAX,MAX);
for ( i=0; i<STRINGS; i++ )
{ if ( (s[i].y<MIN) (s[i].y>MAX) )
continue;
length = strlen(s[i].string) * Char_Width;
if ( (s[i].x<MIN) (s[i].x>MAX) )
continue;
outtextxy(s[i].x,s[i].y,s[i].string);
}
outtextxy(10,10,"Clipping 1");
}
void text_clipping_2 ()
{
int i, length;
cleardevice();
rectangle(MIN,MIN,MAX,MAX);
for ( i=0; i<STRINGS; i++ )
{ if ( (s[i].y<MIN) (s[i].y>MAX) )
continue;
length = strlen(s[i].string) * Char_Width;
if ( (s[i].x+length<MIN) (s[i].x>MAX) )
continue;
if ( (s[i].x+length>MIN) && (s[i].x<MIN) )
{ int words,ii,jj;
char newstring[10];
words = ( MIN - s[i].x ) / Char_Width;
for ( ii=(strlen(s[i].string)-words),jj=0; ii<=strlen(s[i].string); ii++)
newstring[jj++] = s[i].string[ii];
newstring[jj] = '\0';
outtextxy(MIN+1,s[i].y,newstring);
continue;
}
outtextxy(s[i].x,s[i].y,s[i].string);
}
outtextxy(10,10,"Clipping 2");
}


lineclip
======



#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <process.h>
#include <math.h>
#define ML 5
int Xmin,Xmax,Ymin,Ymax, ii[4][2]={0};
int l[5][4]={ {10,150,175,200},
{225,50,250,200},
{500,400,200,175},
{150,150,250,250},
{10,400,475,180}};
void dr();
void dl();
void cs();
void drawcsline(int, int, int, int, int);
void main()
{
int gd=0, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");
dr(); getch();
dl(); getch();
cs(); getch();
closegraph();
}
void dr()
{ Xmin = 100; Xmax = 400; Ymin = 100; Ymax = 300;
rectangle(Xmin,Ymin,Xmax,Ymax);
}
void dl()
{ int i;
for(i=0; i<ML; i++)
line(l[i][0],l[i][1],l[i][2],l[i][3]);
}
void cs()
{ int i, j, x1, y1, x2, y2;
for(i=0; i<ML; i++)
{
x1=l[i][0]; y1=l[i][1]; x2=l[i][2]; y2=l[i][3];
drawcsline(x1,y1,x2,y2,i);
}
cleardevice(); dr(); dl(); getch();
}
void drawcsline(int x1, int y1, int x2, int y2, int iii)
{ int pt1, pt2, k, finalx, finaly, minx, miny, maxx, maxy, checkxy(int, int);
void setintersection(int, int, int, int);
minx = ( x1 > x2 ) ? x2 : x1;
miny = ( y1 > y2 ) ? y2 : y1;
maxx = ( x1 > x2 ) ? x1 : x2;
maxy = ( y1 > y2 ) ? y1 : y2;
pt1 = checkxy(x1,y1);
pt2 = checkxy(x2,y2);
if ( pt1 == 0 && pt2 == 0 ) //both inside
return;
if ( pt1 == 0 && pt2 != 0 ) //only pt1 inside
{ setintersection(x1,y1,x2,y2);
for ( int k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
break;
else
continue;
}
l[iii][2] = finalx;
l[iii][3] = finaly;
}
if ( pt1 != 0 && pt2 == 0 ) //only pt2 inside
{ setintersection(x1,y1,x2,y2);
for ( int k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
break;
else
continue;
}
l[iii][0] = finalx;
l[iii][1] = finaly;
}
if ( pt1 != 0 && pt2 != 0 ) //both outside
{ setintersection(x1,y1,x2,y2);
for ( k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
break;
else
continue;
}
l[iii][0] = finalx;
l[iii][1] = finaly;
for ( k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > Xmin && finalx < Xmax ) && ( finaly > Ymin && finaly < Ymax ) )
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
if ( finalx != l[iii][0] &&amp;amp; finaly != l[iii][1] )
break;
else
continue;
}
l[iii][2] = finalx;
l[iii][3] = finaly;

}
}
void setintersection(int x1, int y1, int x2, int y2)
{ int xxmin = 100, yymin = 100, xxmax = 400, yymax = 300;
float slope = (y2-y1)* 1.0 / (x2-x1);
ii[0][0] = x1 + ( yymin - y1 ) / slope;
ii[0][1] = yymin;
ii[1][0] = x1 + ( yymax - y1 ) / slope;
ii[1][1] = yymax;
ii[2][0] = xxmin;
ii[2][1] = y1 + slope * ( xxmin - x1 );
ii[3][0] = xxmax;
ii[3][1] = y1 + slope * ( xxmax - x1 );
}
int checkxy(int x, int y)
{ /* Check where x1,y1 lies Above Below Right Left
1 0 0 1
1001 1000 1010
--------------------
0001 0000 0010
-------------------
0101 0100 0110 */
int a,b,r,l;
a = ( y < Ymin ) ? 1 : 0;
b = ( y > Ymax ) ? 1 : 0;
r = ( x > Xmax ) ? 1 : 0;
l = ( x < Xmin ) ? 1 : 0;
return ( a*1000 + b*100 + r*10 + l );
}


Polygon Slip

//polygon clipping...
#include <graphics.h>
#include <conio.h>
#include <string.h>

int minx, miny, maxx, maxy;

int mypoly[22] = {200,50,250,100,300,100,250,150,250,200,200,150,
150,200,150,150,100,100,150,100,200,50};
int newpoly[50];
int cutx, cuty;

void clippoly(int, int []);
void set_intersection(char *, int, int, int, int);

void main()
{
int gd=0, gm;
initgraph (&gd, &gm,"c:\\tc\\bgi");

minx = 125; miny = 75; maxx=275; maxy=175;

rectangle(minx, miny, maxx, maxy);
getch();
drawpoly(11,mypoly);
getch();

clippoly(11, mypoly);
getch();

closegraph();
}

void clippoly(int edges, int mypoly[])
{
int temppoly[40];
int i, counter=0;
for (i=0; i<edges*2; i+=2)
{
// left clip...
if ( mypoly[i] > minx )
{
temppoly[counter++] = mypoly[i];
temppoly[counter++] = mypoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("left",mypoly[i],mypoly[i+1],mypoly[edges*2-4],mypoly[edges*2-3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("left",mypoly[i],mypoly[i+1],mypoly[i+2],mypoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
else
{
set_intersection("left",mypoly[i-2],mypoly[i-1],mypoly[i],mypoly[i+1]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("left",mypoly[i],mypoly[i+1],mypoly[i+2],mypoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
}
}
edges = counter/2;
counter = 0;
for (i=0; i<edges*2; i+=2)
{
// right clip...
if ( temppoly[i] < maxx )
{
newpoly[counter++] = temppoly[i];
newpoly[counter++] = temppoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("right",temppoly[i],temppoly[i+1],temppoly[edges*2-4],temppoly[edges*2-3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("right",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
else
{
set_intersection("right",temppoly[i-2],temppoly[i-1],temppoly[i],temppoly[i+1]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("right",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
}
}
edges = counter/2;
counter = 0;
for (i=0; i<edges*2; i+=2)
{
// bottom clip...
if ( newpoly[i+1] < maxy )
{
temppoly[counter++] = newpoly[i];
temppoly[counter++] = newpoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[edges*2-4],newpoly[edges*2-3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[i+2],newpoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
else
{
set_intersection("bottom",newpoly[i-2],newpoly[i-1],newpoly[i],newpoly[i+1]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[i+2],newpoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
}
}
edges = counter/2;
counter = 0;
for (i=0; i<edges*2; i+=2)
{
// top clip...
if ( temppoly[i+1] > miny )
{
newpoly[counter++] = temppoly[i];
newpoly[counter++] = temppoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("top",temppoly[i],temppoly[i+1],temppoly[edges*2-4],temppoly[edges*2-3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("top",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
else
{
set_intersection("top",temppoly[i-2],temppoly[i-1],temppoly[i],temppoly[i+1]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("top",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
}
}
cleardevice();
rectangle(minx, miny, maxx, maxy);
setcolor(2);
drawpoly(counter/2, newpoly);
setcolor(15);
getch();
drawpoly(11,mypoly);
}

void set_intersection(char * clipside, int x1, int y1, int x2, int y2)
{
float slope;
if ( x1 != x2 )
slope = (y2-y1)*1.0/(x2-x1);

if ( strcmp(clipside,"left")==0 )
{ cutx = minx; cuty = y1 + slope * ( minx - x1 ); }
if ( strcmp(clipside,"right")==0 )
{ cutx = maxx; cuty = y1 + slope * ( maxx - x1 ); }
if ( strcmp(clipside,"bottom")==0 )
{
if ( x1 != x2 )
cutx = x1 + ( maxy - y1 ) / slope;
else
cutx = x1;
cuty = maxy;
}
if ( strcmp(clipside,"top")==0 )
{
if ( x1 != x2 )
cutx = x1 + ( miny - y1 ) / slope;
else
cutx = x1;
cuty = miny;
}
}

3 comments:

Anonymous said...

thanx for the code..
but where do i get the graphics.h header file for this code????
pl help...
thanx..

amazingly simple code though

Amandeep F Jiddewar said...

You need to improve it by atleast
giving comments to necessary Variables

Anonymous said...

thank u so much..