1、DDA(Digital Differential Analyzer)算法
The DDA algorithm is the simplest algorithm for drawing straight lines in computer graphics.
The two endpoints P0(x0,y0) and P1(x1,y1) of the line segment are known .
Then the slope of the straight line can be obtained:
k = (y2 - y1) / (x2 - x1)
Under the condition that both k and b are calculated, as long as we know a value of x, we can calculate a value of y.
y = kx + b
If the abscissa x increases by 1 each time (we call this step 1, that is, x = x +1), then the step of y is k+b.
x = x + 1
y = y + (k + b)
Also knowing a value of y can also calculate the value of x. At this time, the step of y is 1, and the step of x is (1-b)/k.
y = y + 1
x = x +(1 - b) / k
According to the calculated x and y values, round down to get the coordinates (x',y'), and draw a point on the straight line segment at (x',y').
In order to further simplify the calculation, we can usually set b to 0, and regard the starting point as (0,0).
Suppose the coordinates of the current point are (x i , y i ), and the coordinates of the next pixel point are (x i+1 , y i+1 ),
then use the DDA algorithm to solve the calculation formula of (xi+1, yi+1). Summarized as:
x i+1 = x i + xStep (1)
y i+1 = y i + yStep (2)
We generally determine xStep and yStep by calculating Δx and Δy:
If Δx> Δy, it means that the maximum difference of the x-axis is greater than the maximum difference of the y-axis, and the x-axis direction is the main direction of the step.
xStep = 1,yStep = k;
If Δy> Δx, it means that the maximum difference of the y-axis is greater than the maximum difference of the x-axis, and the y-axis direction is the main direction of the step.
yStep = 1,xStep = 1 / k。
According to this formula, (x i , y i ) can be iteratively calculated (x i+1 , y i+1 ), and then the calculated (x, y) coordinate point can be drawn in the coordinate system.
Implementation tools:
1) VS2019 (C++)
new project:
2) Download plug-in: Easyx. Please refer to the official website for usage and download: https://www.easyx.cn/
click to download and then install Click to install
at VC2019
The source code is as follows:
#include <iostream>
#include <graphics.h>
#include <math.h>
#include <conio.h>
using namespace std;
void DDALine(int x1, int y1, int x2, int y2)
{
int x0 = 400;
int y0 = 300; //记录原点坐标
int steps; //记录步长
int dx, dy; //记录起点和终点的坐标差值
float x, y; //记录即时坐标
float delta_x, delta_y; //记录划线过程中的坐标增量
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy)) //比较横纵坐标增量的大小
steps = dx;
else
steps = dy; //确保每次的增量不超过一个单位长度
x = x1;
y = y1; //记录画线起点
delta_x = float(dx) / steps;
delta_y = float(dy) / steps; //计算相邻两个点的增量
putpixel(x, y, RED);
for (int i = 0; i < steps; i++)
{
x = x + delta_x;
y = y + delta_y;
putpixel(x +int( x0 + 0.5), y0 - int(y + 0.5), RED);
}
}
int main()
{
int x1, x2, y1, y2;
int x0 = 400, y0 = 300; //坐标轴中心(x0,y0)
cout << "请输入两个整数点的坐标(x1,y1),(x2,y2)" << endl;
cin >> x1 >> y1 >> x2 >> y2;
initgraph(x0 * 2, y0 * 2); //初始化图形窗口大小
line(0, y0, x0 * 2, y0); //坐标轴X
line(x0, 0, x0, y0 * 2); //坐标轴Y
DDALine(x1, y1, x2, y2); //DDA画线算法
_getch(); //等待一个任意输入结束
closegraph(); //关闭图形窗口
return 0;
}