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

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

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

数值微分法(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)绘制直线” 的相关文章

九九乘法表算法

九九乘法表算法

namespace _20200324 {     class Program     {         st...

C#事件_Sample_2

事件的拥有者与事件的响应者是分开的情况+=是事件订阅操作符,左边是事件,右边是事件处理器。using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者和事件的响应者是...

C# 正则表达式(2)

// pattan = @"[^ahou]"; 表示匹配除ahou之外的字符,^在表示反义 string res4 = Regex.Replace(s, @"[^ahou]",&...

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...

PIE二次开发 加载栅格数据

1、获得栅格数据集路径2、打开栅格数据集3、创建栅格图层4、将数据添加到图层并刷新要添加两个引用:using PIE.DataSource;using PIE.Carto;// 获得要打开栅格数据的路径 OpenFileDialog file = new&n...