📜  使用 DDA 算法扫描将 (3,2) 的行转换为 (4,7).在点 (3,4) 之后写下一个点的 y 坐标 - 无论代码示例

📅  最后修改于: 2022-03-11 14:59:06.548000             🧑  作者: Mango

代码示例1
// C program for DDA line generation
#include
#include
#include
//Function for finding absolute value
int abs (int n)
{
    return ( (n>0) ? n : ( n * (-1)));
}
 
//DDA Function for line generation
void DDA(int X0, int Y0, int X1, int Y1)
{
    // calculate dx & dy
    int dx = X1 - X0;
    int dy = Y1 - Y0;
 
    // calculate steps required for generating pixels
    int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
 
    // calculate increment in x & y for each steps
    float Xinc = dx / (float) steps;
    float Yinc = dy / (float) steps;
 
    // Put pixel for each step
    float X = X0;
    float Y = Y0;
    for (int i = 0; i <= steps; i++)
    {
        putpixel (round(X),round(Y),RED);  // put pixel at (X,Y)
        X += Xinc;           // increment in x at each step
        Y += Yinc;           // increment in y at each step
        delay(100);          // for visualization of line-
                             // generation step by step
    }
}
 
// Driver program
int main()
{
    int gd = DETECT, gm;
 
    // Initialize graphics function
    initgraph (&gd, &gm, "");  
 
    int X0 = 3, Y0 = 2, X1 = 4, Y1 = 7;
    DDA(3,4);
    return 0;
}