Line Angle

faisalali129

New member
Joined
May 7, 2005
Messages
4
Programming Experience
3-5
i want to calculate angle of a line. when user draw a line on the picture box the angle of the line is calculated automatically no metter where the line is drown plz help me out i will be very thankfull
 
You need to use some trigonometry. If you line starts at (x1, y1) and ends at (x2, y2) then the tangent of the angle between your line and the horizontal is (y2 - y1)/(x2 - x1) and the tangent of the angle between your line and the vertical is (x2 - x1)/(y2 - y1). If you don't recall your trig functions, the tangent of an angle in a right-angled triangle is the ratio of the opposite side to the adjacent side. In code this looks like:
VB.NET:
[color=Blue]Dim[/color] horizontalDistance [color=Blue]As Integer[/color] = Point2.X - Point1.X
[color=Blue] Dim[/color] verticalDistance [color=Blue]As Integer[/color] = Point2.Y - Point1.Y

[color=Green] 'Calculate the horizontal angle in radians.[/color]
[color=Blue] Dim[/color] horizontalAngleRads [color=Blue]As Double[/color] = Math.Atan(verticalDistance / horizontalDistance)

[color=Green]'Calculate the horizontal angle in degrees.[/color]
[color=Blue] Dim[/color] horizontalAngleDegs [color=Blue]As Double[/color] = 180 * horizontalAngleRads / Math.PI

[color=Green]  'Calculate the vertical angle in radians.[/color]
[color=Blue]  Dim[/color] verticalAngleRads [color=Blue]As Double[/color] = Math.Atan(horizontalDistance / verticalDistance)
 
[color=Green]  'Calculate the vertical angle in degrees.[/color]
[color=Blue]  Dim[/color] verticalAngleDegs [color=Blue]As Double[/color] = 180 * verticalAngleRads / Math.PI
 
Back
Top