我想实现c#bresenham的行算法,但它只是绘制水平线,我希望它像我在许多教程中看到的那样绘制线条

发布于 2025-01-27 12:55:25 字数 1636 浏览 1 评论 0原文

我运行时得到的

这是我从用户输入的代码来使用brenesham绘制线路,但是即使我尝试了许多值,我也会绘制水平行,所以我希望它像在他们的教程中看到的,以水平天使

的角度实现它
private void button2_Click(object sender, EventArgs e)
        {
            int x1 = Convert.ToInt32(textBox5.Text);
            int y1 = Convert.ToInt32(textBox6.Text);

            int x2 = Convert.ToInt32(textBox7.Text);
            int y2 = Convert.ToInt32(textBox8.Text);

            Point p1 = new Point(x1, y1);
            Point p2 = new Point(x2, y2);

            Bresenham_Line(p1, p2);
        }
private void Bresenham_Line(Point p1, Point p2)
        {
            double x, y, x2, y2;
            Bitmap pp = new Bitmap(this.Width, this.Height);

            double dx = p2.X - p1.X;
            double dy = p2.Y - p2.Y;
            double p = 2 * (dy - dx);
            double c1 = 2 * dy;
            double c2 = 2 * (dy - dx);

           
            x = p1.X; y = p1.Y;
            
            pp.SetPixel((int)x, (int)y, Color.Blue);
           
            x2 = p2.X; y2 = p2.Y;

            while (x < x2)
            {
                
                if (p < 0)
                {
                    p += c1;
                }
                else
                {
                    p += c2;
                    y += 1;
                }

                x++;

                pp.SetPixel((int)x, (int)y, Color.Black);
            }


            pictureBox1.Image = pp;


        }

我添加了我运行时得到的照片

what i got when i run it

here's my code i take input from user to draw line using brenesham but it just draw horizontal line even when i tried with many values i have same issue so i want it to draw line with angel as i saw in my tutorials where they implement it with angles not just horizontal angel

private void button2_Click(object sender, EventArgs e)
        {
            int x1 = Convert.ToInt32(textBox5.Text);
            int y1 = Convert.ToInt32(textBox6.Text);

            int x2 = Convert.ToInt32(textBox7.Text);
            int y2 = Convert.ToInt32(textBox8.Text);

            Point p1 = new Point(x1, y1);
            Point p2 = new Point(x2, y2);

            Bresenham_Line(p1, p2);
        }
private void Bresenham_Line(Point p1, Point p2)
        {
            double x, y, x2, y2;
            Bitmap pp = new Bitmap(this.Width, this.Height);

            double dx = p2.X - p1.X;
            double dy = p2.Y - p2.Y;
            double p = 2 * (dy - dx);
            double c1 = 2 * dy;
            double c2 = 2 * (dy - dx);

           
            x = p1.X; y = p1.Y;
            
            pp.SetPixel((int)x, (int)y, Color.Blue);
           
            x2 = p2.X; y2 = p2.Y;

            while (x < x2)
            {
                
                if (p < 0)
                {
                    p += c1;
                }
                else
                {
                    p += c2;
                    y += 1;
                }

                x++;

                pp.SetPixel((int)x, (int)y, Color.Black);
            }


            pictureBox1.Image = pp;


        }

i added photo from what i get when i run it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文