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
    What does this have to do with graphics?2011-10-20
  • 0
    It's to do with what I'm using this for2011-10-20
  • 0
    So, if your diagonal line is the short hand on a clock, then 6 o'clock is 0 degrees, 9 o'clock is -90, and 12 o'clock is -180? What about the right side (3 o'clock, etc.)? In which direction are your x and y values measured?2011-10-20
  • 1
    @NeoHaxxor: Tags are intended to describe the nature of the question, not necessarily what your intended purpose is (unless it has some bearing on the question).2011-10-20
  • 0
    What does your input look like? You say you have a diagonal line, but how is it represented?2011-10-20
  • 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
    Where the first vector is the centre and the second the mouse position?2011-10-20
  • 0
    The first vector is a unit vector (magnitude of 1) in the vertical direction (0, 1) from the center. The second, if it is based off of mouse position, then you calculate the vector from the mouse to the center, and proceed with calculations as mentioned.2011-10-20
  • 0
    Got code? Not much of a mathematician :)2011-10-20
  • 0
    The first vector is given in the comment, the second is the position of the mouse relative to the middle of the canvas. The calculations are all functions. The real question is what math library you'll use (as C# doesn't come with vector math), past that, it's all in there with the same names as iKiar gave.2011-10-20
  • 0
    Exactly as peachykeen said. :)2011-10-20