-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPOLYGONS.CPP
67 lines (66 loc) · 1.5 KB
/
POLYGONS.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct Point{
int x,y;
};
int isInside(Point poly[],int n,Point test)
{int i,j,c=0;
for(i=0;i<n;i++)
{j=(i+1)%n;
//check whether point lie on vertex
if((test.x==poly[j].x&&test.y==poly[j].y)||(test.x==poly[i].x&&test.y==poly[i].y))
{c=1;
break;
}
//check by slope comparing
if(poly[j].y!=poly[i].y)
{if((poly[i].y>test.y)!=(poly[j].y>test.y))
if(test.x<(poly[j].x-poly[i].x)*(test.y-poly[i].y)/(poly[j].y-poly[i].y)+poly[i].x)
c=!c;
else if(test.x==(poly[j].x-poly[i].x)*(test.y-poly[i].y)/(poly[j].y-poly[i].y)+poly[i].x)
{c=1;
break;
}
} //special case for horizontal lines
else
{
if((poly[i].x>test.x)!=(poly[j].x>test.x)&&(poly[i].y==poly[j].y&&poly[i].y==test.y))
{
c=1;
break;
}
}
}
return c;
}
void fillPoly(Point ver[],int n)
{ int xmax=ver[0].x,xmin=ver[0].x,ymax=ver[0].y,ymin=ver[0].y,i,j;
Point p;
for(i=1;i<n;i++)
{if(xmax<ver[i].x)
xmax=ver[i].x;
if(xmin>ver[i].x)
xmin=ver[i].x;
if(ymax<ver[i].y)
ymax=ver[i].y;
if(ymin>ver[i].y)
ymin=ver[i].y;
}
for(j=ymin;j<=ymax;j++)
{for(i=xmin;i<=xmax;i++)
{p.x=i;p.y=j;
if(isInside(ver,n,p))
putpixel(i,j,WHITE);
}
}
}
void main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
Point polygon[]={{50,100},{100,50},{150,100},{100,150},{120,70},{100,90}};
int n=sizeof(polygon)/sizeof(polygon[0]);
fillPoly(polygon,n);
getch();
closegraph();
}