I've been in a math mood lately and decided to have some fun with trigonometry. I coded a galvanometer in PHP and made a quick Javascript interface to control it. The position coordinates of the needle on a galvanometer are not exactly trivial--at least, doing it correctly.
First, in order to determine the location of the needle, we need to think of the range of the needle. For our needle, we used a swing of 90°, represented by the shaded red area. This is π/2 radians (for you non-math types, a full circle is 2π radians). This span starts 135° counter-clockwise from the standard 0 location. So to determine the needle position, we simple scale the needle position (in this case, 0-100) to radians 0 to π/2.
From there, the X and Y positions are pretty simple:
We now have the coordinates for the top of the needle, but not the bottom. If the entire needle is drawn, those coordinates are simple. Y is the bottom of the chart, and X is the middle. But I wasn't going for simple and decided to hide the bottom of the needle.
What we have turns our to be a geometry problem--similar triangles. Proportionally, the two triangles the same. We know the coordinates of point A, point B and the Y-coordinate of point C. What we don't know is the X-coordinate of point C.
Now we have the full coordinates for the needle. For division marks, we can simply recycle the equation used to determine the top of the needle's location and run it twice. For the top, we extended the needle length by some amount (5 pixels in the example case) and for the bottom, decrease the needle length by the same amount. This will result in top and bottom coordinates for the divisions.
I thought about trying to come up with some sort of automatic scaling so that the chart would determine a needle length needed to fit the full 90° to the window. But I didn't like what I came up with. Instead, I simply made the needle length 3 times with height of the meter window.
Here are the results:
For testing, I made the javascript automatically update the position ever 1/2 second and redraw the image. This worked fine, but Firefox never freed the memory for any of images. I left my computer for a time and returned to find Firefox consuming over 650 MB of RAM. It took a few minutes to get Firefox to close and the system was crawling. I've herd good things about FF3 having fixed memory leaks. Let's hope that's true.
The top chart is a graph of the function and the bottom graph a histogram to show the data spread.