|
-
Aug 2nd, 2007, 02:01 AM
#1
Thread Starter
Addicted Member
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?
-
Aug 2nd, 2007, 02:06 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 2nd, 2007, 02:14 AM
#3
Thread Starter
Addicted Member
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?
-
Aug 2nd, 2007, 02:31 AM
#4
Re: How do you find the length of a line
Width = X2 - X1
Width in pixels = (X2 / Screen.TwipsPerPixelsX) - (X1 / Screen.TwipsPerPixelsX)
-
Aug 2nd, 2007, 02:38 AM
#5
Thread Starter
Addicted Member
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.
-
Aug 2nd, 2007, 02:41 AM
#6
Re: How do you find the length of a line
-
Aug 2nd, 2007, 02:43 AM
#7
Thread Starter
Addicted Member
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.
-
Aug 2nd, 2007, 02:46 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 2nd, 2007, 02:57 AM
#9
Addicted Member
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
-
Aug 2nd, 2007, 02:59 AM
#10
Thread Starter
Addicted Member
Re: How do you find the length of a line
Do you know any formulas that will work with a slooping line?
-
Aug 2nd, 2007, 03:02 AM
#11
Addicted Member
Re: How do you find the length of a line
 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.
-
Aug 2nd, 2007, 03:03 AM
#12
Thread Starter
Addicted Member
Re: How do you find the length of a line
Thanks Jazz , Rated
-
Aug 2nd, 2007, 03:04 AM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 2nd, 2007, 03:08 AM
#14
Thread Starter
Addicted Member
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
-
Aug 2nd, 2007, 03:15 AM
#15
Addicted Member
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.
-
Aug 2nd, 2007, 03:20 AM
#16
Thread Starter
Addicted Member
Re: How do you find the length of a line
Ok thanks .
-
Aug 2nd, 2007, 04:54 AM
#17
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
-
Aug 2nd, 2007, 06:35 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|