Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Angle of two lines

Status
Not open for further replies.

robdunfey

Technical User
Apr 26, 2002
110
GB
I want to know the angle of a line from the horizontal. What is the simplest was of calulating this in VB?

Rob
 
robdunfey,

Try this:

Private Sub Command2_Click()
Dim x1, x2, y1, y2
Const pi = 3.14159265358979

Dim dblNormUnit As Double
Dim lngRadius As Long
Dim dblCurrentAngle As Double
Dim dblRadians As Double

'These numbers define your line. Put your numbers here:
x1 = 0
y1 = 0
x2 = 2
y2 = 2

'This displays angle in degrees
MsgBox 180 * Atn((y2 - y1) / (x2 - x1)) / pi


End Sub

vladk
 
vladk,

Thank You. I actually meant from the vertical?! So if a point is in the top right corner of the screen the angle would be 45 degrees, halfway down the right it would be 90 degrees, bottom left 225 degrees etc.

Rob
 
To get angles from vertical just swap x and y coordinates from above. You will need to check for DIV/0 errors when X2 = X1

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top