locked
Calculate distance between tow points RRS feed

  • Question

  • Dears,

    I want to create windows application form to allow user to draw two points only and draw line between those two points on picturebox and display distance between those two point.

    if user select another two points it will clear previous point

    Thank you

    Tuesday, January 26, 2016 8:34 AM

Answers

  • Hi Yasser Zaid,

    You could use 'FillEllipse' to draw point.

    Here is the code:

    Graphics g;
            private void Form2_Load(object sender, EventArgs e)
            {
                g = pictureBox1.CreateGraphics();
            }
            Point FirstP = new Point();
            Point SecondP = new Point();
            //doubleclick to drawline
            private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (FirstP == new Point())
                {
                    FirstP = new Point(e.X, e.Y);
                    //refresh the line
                    pictureBox1.Refresh();
                    //draw point
                    g.FillEllipse(new Pen(Color.Black, 2).Brush, e.X, e.Y, 2, 2);
                }
                else if (SecondP == new Point())
                {
                    SecondP = new Point(e.X, e.Y);
                    //draw point
                    g.FillEllipse(new Pen(Color.Black, 2).Brush, e.X, e.Y, 2, 2);
                    g.DrawLine(new Pen(Color.Black, 1), FirstP, SecondP);
                    //Calculating the distance
                    double lon = Math.Sqrt((FirstP.X - SecondP.X) * (FirstP.X - SecondP.X) + (FirstP.Y - SecondP.Y) * (FirstP.Y - SecondP.Y));
                    g.DrawString(lon.ToString(), this.Font, new Pen(Color.Black, 1).Brush, SecondP);
                    //clean point
                    FirstP = new Point();
                    SecondP = new Point();
                }
            }

    Regards,

    Moonligth


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Yasser Zaid Tuesday, January 26, 2016 12:17 PM
    Tuesday, January 26, 2016 11:43 AM

All replies

  • Hi Yasser Zaid,

    You could refer to the following code(I have used the 'MouseDoubleClick' event):

            Point FirstP = new Point();
            Point SecondP = new Point();
            //doubleclick to drawline
            private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (FirstP == new Point())
                {
                    FirstP = new Point(e.X, e.Y);
                    //refresh the line
                    pictureBox1.Refresh();
                }
                else if (SecondP == new Point())
                {
                    SecondP = new Point(e.X, e.Y);
                    Graphics g = pictureBox1.CreateGraphics();
                    g.DrawLine(new Pen(Color.Black, 1), FirstP, SecondP);
                    //Calculating the distance
                    double lon = Math.Sqrt((FirstP.X - SecondP.X) * (FirstP.X - SecondP.X) + (FirstP.Y - SecondP.Y) * (FirstP.Y - SecondP.Y));
                    g.DrawString(lon.ToString(), this.Font, new Pen(Color.Black, 1).Brush, SecondP);
                    //clean point
                    FirstP = new Point();
                    SecondP = new Point(); } }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, January 26, 2016 9:17 AM
  • Thank you but can you please when user click on picturebox draw first point then second point with line between them

    Tuesday, January 26, 2016 9:57 AM
  • Hi Yasser Zaid,

    You could use 'FillEllipse' to draw point.

    Here is the code:

    Graphics g;
            private void Form2_Load(object sender, EventArgs e)
            {
                g = pictureBox1.CreateGraphics();
            }
            Point FirstP = new Point();
            Point SecondP = new Point();
            //doubleclick to drawline
            private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (FirstP == new Point())
                {
                    FirstP = new Point(e.X, e.Y);
                    //refresh the line
                    pictureBox1.Refresh();
                    //draw point
                    g.FillEllipse(new Pen(Color.Black, 2).Brush, e.X, e.Y, 2, 2);
                }
                else if (SecondP == new Point())
                {
                    SecondP = new Point(e.X, e.Y);
                    //draw point
                    g.FillEllipse(new Pen(Color.Black, 2).Brush, e.X, e.Y, 2, 2);
                    g.DrawLine(new Pen(Color.Black, 1), FirstP, SecondP);
                    //Calculating the distance
                    double lon = Math.Sqrt((FirstP.X - SecondP.X) * (FirstP.X - SecondP.X) + (FirstP.Y - SecondP.Y) * (FirstP.Y - SecondP.Y));
                    g.DrawString(lon.ToString(), this.Font, new Pen(Color.Black, 1).Brush, SecondP);
                    //clean point
                    FirstP = new Point();
                    SecondP = new Point();
                }
            }

    Regards,

    Moonligth


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Yasser Zaid Tuesday, January 26, 2016 12:17 PM
    Tuesday, January 26, 2016 11:43 AM
  • Thank you
    Tuesday, January 26, 2016 12:17 PM
  • Dear Sir,

    What if i want also to draw rectangle  with mouse and calculate it's area with the above example

    Thank you

    Wednesday, January 27, 2016 5:36 AM