📄 逐点判断方法.c
字号:
/**********************************************************************
This is a program of filling a Polygon Area Point by Point
CopyRights 2002 By LiLi.
**********************************************************************/
#include "time.h"
#include "graphics.h"
#define MAX 100
typedef enum {FALSE,TRUE}Boolean;
typedef struct { int x;
int y;
}Point;
typedef struct { int PolygonNum;
Point verteces[MAX];
}Polygon;
clock_t starttime,finishtime;
double timeused;
void FillPolygonPbyP(Polygon *P,int polygonColor,int backgroundColor)
{int x,y;
int i;
int gdriver=DETECT,gmode;
int Height,Width;
Boolean IsInside(Polygon *,int,int);
initgraph(&gdriver,&gmode,"");
Height=getmaxy();
Width=getmaxx();
starttime=clock(); /* start record time */
for(y=0;y<=Height;y++) /* Judge Point By Point */
for(x=0;x<=Width;x++)
if(IsInside(P,x,y))
putpixel(x,Height-y,polygonColor);
else
putpixel(x,Height-y,backgroundColor);
finishtime=clock(); /* stop record time */
/* getch(); */
closegraph();
}
Boolean IsInside(Polygon *P,int x,int y)
{int i;
int sum=0; /* sum is used to record the Code of each edge */
int Coding(float,float,float,float,int,int);
Boolean IsInEdge(int,int,int,int,int,int);
/* Deal with the Points On the Polygon */
for(i=0;i<P->PolygonNum;i++)
if(IsInEdge(P->verteces[i].x,P->verteces[i].y,P->verteces[i+1].x,
P->verteces[i+1].y,x,y))
return TRUE;
for(i=1;i<=P->PolygonNum;i++)
sum+=Coding(P->verteces[i-1].x,P->verteces[i-1].y,
P->verteces[i].x,P->verteces[i].y,x,y);
if(sum==0)
return FALSE;
else
return TRUE;
}
Boolean IsInEdge(int x0,int y0,int x1,int y1,int x,int y)
{
if(x0==x1&&x0==x) /* when it's a vertical line */
{if((y>=y0&&y<=y1)||(y<=y0&&y>=y1))return TRUE;}
else if((y-y0)*(x1-x0)==(x-x0)*(y1-y0))
{if((x>=x0&&x<=x1)||(x<=x0&&x>=x1))return TRUE;}
return FALSE;
}
int Coding(float x0,float y0,float x1,float y1,int x,int y)
{int code0, code1;
float MidPointx,MidPointy;
if(x0>x&&y0>=y)
code0=0;
else if(x0<=x&&y0>y)
code0=1;
else if(x0<x&&y0<=y)
code0=2;
else
code0=3;
if(x1>x&&y1>=y)
code1=0;
else if(x1<=x&&y1>y)
code1=1;
else if(x1<x&&y1<=y)
code1=2;
else
code1=3;
if(code1-code0==3)
return -1;
else if(code1-code0==-3)
return 1;
else if(code1-code0==1)
return 1;
else if(code1-code0==-1)
return -1;
else if(code1==code0)
return 0;
else
{MidPointx=(x0+x1)/2;
MidPointy=(y0+y1)/2;
return Coding(x0,y0,MidPointx,MidPointy,x,y)+
Coding(MidPointx,MidPointy,x1,y1,x,y);
}
}
main()
{int i;
int Color,bgColor;
Polygon *P;
printf("Please Enter The Vertex Number Of the Polygon.\n");
scanf("%d",&P->PolygonNum);
for(i=0;i<P->PolygonNum;i++)
{printf("The x Coordinate Of the %d Vertex.\n",i);
scanf("%d",&P->verteces[i].x);
printf("The y Coordinate Of the %d Vertex.\n",i);
scanf("%d",&P->verteces[i].y);
}
printf("The Color Of the Plygon Area:\n");
scanf("%d",&Color);
printf("The BackGroundColor Of the Screen.\n");
scanf("%d",&bgColor);
P->verteces[i].x=P->verteces[0].x;
P->verteces[i].y=P->verteces[0].y;
FillPolygonPbyP(P,Color,bgColor);
timeused=(double)(finishtime-starttime)/CLK_TCK;
printf("The Program Used %f seconds.\n",timeused);
getch();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -