Scripting the Seeds of a Sunflower
Scripting the Seeds of a Sunflower
In this tutorial you will learn some elementary applications of CindyScript, the internal Programming language of Cinderella. You should be aware that this tutorial covers only a very small portion of the CindyScript functionality. Nevertheless, you will receive an impression of how CindyScript can be used.
The following example will be developed step by step. In each step we will add one conceptually new element. At the very end we will arrive at an interactive visualization of the packing pattern of seeds in a sunflower head.
A first line of CindyScript
We start with an extremely simple task. We want to draw one point by CindyScript that is bound to a given geometric point. We start by adding one point using the add a point mode . It is a good idea for the following example to also turn on the snap grid by pressing the button in the button line below the drawing surface. After this step your drawing should look like Figure 1.
CindyScript is (among other things) capable of adding graphical elements to your construction. Now we want to draw a single point displaced by (1,1) from point A. In order to achieve this we must open the CindyScript editor in the menu Scripting --> Edit Scripts. Then the window shown below will pop up. This is where CindyScript code is entered primarily.
You will notice that on the left side of this window there is a list of many occasions. Each piece of CindyScript is associated to one of these occasions and will be executed when a corresponding event occurs. We will add a piece of script in the draw event. This means that the script will be executed right before drawing. This is the right place to add code that produces additional graphical output. For adding the piece of code we click on the draw event (see Figure 3).
After this we can add a piece of code in the main part of the editor. To draw the desired point the code snippet shown in Figure 4 is already sufficient.
We will comment on this line of code briefly. The
After entering the line of code you must press the script start button (the one with the two gears) in the script editor. After this your drawing should approximately as shown in Figure 5.
Now we will add a simple control structure to our script. Instead of one single point we want to draw a whole sequence of points. For this we will use the
This code draws all together 10 points. The variable
In the next step we want to place the point cyclically around point A. The distance from A should be 1 and the points should have an angular displacement of 10°. For this we define a new variable
Adding a slider
We now want to interactively control the number of points added by a slider in the drawing surface. Sliders are constructed explicitly as geometric objects. For this one usually adds a line segment and places a point on it. However also other types of sliders (for instance circular sliders) are possible variants. We add a simple segment by using the add a segment mode. The endpoints of this segment will be B and C. Then we add a point on this segment by using the add a point mode. This point will be point D. We will now use the distance from C to D as a controlling entity for the number of points. The distance can be measured by
A strange angle
We will now do something the importance of which may not be clear immediately. We change the angular displacement to a value of 137,508°. The result of this action is shown in Figure 9 below.
The points seem to be distributed almost randomly. In fact the value of 137,508° has a certain mathematical meaning. It is 360° times the square of the golden ratio. It has the strange property that it scatters the points in an kind of optimal way. Each new point is placed in a position that is as far away from the previous points as possible. We can see this by changing the distance of each point to the center A. A good choice of this radial displacement is sqrt(i)*0.2. We do so by multiplying the vector we add by a factor
Let us be ambitious and increase the number of points significantly. We change our factor 10 in the first line to a value of 80. We recommend to move the points A and D to see the speed of the visualization. A Situation with roughly 400 points is shown in Figure 11.
Spirals in a sunflower
The points we drew are roughly positioned like the seeds in the head of a sunflower. You will also observe the typical spiral structures that one sees in a sunflower. The angular displacement factor of 137,508° is crucial for this pattern. We will exemplify this by slightly disturbing this angle and seeing what happens. For this we add another slider with which we can control the amount of disturbance. We again draw a segment with a point on it and add define a (small!) disturbance value by
The number of spirals is intimately related to the golden ratio and its development by continued fractions. We will not elaborate on this (quite deep) mathematical structure. We just mention that the so-called Fibonacci numbers 1,1,2,3,5,8,13,21,34,... come into play. Here each number is the sum of its two predecessors. Dividing two consecutive Fibonacci numbers by each other gives a better approximation of the golden ratio the larger these two numbers are. Amazingly the number of prominent spirals will be a Fibonacci number (pick a real sunflower and check it!).
We will exemplify this by changing the color of the points in our visualization. Many CindyScript functions (and in particular
Contributors to this page: Richter
The content on this page is licensed under the terms of the License.