none
VB Forms Line Length RRS feed

  • Question

  • I'm making a program that involves the user drawing a line. I want to be able to find the distance of the line (in pixels) and display it. I also want the length to be exactly the way the user draws them, not just distance between first and second point. Any tips?
    Saturday, September 22, 2018 1:07 AM

Answers

  • Hi

    Try this code as a test. You need a new project with a Label1 on Form1. This example keeps a running total from mouse click to release and puts total px moved in Label1.

    ' Form1 with Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = ClientSize.Width.ToString & "  " & ClientSize.Height.ToString
      End Sub
      Dim px As Double = 0.0
      Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Static last As New Point
        If e.Button = Windows.Forms.MouseButtons.Left AndAlso ClientRectangle.Contains(New Point(e.X, e.Y)) Then
          CreateGraphics.DrawLine(Pens.Red, last.X, last.Y, e.X, e.Y)
          px += Math.Sqrt(Math.Abs(e.X - last.X) ^ 2 + Math.Abs(e.Y - last.Y) ^ 2)
        End If
        last = e.Location
      End Sub
      Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        Label1.Text = px.ToString("0.00")
        px = 0.0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Tejas Vinod Sunday, September 23, 2018 11:05 PM
    Sunday, September 23, 2018 9:13 PM

All replies

  • " I also want the length to be exactly the way the user draws them, not just distance between first and second point"

    It is not clear to me what you mean eactly.

    Do you mean the user is drawing a freehand curve between points and you want the length of curve in pixels?

    If it is a straight line then I don't get what you mean.

    Perhaps show a simple code example and or a picture of what you mean. If you cant show an image yet give us a link to an image.

    Saturday, September 22, 2018 12:30 PM
  • I would like it so that the user is drawing a freehand line and I want the length of the curve in pixels.

    Code and Output:
    https://gyazo.com/5fb3d99dc618c81d87252445a22d1b25
    • Edited by Tejas Vinod Sunday, September 23, 2018 6:21 PM Added URL to Image
    Sunday, September 23, 2018 6:14 PM
  • Hi

    Why not count the pixels as the line is being drawn by the user? After all, the user needs to move/click the mouse to effect the line, and the mose position is known at all times.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, September 23, 2018 8:31 PM
    Sunday, September 23, 2018 7:29 PM
  • I would like it so that the user is drawing a freehand line and I want the length of the curve in pixels.

    Code and Output:
    https://gyazo.com/5fb3d99dc618c81d87252445a22d1b25

    Sorry I am not going to run a project from the net like that.

    You should post an easily reproducible one form code example of how you draw now.

    Also how do you save your data now?

    Sunday, September 23, 2018 7:56 PM
  • That is exactly what I want to do but I have no Idea how to do it.
    Sunday, September 23, 2018 8:28 PM
  • You don't need to run it, I just want a way to count the pixels in the line.

    And I don't know what you mean by "How do you save your data now?"

    Sunday, September 23, 2018 8:29 PM
  • Hi

    How is the line drawn ny the user? It could be one of a couple of ways. For example;

    1. click, drag mouse making line as it goes, release to end.

    2. click, release, move mouse making line as it goes, click to end.

    3. click, make start point, click intermediate points, joining up as they go, right clcik to end.

    ........... and many other possibilities.


    Regards Les, Livingston, Scotland

    Sunday, September 23, 2018 8:36 PM
  • click, drag mouse making line as it goes, release to end

    Code:

    Public Class Form1
        Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            Static last As New Point
            Dim myPen As Pen
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Me.CreateGraphics.DrawLine(Pens.Red, last.X, last.Y, e.X, e.Y)
            End If
            last = e.Location
        End Sub
    End Class
    

    Sunday, September 23, 2018 8:41 PM
  • You don't need to run it, I just want a way to count the pixels in the line.

    And I don't know what you mean by "How do you save your data now?"

    Well, that could get to be a hard problem.

    What I would do is calc the length of the hypotenuse of the x,y triangle between each two points. That would be good enough for what I do. In fact that is what I want for what I do ie CAD. I want 6.7 pixels.

    Is that what you want? 6.7 pixels distance between points?

    Or do you want to count the pixels drawn on the screen for the line drawn? That would be harder and why would you want that?

    So do you have any code now? if so post it.

     

    PS Oh I see your example.

    Explain why you want to do this.

    Sunday, September 23, 2018 8:41 PM
  • click, drag mouse making line as it goes, release to end

    Code:

    Public Class Form1
        Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            Static last As New Point
            Dim myPen As Pen
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Me.CreateGraphics.DrawLine(Pens.Red, last.X, last.Y, e.X, e.Y)
            End If
            last = e.Location
        End Sub
    End Class

    Hi

    Seems you are creating straight lines rather than curves. In which case, why not use a List(Of Point) to store user points and then you can calculate at leasure in any way needed.


    Regards Les, Livingston, Scotland

    Sunday, September 23, 2018 8:48 PM
  • Hi

    Try this code as a test. You need a new project with a Label1 on Form1. This example keeps a running total from mouse click to release and puts total px moved in Label1.

    ' Form1 with Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = ClientSize.Width.ToString & "  " & ClientSize.Height.ToString
      End Sub
      Dim px As Double = 0.0
      Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Static last As New Point
        If e.Button = Windows.Forms.MouseButtons.Left AndAlso ClientRectangle.Contains(New Point(e.X, e.Y)) Then
          CreateGraphics.DrawLine(Pens.Red, last.X, last.Y, e.X, e.Y)
          px += Math.Sqrt(Math.Abs(e.X - last.X) ^ 2 + Math.Abs(e.Y - last.Y) ^ 2)
        End If
        last = e.Location
      End Sub
      Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        Label1.Text = px.ToString("0.00")
        px = 0.0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Tejas Vinod Sunday, September 23, 2018 11:05 PM
    Sunday, September 23, 2018 9:13 PM
  • Well, I'm designing a calculator for my FRC robotics team that will estimate how long it will take to get from one point of a field to another point. Last year I was using google sheets to accomplish this but then I realized if I could program this on a VB form it would be much more user-friendly and easier for me. The field often has obstacles and several places we need to get to so the path of the robot will not always be in a straight line. If I can count the pixels in a line drawn by the User I could convert it to inches. The User can plug in the speed of the robot and it will display the time it will take the robot to reach that point.

    Sunday, September 23, 2018 9:15 PM
  • This seems to work fine but does it use The Pythagorean Theorem?
    Sunday, September 23, 2018 9:28 PM
  • This seems to work fine but does it use The Pythagorean Theorem?

    Hi

    Please use the Quote button to reply, or, use the person name you are replying to - otherwise, it is difficult to know who you are replying to.

    If that was directed to me, the Yes it does use Pythagoras.


    Regards Les, Livingston, Scotland

    Sunday, September 23, 2018 9:39 PM
  • This seems to work fine but does it use The Pythagorean Theorem?

    Hi

    Please use the Quote button to reply, or, use the person name you are replying to - otherwise, it is difficult to know who you are replying to.

    If that was directed to me, the Yes it does use Pythagoras.


    Regards Les, Livingston, Scotland

    Ok the thing is Pythagorean theorem will give me the length of the hypotenuse, not side A + side B. I need this to be as accurate as possible, so is there a way we can make a pixel counter?
    Sunday, September 23, 2018 9:48 PM
  • This seems to work fine but does it use The Pythagorean Theorem?

    Hi

    Please use the Quote button to reply, or, use the person name you are replying to - otherwise, it is difficult to know who you are replying to.

    If that was directed to me, the Yes it does use Pythagoras.


    Regards Les, Livingston, Scotland

    Ok the thing is Pythagorean theorem will give me the length of the hypotenuse, not side A + side B. I need this to be as accurate as possible, so is there a way we can make a pixel counter?

    Hi

    You have me confused now. Why would side A + Side B be more accurate?


    Regards Les, Livingston, Scotland

    Sunday, September 23, 2018 9:53 PM
  • Pythagorean Theorem states that a^2 + b^2 = c^2, In which Side A and Side B are 2 sides of a triangle and C is its hypotenuse. Say I drew 2 perpendicular lines(Side A and Side B), the formula you put in your code:

    px += Math.Sqrt(Math.Abs(e.X - last.X) ^ 2 + Math.Abs(e.Y - last.Y) ^ 2)

    Would be for finding the hypotenuse for the two perpendicular lines. Not Side A + Side B.

    If I'm misunderstanding something or explaining this wrong please correct me.

    • Proposed as answer by pauledy Sunday, September 23, 2018 10:02 PM
    • Unproposed as answer by pauledy Sunday, September 23, 2018 10:02 PM
    Sunday, September 23, 2018 10:02 PM
  • Pythagorean Theorem states that a^2 + b^2 = c^2, In which Side A and Side B are 2 sides of a triangle and C is its hypotenuse. Say I drew 2 perpendicular lines(Side A and Side B), the formula you put in your code:

    px += Math.Sqrt(Math.Abs(e.X - last.X) ^ 2 + Math.Abs(e.Y - last.Y) ^ 2)

    Would be for finding the hypotenuse for the two perpendicular lines. Not Side A + Side B.

    If I'm misunderstanding something or explaining this wrong please correct me.

    You are misunderstanding how many points are being used as you drag the mouse on the screen.

    The result can only be as accurate as the user draws it. This is dependant on a few things. One is how big is the "field" drawing the user is drawing upon on the screen? Both in real life and in pixels on the screen ie scale ratio 1/24 ? or what. That determines part of how accurate you will be.

    The second thing is the accuracy of the path the user draws to represent the actual path. The user can curve and backup or duplicate sections of the path.

    Another thing is the accuracy of the robot path and speed.

    All that is part of the total accuracy. Since other features of your method seem to control the largest error magnitudes etc, the actual method of adding distance between points becomes less important on total accuracy.

    Realize the code method makes many sets of points and add it all. You might have hundreds of mouse points for one path. Or depending on other factors you might have 10 points or even 1 or 2.

    Unless you can show your other factors have less influance on the accuracy than the method the sum of distance between points as Les shows should be fine.

    :)

    Sunday, September 23, 2018 10:13 PM
  • Hi Tejas

    We both understand Pythagorus, but unless I am misunderstanding things, then if you are wanting the px 'travelled' by the mouse pointer while marking a 'path' then the Hypotenuous is the correct 'side' to use.


    Regards Les, Livingston, Scotland

    Sunday, September 23, 2018 10:14 PM
  • Pythagorean Theorem states that a^2 + b^2 = c^2, In which Side A and Side B are 2 sides of a triangle and C is its hypotenuse. Say I drew 2 perpendicular lines(Side A and Side B), the formula you put in your code:

    px += Math.Sqrt(Math.Abs(e.X - last.X) ^ 2 + Math.Abs(e.Y - last.Y) ^ 2)

    Would be for finding the hypotenuse for the two perpendicular lines. Not Side A + Side B.

    If I'm misunderstanding something or explaining this wrong please correct me.

    You are misunderstanding how many points are being used as you drag the mouse on the screen.

    The result can only be as accurate as the user draws it. This is dependant on a few things. One is how big is the "field" drawing the user is drawing upon on the screen? Both in real life and in pixels on the screen ie scale ratio 1/24 ? or what. That determines part of how accurate you will be.

    The second thing is the accuracy of the path the user draws to represent the actual path. The user can curve and backup or duplicate sections of the path.

    Another thing is the accuracy of the robot path and speed.

    All that is part of the total accuracy. Since other features of your method seem to control the largest error magnitudes etc, the actual method of adding distance between points becomes less important on total accuracy.

    Realize the code method makes many sets of points and add it all. You might have hundreds of mouse points for one path. Or depending on other factors you might have 10 points or even 1 or 2.

    Unless you can show your other factors have less influance on the accuracy than the method the sum of distance between points as Les shows should be fine.

    :)

    The other factors have less influence so could you explain how exactly the code works?
    Sunday, September 23, 2018 10:18 PM
  • Hi Tejas

    We both understand Pythagorus, but unless I am misunderstanding things, then if you are wanting the px 'travelled' by the mouse pointer while marking a 'path' then the Hypotenuous is the correct 'side' to use.


    Regards Les, Livingston, Scotland

    Please Explain how the code works.
    Sunday, September 23, 2018 10:18 PM
  • Hi Tejas

    We both understand Pythagorus, but unless I am misunderstanding things, then if you are wanting the px 'travelled' by the mouse pointer while marking a 'path' then the Hypotenuous is the correct 'side' to use.


    Regards Les, Livingston, Scotland

    Please Explain how the code works.

    Hi Tejas

    The code sums the 'hypotenuouses' of the line each time the MouseMove sub is called and while the mouse pointer is inside the Form and also only if the left button is down. The same events that draw the line.

    When the mouse buttion is released, then the sum of all the hyptenuouses are displayed.

    Here is a slightly different version that shows the number of hypotenuouses summed for each line. Test by trying to draw several lines of similar looking lengths but at differing speeds and see the difference made just by varying the speed of the drawing. BTW the size of the Form is in the Title Bar and can be used as a means to roughly judge the length of a line (but, very roughly)

    ' Form1 with Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = ClientSize.Width.ToString & "  " & ClientSize.Height.ToString
      End Sub
      Dim px As Double = 0.0
      Dim c As Integer = 0
      Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Static last As New Point
        If e.Button = Windows.Forms.MouseButtons.Left AndAlso ClientRectangle.Contains(New Point(e.X, e.Y)) Then
          CreateGraphics.DrawLine(Pens.Red, last.X, last.Y, e.X, e.Y)
          px += Math.Sqrt(Math.Abs(e.X - last.X) ^ 2 + Math.Abs(e.Y - last.Y) ^ 2)
          c += 1
        End If
        last = e.Location
      End Sub
      Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        Label1.Text = px.ToString("0.00    ") & c & "Hypot"
        px = 0.0
        c = 0
      End Sub
      Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
        Text = ClientSize.Width.ToString & "  " & ClientSize.Height.ToString
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, September 23, 2018 10:32 PM
    Sunday, September 23, 2018 10:28 PM
  • Thank you! I misunderstood how the code works

    • Edited by Tejas Vinod Sunday, September 23, 2018 11:06 PM
    Sunday, September 23, 2018 11:05 PM