线型算法(计算机图形学)

#include<graphics.h>
#include<conio.h>

//Bresenham画线算法
void BresenhamLine(int x0,int y0,int x1,int y1,int color){
    int x,y,dx,dy,e;
	dx=x1-x0;
	dy=y1-y0;
	e=-dx;
	x=x0;
	y=y0;
    for(int i=0;i<=dx;i++){
	     putpixel(x,y,color);
		 x=x+1;
		 e=e+2*dy;
		 if(e>=0){
		    y=y+1;
			e=e-2*dx;
		 }
	}
}

//画短横线
void ShortLine(int x0,int y0,int x1,int y1,int color){

	  int x,y,dx,dy,e;
	  int linkmark[12]={1,1,1,1,1,1,1,1,1,0,0,0};
		dx=x1-x0;
		dy=y1-y0;
		e=-dx;
		x=x0;
		y=y0;
		for(int i=0;i<=dx;i++){
			if(linkmark[i%12]==1){
			   putpixel(x,y,color);
			}
			 x=x+1;
			 e=e+2*dy;
			 if(e>=0){
				y=y+1;
				e=e-2*dx;
			 }
		}
}

//画点横线
void PointShortLine(int x0,int y0,int x1,int y1,int color){
		int x,y,dx,dy,e;
		dx=x1-x0;
		dy=y1-y0;
		e=-dx;
		x=x0;
		y=y0;
		int linkmark[20]={1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0};
		for(int i=0;i<=dx;i++){
			if(linkmark[i%20]==1){
			   putpixel(x,y,color);
			}
			 x=x+1;
			 e=e+2*dy;
			 if(e>=0){
				y=y+1;
				e=e-2*dx;
			 }
		}
}

// 中点画圆法
void Circle_Midpoint(int x, int y, int r, int color,int flag)
{
	int tx = 0, ty = r, d = 1 - r;
	while(tx <= ty)
	{
		// 利用圆的八分对称性画点
		if(flag==0){
		   	putpixel(x + tx, y + ty, color);
		    putpixel(x - tx, y + ty, color);
			putpixel(x + ty, y + tx, color);
			putpixel(x - ty, y + tx, color);
		}
		else{
		  	putpixel(x + tx, y - ty, color);
		    putpixel(x - tx, y - ty, color);
		    putpixel(x + ty, y - tx, color);
		    putpixel(x - ty, y - tx, color);
		}

		if(d < 0)
			d += 2 * tx + 3;
		else
			d += 2 * (tx - ty) + 5, ty--;

		tx++;
	}
}

//画波浪线
void WaveLine(int x0,int y0,int x1,int y1,int color){

        //初始化图符即半圆的信息
	    int flag=1;
		int r=2;
		int dx=x1-x0;
		int y=y0;
		int x=x0+r;
		while(x<=dx){
			if(flag==1){
			   Circle_Midpoint(x,y,r,RED,1);
			   flag=0;
			}
			else{
			   Circle_Midpoint(x,y,r,RED,0);
			   flag=1;
			}
			x+=2*r;
		}
}

void main(){

    int gdriver=DETECT, gmode;
	initgraph(&gdriver,&gmode,"");//根据测试结果初始化图形界面

	//画直线
	BresenhamLine(10,80,500,80,RED);

	//画短横线
	ShortLine(10,120,500,120,RED);

    //画点横线
	PointShortLine(10,160,500,160,RED);

	//画波浪线
    WaveLine(10,200,500,200,RED);


   // 按任意键退出
	getch();
	closegraph();
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值