0
$\begingroup$

I need to calculate the degrees from vertical of a diagonal line, from 0 to -180, clockwise. I have used arctan to find out the degrees, but that gives me angles out of 90, with the sign depending on the quadrant. Conversion to what I need would be messy. Is there a function I can use to get me straight there, if that makes sense? Thanks

EDIT: The line is drawn between the centre of the screen and the position of the mouse. The angle I would like to calculate is the slope of the diagonal between these points, in a 0 to -180 degree system, clockwise. Like longitude is measured.

enter image description here I would expect the red line to have an angle of -135.

  • 0
    -180 and 0 are equal2011-10-20

2 Answers 2

1

Perhaps the atan2 function would be of use to you. It returns a value from -π to +π in radians. Convert the value to degrees, and you should have a proper answer.

  • 0
    I'm happy with this in case anyone needs it: Math.Atan2((centreY - y), (centreX - $x$)) * (180.0/Math.PI)2011-10-20
0

To get an angle (lets call it theta), from a line, you'd want to simply apply the dot product between the two lines. Recall:

vector1*vector2 = |vector1||vector2|cos(theta) , 

and according to such,

theta = cos^-1(vector1*vector2/|vector1||vector2|). 

If you were to use your vertical as vector 1 (simply use a unit vector (0, 1)), and your diagonal is vector 2 (from the slope, calculate a unit vector along the line of your diagonal. you can do this by picking two arbitrary points on your line), you should be able to use the second equation I posted to calculate your theta. Note: In order for theta to fall between 0 and -180, make sure the coordinates chosen to calculate vector 2 are to the right of the center. This will ensure that the inverse cosine function returns the smallest angle possible, which should be in that range.

Hope this helps!

  • 0
    Exactly as peachykeen said. :)2011-10-20