我想实现c#bresenham的行算法,但它只是绘制水平线,我希望它像我在许多教程中看到的那样绘制线条
这是我从用户输入的代码来使用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;
}
我添加了我运行时得到的照片
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论