DDA算法画直线有个问题,麻烦大家看看,帮帮忙?

timmyangel 2008-12-09 12:53:40
private void DDAToolStripMenuItem_Click(object sender, EventArgs e)
{

if (bt == null)
bt = new Bitmap(PictureBox1.Width,PictureBox1.Height);

float xa = 33;
float ya = 44;
float xb = 66;
float yb = 234;

float delta_x, delta_y;
float x, y;
int dx, dy, steps, k;
dx = (int)(xb - xa);
dy = (int)(yb - ya);
if (Math.Abs(dx) > Math.Abs(dy))
steps = Math.Abs(dx);
else steps = Math.Abs(dy);
delta_x = (float)dx / (float)steps;
delta_y = (float)dy / (float)steps;
x = xa;
y = ya;
bt.SetPixel((int)x, (int)y, BtnColor.BackColor);
for (k = 1; k <= steps; k++)
{
x += delta_x;
y += delta_y;
bt.SetPixel((int)x, (int)y, BtnColor.BackColor);
}
PictureBox1.Image = bt;


}
这是之前的DDA画线,但只能定位坐标来画,我问下怎么样才能用鼠标在PictureBox1上面随便画直线??
已知:MouseDown { pStart.X = e.X;
pStart.Y = e.Y;}
MouseUp{ pEnd.X = e.X;
pEnd.Y = e.Y;}
...全文
493 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
timmyangel 2008-12-10
  • 打赏
  • 举报
回复
flysystem123@163.com
大哥这是我的msn:yf1229yf@hotmail.com
可以加我吗?我有很多东西想请教你
LorenLiu 2008-12-09
  • 打赏
  • 举报
回复
已经弄出来了,怎么发给你?
timmyangel 2008-12-09
  • 打赏
  • 举报
回复
假如我现在定义了这样的一个类,怎么样把值传进去呢?
public class Pixel
{
public int _x;
public int _y;

public Pixel()
{
this._x = 0;
this._y = 0;
}

public Pixel(int x,int y)
{
this._x = x;
this._y = y;
}
}
public class Point
{
public float x;
public float y;
}
public Pixel LineDDA(Point p0,Point p1,Pixel precedent)
{
Pixel descendent = new Pixel();
float k=Math.Abs((p1.y-p0.y)/(p1.x-p0.x));

if(k>1||k==1)
{
descendent._x = precedent._x + 1;
descendent._y = precedent._y + 1;
}
else
{
descendent._x = precedent._x + 1;
descendent._y = precedent._y + 1;
}
return descendent;
}
MouseDown { pStart.X = e.X;
pStart.Y = e.Y;}
MouseUp{ pEnd.X = e.X;
pEnd.Y = e.Y;}
之前试过老是不行,大家帮下忙......课程设计来的

LorenLiu 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 timmyangel 的回复:]
引用 2 楼 LorenLiu 的回复:
引用 1 楼 h_w_king 的回复:
if (bt == null)
bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);

Graphics g = Graphics.FromImage(bt);
g.DrawLine(Pens.Black, new Point(pStart.X, pStart.Y ), new Point(pEnd.X, pEnd.Y));

this.pictureBox1.Image = bt;


LZ是要用DDA方法来画线。。。。

LZ可以把DDA方法定…
[/Quote]

PictureBox1注册MouseDown和MouseUp事件后,就能得到e.Location,就是需要的坐标值啊
timmyangel 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 LorenLiu 的回复:]
引用 1 楼 h_w_king 的回复:
if (bt == null)
bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);

Graphics g = Graphics.FromImage(bt);
g.DrawLine(Pens.Black, new Point(pStart.X, pStart.Y ), new Point(pEnd.X, pEnd.Y));

this.pictureBox1.Image = bt;


LZ是要用DDA方法来画线。。。。

LZ可以把DDA方法定义成一个函数 Bitmap DDA(Po…
[/Quote]
怎么样把鼠标的两个位置传入startPoint和endPoint 呢?之前弄过还是不行?
wartim 2008-12-09
  • 打赏
  • 举报
回复
我以前写的,在form上随便画线
第一次点击确定起点
第二次点击确定终点,而且在鼠标移动过程中也会动态的显示直线
第三次点击画的直线消失

namespace WindowsApplication27
{
public partial class Form1 : Form
{
List<Point> Points = new List<Point>();
Point MovePoint;
public Form1()
{
InitializeComponent();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (Points.Count == 0)
{
Points.Add(e.Location);
MovePoint = new Point(e.X, e.Y);
}
else if (Points.Count == 1)
Points.Add(e.Location);
else if (Points.Count == 2)
{
Points.RemoveAt(0);
Points.RemoveAt(0);
}
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (Points.Count == 1)
MovePoint = new Point(e.X,e.Y);
this.Refresh();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
if (Points.Count == 1)
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), Points[0], MovePoint);
else if (Points.Count == 2)
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), Points[0], Points[1]);
}
}
}

LorenLiu 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 h_w_king 的回复:]
if (bt == null)
bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);

Graphics g = Graphics.FromImage(bt);
g.DrawLine(Pens.Black, new Point(pStart.X, pStart.Y ), new Point(pEnd.X, pEnd.Y));

this.pictureBox1.Image = bt;
[/Quote]

LZ是要用DDA方法来画线。。。。

LZ可以把DDA方法定义成一个函数 Bitmap DDA(Point startPoint, Point endPoint)来实现,startPoint和endPoint分别传入鼠标的两个位置就好了
h_w_king 2008-12-09
  • 打赏
  • 举报
回复
if (bt == null)
bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);

Graphics g = Graphics.FromImage(bt);
g.DrawLine(Pens.Black, new Point(pStart.X, pStart.Y ), new Point(pEnd.X, pEnd.Y));

this.pictureBox1.Image = bt;

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧