要用中点划线法和bresenham算法画个五角星并填色,我不知道怎么填色就直接在下面用solidpolygon画了个无边框五角星。
#include <graphics.h> // 引用图形库头文件
#include <conio.h>
#include <math.h>
int Sign(int x)
{
if(x<0) return -1;
else if(x==0) return 0;
else return 1;
}
void MPLine(int x1,int y1,int x2,int y2,int color)
{
int x,y,a,b,d1,d2,d,i,s1,s2,temp,swap;
a=-abs(y2-y1); b=abs(x2-x1);//斜率
x=x1; y=y1;//起始点赋值
s1=Sign(x2-x1); s2=Sign(y2-y1);//判断方向
if(-a>b)//斜率1-n
{ temp=b;b=-a;a=-temp;swap=1;}//ab基准交换
else swap=0;//斜率0-1
//增量
d=2*a+b;//开始
d1=2*a;//正右
d2=2*(a+b);//右上
putpixel(x,y,color);//起始点画
for(i=0;i<=b;i++)//以b为基准
{
if(swap==1) y=y+s2;
else x=x+s1;
if(d<0)//取右上方
{
if(swap==1) x=x+s1;
else y=y+s2;
d+=d2;
}
else (d+=d1);
putpixel(x,y,color);
}
}
void BHLine(int x1,int y1,int x2,int y2,int color)
{
int x,y,dx,dy,dk,i,s1,s2,temp,swap;
dx=abs(x2-x1); dy=abs(y2-y1); //斜率
x=x1;y=y1;
s1=Sign(x2-x1); s2=Sign(y2-y1);
if(dy>dx)//斜率1-n
{ temp=dx;dx=dy;dy=temp;swap=1;}//ab基准交换
else swap=0;//斜率0-1
dk=2*dy-dx;
for(i=1;i<=dx;i++)
{
putpixel(x,y,color);
if(swap=1) y=y+s2;
else x=x+s1;
dk=dk+2*dy;
if(dk>=0)
{
if(swap==1) x=x+s1;
else y=y+s2;
dk=dk-2*dx;
}
}
}
int main()
{
initgraph(200, 200); // 创建绘图窗口
setbkcolor(WHITE);//白色背景
cleardevice();//刷新窗口
//setorigin(320,320);
POINT points[] ={{100,0},{41,181},{195,69},{5,69},{159,181}};
setfillcolor(YELLOW);
solidpolygon(points,5);
BHLine(100,0,41,181,BLACK);
MPLine(41,181,195,69,BLACK);
MPLine(195,69,5,69,BLACK);
MPLine(5,69,159,181,BLACK);
BHLine(159,181,100,0,BLACK);
getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
return 0;
}