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

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

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

数值微分法(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#的类型系统

C#的类型系统

C#的五大数据类型:    类(Class)    结构体(Structures)    枚举(Enumerations)    接口(In...

C# 中左移运算

C# 中左移运算

将一个数换算成二进制,整体向左移动指定的位数。如:7的二进制在Int32中二进制表达为:00000000 00000000 00000000 00000111将其左移一位则变为:00000000 00000000 00000000 00001110若移位后超出最大位数,则超出部分舍掉,如:Int32...

C#事件_Sample_3

事件的拥有者同时是事件的响应者using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者同时是事件的响应者 /// </summary> na...

C# 正则表达式(2)

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

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

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

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