画线算法
- DDA(数值微分法)
void DDALine(int x0,int y0,int x1,int y1){
float delta,x,y,dx,dy;
dx = x1-x0;
dy = y1-y0;
delta = dy/dx;
y = y0;
x = x0;
if(delta>=1){ //斜率大于等于1
for(y=y0;y<=y1;y++){
DrawPixel(int(x+0.5),y);
x+=1./delta;
}
}
else for(x=x0;x<=x1;x++){ //斜率小于1
DrawPixel(x,int(y+0.5));
y+=delta;
}
}
- 中点画线法
void MidPointLine(int x0,int y0,int x1,int y1){
int a,b,x,y,d,delta1,delta2,k;
a = y0-y1;
b = x1-x0;
x = x0;
y = y0;
k = (y1-y0)>=(x1-x0)?-1:1;
DrawPixel(x,y);
if(k==1){ //斜率为0~1
delta1 = 2*a;
delta2 = 2*(a+b);
d = 2*a+b;
while(x<x1){
if(d<0){
x++;
y++;
d+=delta2;
}
else{
x++;
d+=delta1;
}
DrawPixel(x,y);
}
}
else{ //斜率为1~∞
delta1 = 2*b;
delta2 = 2*(a+b);
d = a+2*b;
while(y<y1){
if(d>0){
x++;
y++;
d+=delta2;
}
else{
y++;
d+=delta1;
}
DrawPixel(x,y);
}
}
}
- Bersenham画线算法(e=k-0.5)
void BersenhamLine(int x0,int y0,int x1,int y1){
int x,y,dx,dy,e;
x = x0;
y = y0;
dx = x1-x0;
dy = y1-y0;
e = 2*dy - dx; //new_e = 2*e*dx,此处代码里出现的均为new_e
while(x<x1){
DrawPixel(x,y);
x++;
e+=2*dy;
if(e>=1){
y++;
e-=2*dx;
}
}
}
没有图,有点无聊,附上喜欢的图:
ps3大神绝景版,水墨渲染厉害了