计算机图形学平时作业3

要用中点划线法和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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值