The Product Design class I am a TA for had an assignment to design a four-bar mechanism to move a lid on a shipping container up and over so that it rested on the top of the container during loading. There was extra credit awarded to those who could analytically arrive at a design using MATLAB (or some other language). I made my own solution for fun and to check student's solutions. I tried to get it to plot each snapshot (in magenta) but it was not correct and I didn't have time to figure it out.
So, here's how to design a typical four-bar linkage:
Define the desired locations of your coupler. You define two or three locations you want the coupler to travel through. My code only works with three locations.
Find the locus of points that is equidistant from where the crank connects to the coupler in each position. If two coupler positions are defined, the locus will be an infinite line (think of the intersection of two circles; connecting the two points forms a line). If three coupler positions are defined, the locus collapses to a single point. This point also happens to be the circumcenter of the triangle formed by the three vertices the bearing must be equidistant from. The bearing must be placed on a point on the locus. If the locus is a single point, your job is done. If the locus is a line, you must select a point on that line to be your bearing location.
Repeat step 2 for the points where the rocker connects to the coupler.
Once you have the bearing locations for the crank and the rocker, you can easily calculate the lengths of each link.