Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Oct 20 '11 at 5:52

This question came from our site for professional and enthusiast programmers.

    
What does this have to do with graphics? –  Adam Robinson Oct 20 '11 at 1:45
    
It's to do with what I'm using this for –  NeoHaxxor Oct 20 '11 at 1:48
    
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? –  Jeffrey Sax Oct 20 '11 at 1:49
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). –  Adam Robinson Oct 20 '11 at 1:50
    
What does your input look like? You say you have a diagonal line, but how is it represented? –  Keith Thompson Oct 20 '11 at 1:51

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
I'm happy with this in case anyone needs it: Math.Atan2((centreY - y), (centreX - x)) * (180.0/Math.PI) –  NeoHaxxor Oct 20 '11 at 2:35

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!

share|improve this answer
    
Where the first vector is the centre and the second the mouse position? –  NeoHaxxor Oct 20 '11 at 2:02
    
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. –  iKiar Oct 20 '11 at 2:06
    
Got code? Not much of a mathematician :) –  NeoHaxxor Oct 20 '11 at 2:14
    
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. –  ssube Oct 20 '11 at 2:26
    
Exactly as peachykeen said. :) –  iKiar Oct 20 '11 at 3:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.