The deals dealer
The deals dealer

Reputation: 1016

For a manual speedometer control, how do I convert needle angle to speed?

I'd like to have a manual speedometer control where a user can drag the needle to a particular speed setting, like the following:

Sample speedometer control

From the angle of the above needle, how would I read back the speed that corresponds to? For example, the above would read back a speed of 100.

Upvotes: 0

Views: 1240

Answers (1)

Bogatyr
Bogatyr

Reputation: 19323

I've done just this calculation myself (but in reverse, given a speed, adjust the needle angle) on a similar app. Presumably your code knows the angle of the needle since you're animating it, and that you prevent the needle from going below 0 or above 160. I'll assume your needle image at rest is pointing straight up and down (at 80mph) Figure out what the angle is when the needle is pointing at 160mph, and pointing at 0mph by putting an NSLog dumping out the angle when you animate in response to touches and notice the values that make the needle just about perfectly displaying 0 and 160 (you can tweak this in code later to get it just right). Also I'm assuming that positive angles rotate clockwise and negative angles rotate counter-clockwise.

Then the speed for any arbitrary angle is:

double mphPerDegree = 160 / (angle160mph - angle0mph);
double speed = (currentAngleInDegrees * mphPerDegree) + 80.0;

Upvotes: 1

Related Questions