c# - 如何在 C# 中编写 bresenham 算法?

标签 c# graph-algorithm

我是这样写的,但它只在 50% 的情况下有效。谁能告诉我哪里出了问题?

public void Bresenham(int x1,int y1,int x2,int y2,Color c)
        {            
            double dx = x2 - x1;
            double dy = y2 - y1;
            double d = 2*dy-dx; //aux variable
            double p1 = 2 * dy ;
            double p2 = 2 * (dy - dx);
            int x = x1;
            int y = y1;
            int xend;
            c = kolor;
            if (x1 > x2)
            {
                x = x2;
                y = y2;
                xend = x1;
            }
            else
            {
                x = x1;
                y = y1;
                xend = x2;
            }
            bitmapa.SetPixel(x, y,c);
            try
            {
                while (x < xend)
                {
                    x++;
                    if (d < 0)
                    {
                        d += p1;
                    }
                    else
                    {
                        d += p2;
                        y += 1;
                    }
                    bitmapa.SetPixel(x, y, c);
                }
            }

谢谢:)

最佳答案

在第一次拍摄时,您错过了一个应该像现在处理 Y 一样处理其他坐标的情况。您现在处理 DY < DX 时的情况,您还应该处理 DX < DY 时的情况,即线的斜率是不同的。

要理解我在说什么,请看steep here .

实际上,您的算法仅适用于 1/4 的情况。

关于c# - 如何在 C# 中编写 bresenham 算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5203708/

相关文章:

algorithm - 从一组数字中计算目标数字

c++ - 代码在我的系统上运行良好,但在提交 HackerRank 时出现段错误

c# - 修改EmguCV以使用P/Invoke添加缺少的功能

c# - 在 Raspberry Pi 上 dockerize .net core mvc 应用程序后,wwwroot 文件夹中没有静态文件

C# 和 NpgsqlDataAdapter 返回单个字符串而不是数据表

c# - Android C2DM 服务器端

c# - 使用 JasperReports Server API 从运行报告中检索 PDF

algorithm - 从 3 着色减少到公平 3 着色

php - 在无向图中查找路径

algorithm - 给定一个具有节点权重的二部图,根据一定的启发式获取一种类型节点的有序列表