Results 1 to 18 of 18

Thread: How do you find the length of a line

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    How do you find the length of a line

    Is there/What is the formula to find the length of a line in pixels with the supplied coordenents like X1,X2,Y1 and Y2?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do you find the length of a line

    Just subtract the X coordinate values and divide by Screen.TwipsPerPixelX.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Yes but what coordinates do I devide by... E.G if I have X1=2040 X2=2520 Y1=1560 Y2=1800 What would the formula be?

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How do you find the length of a line

    Width = X2 - X1

    Width in pixels = (X2 / Screen.TwipsPerPixelsX) - (X1 / Screen.TwipsPerPixelsX)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Yes that will give me the width of a square but I only want the value of a lines length.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How do you find the length of a line

    Use Pythagorean theorem.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Can you please explain how to use that. Im only 14, and I haven't leant any "Pythagorean theorem" yet.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do you find the length of a line

    If its a sloping line then just use geometry as leinard31 posted. I too originally thought you wanted a horizontal or vertical line.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: How do you find the length of a line

    Code:
    Private Sub Form_Load()
          'MsgBox CalcPythag(1, 4, 4, 0)
          'MsgBox CalcPythag2(3, 4)
    End Sub
    
    Function CalcPythag(X1 As Double, X2 As Double, Y1 As Double, Y2 As Double) As Double
          CalcPythag = Round(Sqr((Abs(X2 - X1) ^ 2) + (Abs(Y2 - Y1) ^ 2)), 2)
    End Function
    
    Function CalcPythag2(Length As Double, Height As Double) As Double
          CalcPythag2 = Round(Sqr((Height ^ 2) + (Length ^ 2)), 2)
    End Function

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Do you know any formulas that will work with a slooping line?

  11. #11
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: How do you find the length of a line

    Quote Originally Posted by nuclear112
    Do you know any formulas that will work with a slooping line?
    The above should work with any kind of straight lines (sloping or not, just not curved) just plug in the x1,x2,y1,y2 values and away you go.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Thanks Jazz , Rated

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do you find the length of a line

    Its two triangles basically. So if you apply the Pythagorean therom then its a^2 + b^2 = c^2. I think thats the formula. Its been many years since I used it.

    a = x and b = y and c = the sloping line
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Is it possible to make a line keep a certain length and when the mouse is moved on the form, the line is in the direction of the mouse and is still the same length. Thanks

  15. #15
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: How do you find the length of a line

    maths functions <_<.

    Possibly obtaining the mouses position, calculating the angle from the base point of the line and then redrawing the line so that it is X length and pointing towards the mouse....

    one of those clock sources would be good here, since I can't remember if its cos, sin or tan that would be needed.

    I'll take a quick look for you.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How do you find the length of a line

    Ok thanks .

  17. #17
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: How do you find the length of a line

    This draws a line (1000 scaleunits long) from the center of the form to the mouse
    Code:
    Option Explicit
    Dim OX As Single, OY As Single
    Const LINELEN = 1000
    
    Private Sub Form_Resize()
      OX = ScaleWidth \ 2
      OY = ScaleHeight \ 2
    End Sub
    
    Private Sub form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Dim R As Single, D As Single
      D = Sqr((X - OX) ^ 2 + (Y - OY) ^ 2) 'find the distance
      If D > 0 Then 'if the distance is greater than 0...
        R = LINELEN / D 'find the ratio...
        Cls 'clear the form...
        Line (OX, OY)-Step((X - OX) * R, (Y - OY) * R) 'and draw the line
      End If
    End Sub
    or even simpler...
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Dim R As Single, VX As Single, VY As Single
      VX = X - OX:  VY = Y - OY
      R = Sqr(VX * VX + VY * VY) / LINELEN
      If R Then Cls: Line (OX, OY)-Step(VX / R, VY / R)
    End Sub
    Last edited by Milk; Aug 2nd, 2007 at 10:33 AM. Reason: explanation added

  18. #18
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: How do you find the length of a line

    Using a Line control instead of the Line method:
    Code:
    Option Explicit
    
    Private lngLineLength As Single
    
    Private Sub Form_Load()
      lngLineLength = 1500
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Dim xDiff As Single
      Dim yDiff As Single
      Dim Angle As Single
      
      With Line1
        xDiff = X - .X1
        yDiff = Y - .Y1
        If xDiff <> 0 Then
          Angle = Atn(yDiff / xDiff)
          .X2 = .X1 + lngLineLength * Cos(Angle) * Sgn(xDiff)
          .Y2 = .Y1 + lngLineLength * Sin(Angle) * Sgn(xDiff)
        Else
          .X2 = .X1
          .Y2 = .Y1 + lngLineLength * Sgn(yDiff)
        End If
      End With
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width