The Chinese community is celebrating the Chinese New Year in this week. Each year of the Chinese calendar is associated with a particular animal. This year is the Pig. To celebrate the new year and to brush up our programming/math skill, let us draw a few pigs by code. The pigs we are going to draw actually consist of some ellipses and parabolas. In case you forget, an ellipse is basically a flattened circle like this one.
This ellipse has the center \((u,v)\), width \(a\), and height \(b\). For a general ellipse, \(a\) is not necessarily equal to \(b\). However, when \(a=b\), we call it a circle and \(a\) the radius.
An ellipse can be represented by the equation:
We can also use parametric equations to represent an ellipse:
y&=v+b\cos(t),\quad 0\leq t\leq 2\pi.
The parametric form is actually more convenient for plotting since it gives the \((x,y)\) point lying on the ellipse. Each \((x,y)\) point is associated with the parameter \(t\). Here, \(t\) goes from 0 to 180 degrees (i.e., 0 to \(2\pi\) in radians) and it means the full ellipse. If we just want to plot part of the ellipse, we can use other different intervals of \(t\). The following function draws an ellipse with given center \((u,v)\), width \(a\), height \(b\), and line color.
ellipse.m: Draw an ellipse with given center, width, height, and line color.
Then, we can plot an ellipse with center \((u,v)=(0,1)\), width \(a=5\), height \(b=6\), and red line, as shown in the following.
Drawing an ellipse with center (0,1), width 5 and height 6.
Our pigs consist of several ellipses, which represent the body, head, eyes, eye balls, nose, and nostrils. The mouth and ears are shifted and scaled parabolas. Finally, the tail is simply a cosine curve. The whole construction of a single pig with center \((x_c,y_c)\) is shown in the following function.
pig.m: Draw a pig, with center and line color.
For example, we can use the function to draw a pig centered at \((x,y)=(2,3)\). The result is shown below.
Drawing a pig with center (2,3)
Finally, we want to plot 4 pigs with centers \((0,0)\), \((35,0)\), \((35,30)\), \((0,30)\). This can be done using the script below. And, finally, we have 4 cute pigs to celebrate the new year!
draw.m: Drawing pigs, one by one.