So while working on the galvanometer project I ran into a tricky math question. I wanted to scale the needle length and degrees of sweep based on the dimensions of the image. Here are some examples:
In both examples, the needle in the galvanometer is a different length; longer in the top and shorter on the bottom. The sweep is also different, less on the top, and greater on the bottom. The divisions are the same for both. This was the kind of auto-scaling I wanted to achieve. But the math was being tricky. I spent a day playing with the math from an algorithms standpoint, but didn't get anywhere. So I decided to play with it geometrically. I needed an arc to pass through 3 points: the bottom left corner, the top center, and the bottom right corner. Since I was using a perfect circle, as long as I passed through one of the bottom points to the top center I could be assured the arc would do the same on the other side.
Initially I approached this as an equation with two known (one bottom corner, and the top center), and two unknowns (radius and angle). I failed to find any system of equations that did what I needed.
In the past I have used a geometric approach to solve these kind of problems, looking for way I could draw a solution rather than calculate it. So I played around for awhile in Sketchup before I discovered the following.
In the above drawing I have half the galvanometer (width is really ½ width). Point A is the top center, and B is the bottom right. What is known is the width and height, and the unknowns are the needle length (line AC), and angle c (∠ACD). I found that by drawing a line from A to B, dividing this line in half (point D) and making a line perpendicular to AB at point D, this would intersect line AC at exactly the correct location. That is if line AC were rotated at point C, point A would end up perfectly touching point B and making the desired arc. So the length of line AC is the needle length. The sweep angle can also be derived from this drawing. Angle c is half of what it would need to be to rotate AC to touch point B. And since this drawing is half the total sweep, angle c is ¼ the total sweep for the galvanometer.
This did not take long to implement, and had the desired results. One added benefit was the fact the math worked just fine for ratios that had more than 180 degrees of sweep. However, there are a couple of scenarios now possible with the galvanometer I needed to address. If the sweep is greater than π radians (180 degrees), the needle's start is no longer off the screen. Here is an example:
I no longer have a galvanometer, but a more general analogue gauge. So I added a correction to make the needle center when it was not off the screen. The last issue was when the ratio was such that the needle length was greater than half with image width. This caused some of the chart to be drawn off screen. When this happened, I simply limited the needle length to half the width. The results are exactly what I desired.
The library needs some cleanup and I need see if anymore functionality is required. Afterward I plan to add this to my open source libraries.