OpenGL实现DDA画线算法

数字微分分析仪(digital differential analyzer)方法是一种线段扫描转换算法。

DDA算法比直接使用直线方程计算的速度更快,它利用光栅特性消除了直线方程种的乘法。

本代码参考自《计算机图形学》第四版,该函数将在verts末尾按次序添加需要的点坐标

inline int round(const float a)
{
    return int(a + 0.5);
}

void
GLApp::lineDDA(std::vector<glm::vec2> &verts,int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs(dx) > fabs(dy)) { steps = fabs(dx); } else { steps = fabs(dy); } xIncrement = float(dx) / float(steps); yIncrement = float(dy) / float(steps); verts.push_back(glm::vec2(round(x), round(y))); for (k = 0; k < steps; ++k) { x += xIncrement; y += yIncrement; verts.push_back(glm::vec2(round(x), round(y))); } }

猜你喜欢

转载自www.cnblogs.com/paralysis/p/10783211.html
dda
今日推荐