#include "stdio.h"
#include <math.h>
#include "conio.h"
#include<graphics.h>
typedef struct
{
float x;
float y;
}Vertex;
typedef Vertex Edge[2];
typedef Vertex VertexArray[20];
int Inside(testpt,ClipBoundary)
Vertex testpt;
Edge ClipBoundary;
{
if(ClipBoundary[1].x> ClipBoundary[0].x)
if(testpt.y<= ClipBoundary[0].y)
return 1;
else if(ClipBoundary[1].x< ClipBoundary[0].x)
if(testpt.y>= ClipBoundary[0].y)
return 1;
else if(ClipBoundary[1].y> ClipBoundary[0].y)
if(testpt.x>= ClipBoundary[0].x)
return 1;
else if(ClipBoundary[1].y< ClipBoundary[0].y)
if(testpt.x<= ClipBoundary[0].x)
return 1;
return 0;
}
int outside(testpt,ClipBoundary)
Vertex testpt;
Edge ClipBoundary;
{
if(ClipBoundary[1].x> ClipBoundary[0].x)
if(testpt.y<= ClipBoundary[0].y)
return 0;
else if(ClipBoundary[1].x< ClipBoundary[0].x)
if(testpt.y>= ClipBoundary[0].y)
return 0 ;
else if(ClipBoundary[1].y> ClipBoundary[0].y)
if(testpt.x>= ClipBoundary[0].x)
return 0;
else if(ClipBoundary[1].y< ClipBoundary[0].y)
if(testpt.x<= ClipBoundary[0].x)
return 0;
else
return 1;
}
int xiangjiao(s,p,ClipBoundary)
Vertex s,p;
Edge ClipBoundary;
{
if(ClipBoundary[0].x==ClipBoundary[1].x)
{ if ((s.x<ClipBoundary[0].x)&&(p.x>ClipBoundary[0].x))
return 1;
else if ((p.x<ClipBoundary[0].x)&&(s.x>ClipBoundary[0].x))
return 1;
}
else if (ClipBoundary[0].y==ClipBoundary[1].y)
{if((s.y>ClipBoundary[0].y)&&(p.y<ClipBoundary[0].y))
return 1;
else if ((p.y>ClipBoundary[0].y)&&(s.y<ClipBoundary[0].y))
return 1;
}
return 0;
}
Vertex Intersect(s,p,ClipBoundary)
Vertex s,p;
Edge ClipBoundary;
{
Vertex IntersectPt;
if(ClipBoundary[0].y==ClipBoundary[1].y)
{ IntersectPt.y =ClipBoundary[0].y;
IntersectPt.x =s.x+(ClipBoundary[0].y -s.y)*(p.x - s.x)/(p.y - s.y);
}
if(ClipBoundary[0].x==ClipBoundary[1].x)
{
IntersectPt.x =ClipBoundary[0].x;
IntersectPt.y =s.y+(ClipBoundary[0].x - s.x)*(p.y - s.y)/(p.x - s.x);
}
return IntersectPt;
}
void main()
{
VertexArray InVertexArray,A,K;
Edge ClipBoundary;
int Inlength,j,i,k,driver,mode;
Vertex s, p;
Inlength=4;
driver=DETECT;
mode=0;
initgraph(&driver,&mode,"");
setcolor(20);
s.x=50;
s.y=150;
InVertexArray[0].x=50;
InVertexArray[0].y=150;
InVertexArray[1].x=150;
InVertexArray[1].y=150;
InVertexArray[2].x=50;
InVertexArray[2].y=180;
InVertexArray[3].x=50;
InVertexArray[3].y=150;
for(i=1;i<=4;i++)
{
switch(i){
case 1:ClipBoundary[0].x=100;
ClipBoundary[0].y=100;
ClipBoundary[1].x=100;
ClipBoundary[1].y=200;break;
case 2:ClipBoundary[0].x=100;
ClipBoundary[0].y=200;
ClipBoundary[1].x=300;
ClipBoundary[1].y=200;break;
case 3:ClipBoundary[0].x=300;
ClipBoundary[0].y=200;
ClipBoundary[1].x=300;
ClipBoundary[1].y=100; break;
case 4:ClipBoundary[0].x=300;
ClipBoundary[0].y=100;
ClipBoundary[1].x=100;
ClipBoundary[1].y=100;break;
default: break;
}
for(j =1;j<Inlength;j++)
{
p=InVertexArray[j];
if (xiangjiao(s,p,ClipBoundary))
{ if(Inside(p,ClipBoundary))
InVertexArray[j-1]=Intersect(s, p, ClipBoundary);
if(Inside(s,ClipBoundary))
InVertexArray[j]=Intersect(s, p, ClipBoundary);
}
s=p;
}
}
printf("%f\n",InVertexArray[0].x);
printf("%f\n",InVertexArray[0].y);
printf("%f\n",InVertexArray[1].x);
printf("%f\n",InVertexArray[1].y);
printf("%f\n",InVertexArray[2].x);
printf("%f\n",InVertexArray[2].y);
getch();
}