How to make the reading for indicating of deflection of the line
Hi All
I have something like this
Code:
Option Explicit
Dim Ruch As Boolean
Dim intDlugosc As Integer
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
intDlugosc = intDlugosc + 1
Load Line1(intDlugosc)
Line1(intDlugosc).X1 = X
Line1(intDlugosc).Y1 = Y
Ruch = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Ruch Then
Line1(intDlugosc).X2 = X
Line1(intDlugosc).Y2 = Y
Line1(intDlugosc).Visible = True
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Ruch = False
End Sub
How to be visible with my code, I can draw a some the straight of the line - for example : in the vertical.
Until I have pressed mouse's button I can this line to incline under any angle (in initial point of line).
Now I want to read the value of this angle - suffice for me only in the range since 0 to 180 degrees, how to make it?
Any suggestions, maybe a some lines of the code, thanks in advance
Re: How to make the reading for indicating of deflection of the line
Try this sample:
Code:
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim a As Single
Dim b As Single
Dim h As Single
Dim sinA As Single
Dim ratio As Single
Dim degreeA As Integer
Dim degreeB As Integer
Ruch = False
'the following is a typical calculation for right-angled triangles
'note: approach may vary but results should be the same
a = Abs(Line1.X2 - Line1.X1)
b = Abs(Line1.Y2 - Line1.Y1)
'if a = 0 then it's a vertical line so there is nothing to calculate - angle is 0 or 90 (however you want it)
'if b = 0 then it's a horizontal line - angle is 0 or 180 (however you want it)
If a > 0 And b > 0 Then
h = Sqr(a ^ 2 + b ^ 2)
sinA = b / h
'if a=b then angles are 90º,45º,45º and a=b=(sqr(2) / 2) = 0.707106781186548
ratio = sinA / ((Sqr(2) / 2))
degreeA = 45 * ratio
degreeB = 90 - degreeA
End If
MsgBox "Angle a = " & degreeA & Chr(186) & vbNewLine & _
"Angle b = " & degreeB & Chr(186)
End Sub
Re: How to make the reading for indicating of deflection of the line
RhinoBull, I'm having problems with your code as lines approach vertical.
I prefer using the ArcTangent function
Code:
Option Explicit
Const PI As Single = 3.141593 '3.14159265358979
Const PI½ As Single = PI / 2 '1.5707963267949
Public Function ATan2(ByVal DY As Single, ByVal DX As Single) As Single
If DX <> 0 Then
ATan2 = Atn(DY / DX) - (Sgn(DX) - 1) * PI½
Else
ATan2 = PI½ * Sgn(DY)
End If
End Function
calling code being...
Code:
Dim Radians As Single
With Line1(intDlugosc)
Radians = ATan2(.Y2 - .Y1, .X2 - .X1)
End With
...and to convert to degrees...
Code:
Degrees = Radians / PI * 180
'force degrees to be within 0 to 360
If Degrees < 0 then Degrees = Degrees + 360
Re: How to make the reading for indicating of deflection of the line
Quote:
Originally Posted by Milk
I prefer using the ArcTangent function
You can use anything you want really...
Re: How to make the reading for indicating of deflection of the line
What is the problem that you are/were having?