BLOG.CSHARPHELPER.COM: Draw an animation of two rods connected to a rotating wheel in C#
Draw an animation of two rods connected to a rotating wheel in C#
This example draws a rotating wheel connected to a rod that is connected to another rod that is anchored somewhere in the middle. (I made this project because I wanted to see what path the farther end of the blue rod would travel. In retrospect, it's obvious that it must be an arc of a circle.)
The program uses a timer to rotate the wheel. The following code shows the timer's event handler.
// Rotate the wheel. // The wheel's current angle of rotation. private const double Dtheta = Math.PI / 10; private double Theta = 0;
This code simply draws the wheel and its two rods. The center and radius of the circle are known. The blue rod's anchor point A and the blue rod's lengths on each side of that point are also known. Finally the length of the green rod is known. The only tricks are figuring out where points B and C are. Once you know that, drawing the picture is reasonably straightforward.
Finding point B is straightforward. Its location is determined by the wheel's current angle of rotation.
To find point C you need to realize that it is a fixed distance from point B along the blue rod and a different fixed distance from point B along the green rod. That means point C must lie on the blue and green dashed circles. Mathematically point C lies at the intersection of the dashed circles.
The example Determine where two circles intersect in C# explains how to determine where two circles intersect. This example uses the FindCircleCircleIntersections method described by that example.
(Note that two circles may intersect in two places, as they do in this example. That means there are two possible configurations for this system. In the second configuration, the point C is at the bottom of the picture. This example picks the solution it does by always using the first point of intersection returned by the FindCircleCircleIntersections method.)
The rest of the DrawSystem method draws all of the system's pieces so they look nice.
The picture on the right shows the complete program. Use the textboxes to experiment with the length and radius parameters:
D - Distance between the blue rod's anchor and the wheel's center
Comments