⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bressenhamslinedrawingalgorithm.cpp

📁 Bressenhams Line Drawing Algorithm
💻 CPP
字号:
/* Bressenhams Line Drawing Algorithm */

#include <iostream.h>
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <dos.h>

void bshmLine(int sx,int sy,int ex,int ey)
{
	int x=sx,y=sy,dx,dy,s1,s2;
	int length,dp,temp,swap=0;

	putpixel(sx,sy,WHITE);
	dx=abs(ex-sx);
	dy=abs(ey-sy);

	if(ex<sx) s1=-1;
	else if(ex>sx) s1=1;
	else s1=0;

	if(ey<sy) s2=-1;
	else if(ey>sy) s2=1;
	else s2=0;

	dp=2*dy-dx;
	if(dy>dx)
	{
		temp=dx;
		dx=dy;
		dy=temp;
		swap=1;
	}
	for(int i=1;i<=dx;i++)
	{
		if(dp<0)
		{
			 if(swap) putpixel(x,y=y+s2,WHITE);
			 else putpixel(x=x+s1,y,WHITE);
			 dp+=2*dy;
		}
		else
		{
			putpixel(x=x+s1,y=y+s2,WHITE);

			dp=dp+2*dy-2*dx;
		}
	}
}

void main()
{
	int sx,sy,ex,ey;

	clrscr();

	int gdriver=DETECT,gmode, errorcode;
	initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
	errorcode=graphresult();
	if (errorcode!=grOk)
	{
		cout<<"Error at graphics"<<endl;
		getch();
		exit(1);
	}

	printf("Enter the start coordinates: ");
	scanf("%d %d",&sx,&sy);
	printf("Enter the end coordinates:   ");
	scanf("%d %d",&ex,&ey);

	cleardevice();

	bshmLine(sx,sy,ex,ey);

	getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -