当前位置:首页 > 代码相关 > 正文内容

数值微分法(DDA)绘制直线

admin5年前 (2021-03-14)代码相关3433

数值微分法(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;
            }
        }
    }
}

这里和上面链接里的数据是一样的,得出来的结果如下:

image.png


扫描二维码推送至手机访问。

版权声明:本文由lovedm.club发布,如需转载请注明出处。

本文链接:https://lovedm.club/?id=123

分享给朋友:

“数值微分法(DDA)绘制直线” 的相关文章

递归计算1到x的和

递归真是个神奇的东西,当时学C的时候就没搞明白,学C#又遇到例子了。        public int SumXTo1(int x)     &n...

C# try-catch处理异常

使用try-catch进行异常处理,下面是两个小例子:两个例子中没有写finally语句finally的作用是无论有无异常,finally下的语句都会执行。//简单的处理异常namespace _20200323 {     class ...

C# 正则表达式(1)

C# 正则表达式(1)

用于匹配输入文本的模式string s = "this is a test!"; string res = Regex.Replace(s, "^",&nbs...

C# 与文件相关的几个类(1)

C# 与文件相关的几个类(1)

C# 与文件访问相关的常用的类:File类、Directory类、Path类、FileInfo类、DirectoryInfo类、FileSystemInfo类、FileSystemWatcher类以上几个类均在System.IO命名空间下。挨个说吧:File类:静态类,只有静态方法,用于移...

C# 与文件相关的几个类(2)

Directory类:静态类,主要处理文件目录。方法:CreateDirectory(String)在指定路径中创建所有目录和子目录,除非它们已经存在。返回值是一个DirectoryInfo对象Delete(String)从指定路径删除空目录。无返回值。Exists(String)确定给定路径是否引...

C#(或者Java)反转数组

将原数组反转,如[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]反转后变为[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]因为数组是引用类型,所以直接在方法中处理即可,C#和Java写法一样,如下:      &nb...