/* x, y为正多边形的中心坐标 */
/* lng为正多边形的边长 */
/* cnt为多边形的边数 */
/* 返回值可供drawpoly()函数使用 */
int *polygon(int x, int y, unsigned cnt, unsigned lng)
{
double art;
const double PI = 3.1415926f;
unsigned R=(int)(lng >> 1)/sin(PI / cnt);
unsigned i;
int *points;
if (cnt < 2 || (points = malloc(cnt << 1)) == NULL)
{
return NULL;
}
if(cnt & 1) /*判断正多边形边数的奇偶*/
{
points[0] = 0 + x;
points[1] = R + y;
art = PI / 2.0f;
}
else
{
art = PI / 2.0f - PI / cnt;
points[0] = (int)(R * cos(art)) + x;
points[1] = (int)(R * sin(art)) + y;
}
for(i = 0; i < ((cnt - 1) << 1); i += 2)
{
art -= (PI * 2.0f) / cnt;
points[2 + i] = (int)(R * cos(art)) + x;
points[3 + i] = (int)(R * sin(art)) + y;
}
return points;
}
/*你可以用这段代码返回一个包含所要多边形顶点坐标的缓冲*/