学习计算机图形学的第一个画线算法就是LineDDA算法,实现起来也非常的简单。
再此,我只是把它写下来作个留恋。
//==============
#include <stdio.h>
#include <math.h>
#include <graphics.h>
int Rand(float a){return (int)(a+0.5);}
void LineDDA(int x1,int y1,int x2, int y2){
int dx,dy,steps;
int xIncr,yIncr;
int i;
dx = x2 - x1;
dy = y2 - y1;
if(fabs(dx) > fabs(dy))
steps = fabs(dx);
else
steps = fabs(dy);
xIncr = dx/steps;
yIncr = dy/steps;
putpixel(Rand(x1),Rand(y1),5);
for(i = 0;i < steps; i++){
x1+=xIncr; y1+=yIncr;
putpixel(Rand(x1),Rand(y1),5);
}
}
int main(){
int x1=50,x2 = 200,y1 = 50 ,y2 = 200;
int gd = DETECT,gm = 0;
clrscr();
initgraph(&gd,&gm,"");
outtextxy(x1+5,y1-5,"50,50");
outtextxy(x2+5,y2-5,"200,200");
LineDDA(x1,y1,x2,y2);
/*
printf("Hello World./n");
*/ getch();
return 1;
}