PDA

View Full Version : [SOLVED] Angles between 2 lines



Tommy
08-11-2017, 05:30 AM
Hi All,

I am having some issues with figuring angles between 2 lines.
2005620057
Above are 2 images of where I am trying to find the angle. The first I get 5 degrees, the second one I get 60 degrees. They should be 85 degrees and 120 degrees. I am using this formula:


Slope_A = (iData(iOne).Y - iData(iOne).y1) / (iData(iOne).X - iData(iOne).x1)
Slope_B = (iData(iOne + 1).Y - iData(iOne + 1).y1) / (iData(iOne + 1).X - iData(iOne + 1).x1)
Tangent_of_Angle = (Slope_B - Slope_A) / (1 + Slope_A * Slope_B)

Here is the iData class:


Public Class TLine
Public X As Double
Public Y As Double
Public x1 As Double
Public y1 As Double
Public tLength As Double
Public Property LineLength() As Double
Get
Return SettLength()
End Get
Set(ByVal value As Double)
tLength = value
End Set
End Property
Private Function SettLength()
If X = x1 Then 'line is straight
tLength = Math.Abs(Y - y1)
ElseIf Y = y1 Then 'line is straight
tLength = Math.Abs(X - x1)
Else
tLength = Math.Sqrt(Squared(Math.Abs(X - x1)) + Squared(Math.Abs(Y - y1)))
End If
SettLength = tLength
End Function
End Class

I will post whatever information you need to help.
The data is coming from a drawing that is to scale so the data is correct.

Thanks for looking.
Any help at all is appreciated.

Paul_Hossler
08-11-2017, 06:10 AM
1. Is that VB or VBA? Looks like VB to me

2. Can you attach a workbook that has the X-Y's of the points?

3. If X=x1 then the slope would be infinite

4. If Y=y1, then the slope would be 0

Tommy
08-11-2017, 06:39 AM
Morning Paul,


It is VB but we have a lot of very smart people here and I figured ya'll could help me out.
I think I have attached an excel workbook. I have in the workbook a picture of what I am trying to draw and the points and the angles I came up with. I am looking for the angle the next line goes on. Please keep in mind that the lines could go anywhere.
I am getting the slope of the lines and derive the tangents from that. I think it is giving be the difference of the 2 lines but it is using the smaller angles.
hmm maaybe I could determine the angle the lines are on and go from there?

Tommy
08-11-2017, 06:40 AM
Paul,

Yes I do get 0 and infinity, that needs to be taken into account also. These are the 2 number that showed me I was Wrong!> :)

Tommy
08-11-2017, 07:26 AM
Paul Thank you so much for your help. You made me get out of the box I was in.

I figured it out. I was just looking at the lines, I should have been doing calculations for the angle between 2 vectors.
To get the below code to work correctly you need to make the call for the length of the line to get the NewX and NewY to get populated.
Revised class:


Public Class TLine
Public X As Double
Public Y As Double
Public x1 As Double
Public y1 As Double
Public tLength As Double
Public tAngle As Double
Public NewX As Double
Public NewY As Double
Public Property LineLength() As Double
Get
Return SettLength()
End Get
Set(ByVal value As Double)
tLength = value
End Set
End Property
Private Function SettLength() As Double
If X = x1 Then 'line is straight
tLength = Math.Abs(Y - y1)
ElseIf Y = y1 Then 'line is straight
tLength = Math.Abs(X - x1)
Else
tLength = Math.Sqrt(Squared(Math.Abs(X - x1)) + Squared(Math.Abs(Y - y1)))
End If
NewX = X - x1
NewY = Y - y1
Return tLength
End Function
Public Property AnAngle() As Double
Get
Return MakeAnAngle()
End Get
Set(ByVal value As Double)
tAngle = value
End Set
End Property
Private Function MakeAnAngle() As Double
tAngle = Math.Atan((Y - y1) / (X - x1))
Return tAngle
End Function
End Class
Private Function DotProduct(ByRef iData As TLine(), iOne As Integer, iTwo As Integer) As Double
Dim mTemp As Double
mTemp = (iData(iOne).NewX * iData(iTwo).NewX) + (iData(iOne).NewY * iData(iTwo).NewY)
Return mTemp
End Function
Private Function GetCos(ByRef iData As TLine(), iOne As Integer, iTwo As Integer) As Double
Dim mTemp As Double, mWork As Double
mWork = (iData(iOne).LineLength * iData(iTwo).LineLength)
mTemp = DotProduct(iData, iOne, iTwo) / mWork
Return mTemp
End Function

Tommy
09-20-2017, 08:39 AM
OK Update:
That did not work the way I wanted it to. I was getting an angle but I never knew what angle it was. Sometimes it was the compliment.
I used the documentation here : http://www.teacherschoice.com.au/maths_library/trigonometry/solve_trig_sss.htm

Paul_Hossler
09-20-2017, 11:34 AM
Try this



Option Explicit

'based on
'ref http://www.devx.com/tips/Tip/33124

Const cPI As Double = 3.14159265358979

Function getAngleBetweenLinesVect(commonX As Double, commonY As Double, _
X1 As Double, Y1 As Double, _
Y2 As Double, X2 As Double) As Double

Dim aRadians As Double
Dim diffX1 As Double, diffX2 As Double, diffY1 As Double, diffY2 As Double

'check if both the lines are the same, if they are exit the function
If (commonY = Y1 And commonX = X1) Or _
(commonY = Y2 And commonX = X2) Or _
(Y1 = Y2 And X1 = X2) Then

getAngleBetweenLinesVect = 0#
Exit Function
End If

'set the variables
diffX1 = commonX - X1
diffY1 = commonY - Y1
diffX2 = commonX - X2
diffY2 = commonY - Y2

'applying the angle between two lines vector rule, calculate the angle in radians
aRadians = invCos(((diffX1 * diffX2) + (diffY1 * diffY2)) / _
(Sqr(diffX1 ^ 2 + diffY1 ^ 2) * Sqr(diffX2 ^ 2 + diffY2 ^ 2)))

'convert to degrees
getAngleBetweenLinesVect = aRadians * 180# / cPI
End Function

'the inverse cos function. Returns an angle.
Public Function invCos(X As Double) As Double
If X > 1 Or X < -1 Then
'if the ratio supplies is outside the acceptable range.
ElseIf X = 1 Then
invCos = 0
ElseIf X = -1 Then
invCos = cPI

Else
invCos = (Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1))
End If
End Function