数值微分法(DDA)绘制直线
数值微分法(Digital Differential Analyzer)直接从直线的微分方程生成直线。
详细的原理见以下链接:https://blog.csdn.net/weixin_43751983/article/details/106503634
这里直接用C#实现了,用的是计算机图形学基础这本书里提供的C++代码魔改的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Math;
namespace 数值微分法绘制线
{
class Program
{
static void Main(string[] args)
{
DDAAlgorithm(0, 0, 5, 2);
Console.ReadKey();
}
public static void DDAAlgorithm(int X0, int Y0, int X1, int Y1)
{
int dx, dy, epsl, k;
float x, y, xIncre, yIncre;
dx = X1 - X0;
dy = Y1 - Y0;
x = X0;
y = Y0;
if (Abs(dx) > Abs(dy))
{
epsl = Abs(dx);
}
else
{
epsl = Abs(dy);
}
xIncre = (float)dx / (float)epsl;
yIncre = (float)dy / (float)epsl;
for (int i = 0; i <= epsl; i++)
{
Console.WriteLine((int)(x + 0.5) + ", " + (int)(y + 0.5));
x += xIncre;
y += yIncre;
}
}
}
}这里和上面链接里的数据是一样的,得出来的结果如下:




鲁公网安备 37148202000241号