-1

I created a Knob (basically a circle) using the Snap.svg library which I rotate using Snap's transform function and rotate parameter.

I use two Snap built in functions, Snap.deg() and Snap.angle() to rotate the knob to a certain position (so that the "hand" faces the cursor or a touch point). My understanding is that Snap.angle works very similarly to the atan2 function, while Snap.deg converts a radian value to degrees

The issue I am having is that by default, the 0 / 360° position is when the hand of the knob (like the hour hand) is facing 3:00 and (the degrees go up when you move the knob clockwise) I want the 0 / 360° position to be when the hour hand is at 6:00 (with the degrees still going up for clockwise rotation).

I tried to just add subtract 90° but that just makes the value inaccurate because from 3:00 and 6:00 the angle calculated is between 0 and -90.

These values are important not because of how the physical knob looks (the current calculations work for that) but because I am using the calculated values elsewhere in the code and want a value of 0 for due south, 90 for due west, 180 for due north etc.

The reference for Snap.deg is: http://snapsvg.io/docs/#Snap.angle

Startec
  • 12,496
  • 23
  • 93
  • 160
  • Please be a bit more specific on what exactly it is that does not work. Your approach seems to be valid. – Codor Mar 28 '14 at 07:25

2 Answers2

1

The "normal" cartesian coordinates and trig operations use:

x = cos(theta)
y = sin(theta)

and make zero degrees go along the positive X axis, and points sweep anticlockwise as theta increases.

To start at the "6 o'clock" position and rotate clockwise instead, you need:

x = -sin(theta)
y = -cos(theta)

i.e. x and y swapped, and negated.

Hence, to get theta from x and y, use:

theta = atan2(-x, -y);    // usually y, x
Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

If atan2(y, x) gives you an angle, with 0 for east and π/2 for north, then atan2(x, -y) will give you an angle with 0 for south and π/2 for east. If you want π/2 west, as Alnitak mentioned in a comment, simply replace x by -x, and you get atan2(-x, -y).

MvG
  • 57,380
  • 22
  • 148
  • 276
  • knobs usually rotate _clockwise_, so `π/2` should be _west_. – Alnitak Mar 28 '14 at 09:08
  • I see. I should have been more clear, I am looking to less change the value of the rotation, (my graphics library depends on the number) but instead "map" the values (after they are calculated) to get it in a different range. Any idea here? – Startec Mar 28 '14 at 22:37
  • @Startec: Please edit your question to make it more clear. In particular, please outline what libraries you use, where and how you obtain the value and where the library depends on it. Also indicate exactly what orientation you get and what orientation you want. – MvG Mar 29 '14 at 07:08