#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
void DDALine(int x1, int y1, int x2, int y2, int color)
{
float increx,increy,x,y,length;
int i;
if(abs(x2-x1)>abs(y2-y1))
length = abs(x2-x1);
else
length = abs(y2-y1);
increx = (x2-x1)/length;
increy = (y2-y1)/length;
x = x1;
y = y1;
for(i=1;i<=length;i++)
{
putpixel(int(x+0.5),int(y+0.5),color); //在(x,y)处,以color色画点
x += increx;
y += increy;
}
}
void cbLine(int x1,int y1,int x2,int y2)
{
int dx,dy,n,k,i,f;
int x,y;
dx = abs(x2-x1);
dy = abs(y2-y1);
n = dx+dy;
if(x2>=x1)
{
k= y2>=y1?1:4;
x = x1;
y = y1;
}
else
{
k = y2>=y1? 2:3;
x = x1;
y = y1;
}
putpixel(x,y,4);
for(i=0,f=0;i<n;i++)
if(f>= 0)
switch(k)
{
case 1:putpixel(x++,y,4);f -= dy;break;
case 2:putpixel(x,y++,4);f -= dx;break;
case 3:putpixel(x--,y,4);f -= dy;break;
case 4:putpixel(x,y--,4);f -= dx;break;
}
else
switch(k)
{
case 1:putpixel(x,y++,4);f += dx;break;
case 2:putpixel(x--,y,4);f += dy;break;
case 3:putpixel(x,y--,4);f += dx;break;
case 4:putpixel(x++,y,4);f += dy;break;
}
}
int sign(int a)
{
if(a>0)
return 1;
else if(a==0)
return 0;
else
return -1;
}
void BresenhamLine(int x1,int y1,int x2,int y2,int color)
{
int x,y,s1,s2,increx,increy,temp,interchange;
int e,i;
x = x1;
y = y1;
s1 = sign(x2-x1);
s2 = sign(y2-y1);
increx = abs(x2-x1);
increy = abs(y2-y1);
if(increy > increx)
{
temp = increx;
increx = increy;
increy = temp;
interchange = 1;
}
else
interchange = 0;
e = 2*increy-increx;
for(i=1;i<=increx;i++)
{
putpixel(x,y,color);
if(e>=0)
{
if(interchange = 1)
x = x+s1;
else
y = y+s2;
e = e-2*increx;
}
if(interchange = 1)
y = y+s2;
else
x = x+s1;
e = e+2*increy;
}
}
main()
{
/*
*1.--------紫色线条:DDA算法---------
*2.--------红色线条:逐点比较算法---------
*3.--------绿色线条:Bresenham算法---------
*/
int mode,driver;
mode = VGA;
mode = VGAMED;
initgraph(&driver,&mode," ");
cleardevice();
DDALine(10,40,100,300,5);
cbLine(30,50,200,400);
BresenhamLine(100,50,400,400,2);
getch();
closegraph();
}
_肉君
- 粉丝: 2
- 资源: 5
最新资源
- W3CSchool全套Web开发手册中文CHM版15MB最新版本
- Light Table 的 Python 语言插件.zip
- UIkit中文帮助文档pdf格式最新版本
- kubernetes 的官方 Python 客户端库.zip
- 公开整理-2024年全国产业园区数据集.csv
- Justin Seitz 所著《Black Hat Python》一书的源代码 代码已完全转换为 Python 3,重新格式化以符合 PEP8 标准,并重构以消除涉及弃用库实现的依赖性问题 .zip
- java炸弹人游戏.zip学习资料程序资源
- Jay 分享的一些 Python 代码.zip
- 彩色形状的爱心代码.zip学习资料程序资源
- SQLAlchemy库:Python数据库操作的全方位指南
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈