The green flag loads the linkage. Change the bar lengths and press the space bar to see how the red path changes. Press left or right arrows to rotate the crank. If a bar's length makes it impossible to link 2 joints, the joint will be plotted in the lower left corner. Press "1" to see the bar map. For a description of how the algorithms are used in the code see https://www.diywalkers.com/linkage-simulation.html We used this code to optimize TrotBot's linkage. Here's a video of a LEGO TrotBot walking around: https://www.youtube.com/watch?v=d_mz_S3HC_I
Mechanical linkages connect "bars"at "joints" that allow the bars to rotate. When the arrow keys are pressed, the crank turns, which moves all the joints. This code uses algebra and analytical geometry to calculate each joint's X and Y position, allowing the linkage to be drawn with the pen. The joint positions depend on the bar's lengths, which are loaded into the bar variables when the green flag is pressed. To make the code run faster, the X,Y values of each joint are first calculated for one full rotation of the crank, which are saved to each joint's list. Then, when the crank is rotated the "rotation" variable is incremented and the joint values are retrieved from these lists. The main algorithm used to find the joint's X,Y values is called "Circle Intersection", which finds the two points where 2 circles intersect. There are 4 choices for choosing between the two points: "low Y", "high Y", "left X", and "right X". If you look at the joint you are trying to calculate and imagine the path it should take, that should help you to see which of the 4 choices will always give you the correct intersection point. "Circle Intersection" is based on Paul Bourke's "Intersection of two circles" solution, where he used the Pythagorean theorem and some clever analytical geometry to find the 2 intersection points. His derivation is the 5th one down on this webpage: http://paulbourke.net/geometry/circlesphere/