一:简介
二:划分象限
如上图,将平面坐标系,划分为8个象限
1象限中 dx = 1,dy = k,2象限 dx = 1/k,dy = 1
三:增量表格
规律 当|dx| > |dy| 时 dx=1,dy=k,否则 dx=1/k ,dy = 1
于是 总步长 EPS 应该取 dx,dy 中较大的那一个
具体代码:
https://gitee.com/feng_guang_zhi/games101/tree/develop/Rasterizer
代码如下:
void Rasterizer::DDALine(Eigen::Vector3f start,Eigen::Vector3f end){
int dx = round(end.x() - start.x());
int dy = round(end.y() - start.y());
int eps = std::max(abs(dx),abs(dy));
float stepX = (float)dx / eps;
float stepY = (float)dy / eps;
Eigen::Vector3f color(1.0f,0.0f,0.0f);
Eigen::Vector3f sx(start.x(),start.y(),start.z());
for(int i = 1;i <= eps;i++){
setPixel(sx,color);
sx += Eigen::Vector3f(stepX,stepY,0.0f);
}
}
rasterizer.DDALine(Eigen::Vector3f(200,200,0),Eigen::Vector3f(500,456,0));
执行效果: