在 C# 中绘制希尔伯特曲线分形

这个非常短的Hilbert子程序绘制了 Hilbert 曲线。它以递归深度以及指定绘制方向的 dx和dy值作为参数。它以递归方式绘制了四条较小的 Hilbert 曲线并用线将它们连接起来。

cs 复制代码
// Draw a Hilbert curve.
private void Hilbert(Graphics gr, int depth, float dx, float dy)
{
    if (depth > 1) Hilbert(gr, depth - 1, dy, dx);
    DrawRelative(gr, dx, dy);
    if (depth > 1) Hilbert(gr, depth - 1, dx, dy);
    DrawRelative(gr, dy, dx);
    if (depth > 1) Hilbert(gr, depth - 1, dx, dy);
    DrawRelative(gr, -dx, -dy);
    if (depth > 1) Hilbert(gr, depth - 1, -dy, -dx);

    if (DoRefresh) picCanvas.Refresh();
}

由于 C# 没有提供简单的相对线绘制方法,因此代码中包含了一种方法。DrawRelative函数从点 (LastX, LastY) 绘制一条线到新点,并将该点的坐标存储在变量LastX和LastY中。

cs 复制代码
private float LastX, LastY;
...
// Draw the line (LastX, LastY)-(LastX + dx, LastY + dy) and
// update LastX = LastX + dx, LastY = LastY + dy.
private void DrawRelative(Graphics gr, float dx, float dy)
{
    gr.DrawLine(Pens.Black, LastX, LastY, LastX + dx, LastY + dy);
    LastX = LastX + dx;
    LastY = LastY + dy;
}

勾选程序的"刷新"框,使其在绘制每条线时更新其显示。这会大大减慢程序的速度,但它可以让您看到程序绘制深度大于 4 或 5 的线的顺序。

下载示例

https://download.csdn.net/download/ljygood2/90123675

相关推荐
用户29869853014几秒前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户36674625267431 分钟前
接口文档汇总 - 2.设备状态管理
c#
用户36674625267439 分钟前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang21 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530144 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools5 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21886 天前
.NET 本地Db数据库-技术方案选型
windows·c#
lindexi6 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice