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;}

解决方案 »

  1.   

     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;
      

  2.   


    LZ是要用DDA方法来画线。LZ可以把DDA方法定义成一个函数 Bitmap DDA(Point startPoint, Point endPoint)来实现,startPoint和endPoint分别传入鼠标的两个位置就好了
      

  3.   

    我以前写的,在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]);
            }
        }
    }
      

  4.   

    怎么样把鼠标的两个位置传入startPoint和endPoint 呢?之前弄过还是不行?
      

  5.   


    PictureBox1注册MouseDown和MouseUp事件后,就能得到e.Location,就是需要的坐标值啊
      

  6.   

    假如我现在定义了这样的一个类,怎么样把值传进去呢?
    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;}
    之前试过老是不行,大家帮下忙......课程设计来的
      

  7.   

    [email protected]
    大哥这是我的msn:[email protected]
    可以加我吗?我有很多东西想请教你