Today is Valentine's Day. So, we are going to draw some hearts, and then send them all to our lovers. Of course, we are going to do it by coding and math. This is what we are going to accomplish today: The outline of a heart is a path of points $(x,y)$, which are given by the following parametric equations: $$ \begin{align} x&=x_c+16\sin(t)^3,\\ y&=y_c+13\cos(t)-5\cos(2t)\\ &\quad\quad\quad-2\cos(3t)-\cos(4t), \end{align} $$ for $-\pi\leq t\leq \pi.$. Here, $(x_c,y_c)$ is the center of the heart. You can change the location of the heart by changing $(x_c,y_c)$. We implement the above parametric equations in SIMO as a function like the following one: aheart.m
Now, you can run the function above with center $(0,0)$ to produce the following heart. This heart has only 30 path points, and therefore is not very smooth. You can make it smoother by increasing the number of path points. Next, we need to duplicate this heart many times with different centers $(x_c,y_c)$. The centers are in fact path points on a bigger heart. The first part of the following code generates path points of the bigger heart. We need to scale it up 10 times bigger than the normal heart. These path points will serve as the centers of the smaller hearts, which will be drawn in the for-loop. It loops through each of the path points of the bigger hearts, and each of them is the center point of a smaller heart. manyhearts.m
If you run the above script, you will get the following:. Finally, you can save the plot as an image using the menu on the plot in SIMO. Then, you can find the saved picture in the Photos app on your iPhone or iPad. You may want to do some editing like cropping and filters. Finally, you can send it to your lover! Happy Valentine's Day!
0 Comments
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: $$ \frac{(x-u)^2}{a^2}+\frac{(y-v)^2}{b^2}=1. $$ We can also use parametric equations to represent an ellipse: $$ \begin{align} x&=u+a\cos(t),\\ y&=v+b\cos(t),\quad 0\leq t\leq 2\pi. \end{align} $$ 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.
(This post was inspired by the project MathRomance, of which the author is a team member.) ## What is the Golden Ratio?Probably, you have heard about the Golden Ratio. In a nutshell, it refers to a number roughly equal to 1.618. The number could be "seen" by drawing two lines of lengths 1 and 1.618, as shown below. The black and red lines have lengths 1 and 1.618, respectively. And their ratio is roughly equal to the Golden Ratio. I bet you have seen many man-made or natural objects that exhibit this or a similar proportion. That's why many people believe that the Golden Ratio is associated with beauty in our universe. Therefore, whether it is a coincidence or not, many graphic designs and artworks are created seemingly to follow the golden rule. ## Estimating the Golden RatioYou may find it disappointing if I tell you that there is no way to obtain the exact numerical value of the Golden Ratio. Well, actually, the Golden Ratio is an irrational number. Simply put, an irrational number has an infinite number of digits behind the decimal point. (More formally, a number is irrational if it cannot be represented by a fraction of two integers). For example, $\sqrt{2}$, $\pi$ and the exponential $e$ are irrational numbers. Any attempt to write $\pi$ with finite number of digits (e.g., 3.14) will result in an approximation at best. A simple method to obtain an approximation of the Golden Ratio is to use the Fibonacci sequence. The first number in the sequence is $F_0=0$, the second is $F_1=1$, and the third is the sum of the first two, i.e., $F_2=F_1+F_0$. In general, a number in the Fibonacci sequence is the sum of the previous two: $$ \begin{align} F_0&=0\\ F_1&=1\\ F_n &= F_{n-1} + F_{n-2},\quad n=2,3,\ldots \end{align} $$ It is well-known that the Golden Ratio can be approximated by the ratio for a large $n$: $$ \frac{F_n}{F_{n-1}}. $$ Our app SIMO comes with a function fibonacci to calculate Fibonacci numbers. You can use the following code to approximate the Golden Ratio: And the result will be: In fact, the larger $n$ you use, the more accurate the approximation will be. It is because the ratio $F_n/F_{n-1}$ approaches (or converges, formally speaking) to the Golden Ratio. The convergence of $F_n/F_{n-1}$ is evidence from the following figure: |
## Mark Lau
SIMO Developer ## Archives |