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?
Printable View
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?
Just subtract the X coordinate values and divide by Screen.TwipsPerPixelX.
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?
Width = X2 - X1
Width in pixels = (X2 / Screen.TwipsPerPixelsX) - (X1 / Screen.TwipsPerPixelsX)
Yes that will give me the width of a square but I only want the value of a lines length.
Use Pythagorean theorem.
Can you please explain how to use that. Im only 14, and I haven't leant any "Pythagorean theorem" yet.
If its a sloping line then just use geometry as leinard31 posted. I too originally thought you wanted a horizontal or vertical 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
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.Quote:
Originally Posted by nuclear112
Thanks Jazz :), Rated
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
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 :)
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.
Ok thanks :).
This draws a line (1000 scaleunits long) from the center of the form to the mouse
or even simpler...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
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
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