HW9_Spring25
HW9_Spring25
Spring 2025
th
Deadline: May 29 , 11:59 pm
In this question, you are required to draw a shape called “Fibonacci Spiral” in Figure 1,
which is obtained with Fibonacci numbers (we assume that you are familiar with Fibonacci
Sequence). You need to draw this spiral in a recursive way. This spiral is nothing but squares
and circles with side length and radius of Fibonacci numbers that are drawn sequentially to
certain coordinates. In other words, each square and circle have side and radius length of
a Fibonacci number, and they are drawn to coordinates that are determined according to a
pattern that is repeated in a number of iterations.
To draw each square, you can use the DrawRect function provided to you, which takes the
following as input:
• Side lengths,
• Fill color.
The bottom left corner coordinates of each rectangle are updated according to a repeating
1
pattern. Let the side length of the square to be drawn be fn . Bottom-left coordinates of a
square will be obtained by using the coordinates of the previous square. To do this, the x and
y-coordinates of the previous square will be updated according to a pattern that depends on
the values of fn and fn−1 . For example, let x and y coordinates of the blue square (square of
f4 ) be (1,-2).
Then x and y coordinates of the square of f5 (shown in khaki color) is obtained as:
x5 = x4 + f4 − f5 = 1 + 3 − 5 = −1
y5 = y4 + f4 = −2 + 3 = 1
To draw each circle, you can use the DrawCircle function. Please note that this function draws
a 90-degree arc of the circle. This function takes the following inputs:
• Radius,
Please note that the DrawCircle function does not take color as input because circle color is
already provided in DrawCircle function, which is black. The bottom left corner of the circle
has the same coordinates used in the DrawRect function. The radius of the circle to be drawn
is fn . For circles, you can see the coordinate regions in Figure 2. If you will draw the top-right
st
quarter of the circle inside the 1 rectangle, then the coordinate region should be 1. For the
nd nd
2 rectangle, 2 coordinate region should be chosen to draw the top left quarter of the circle
and so on. That is, the last input of DrawCircle function should be 1, 2, 3, or 4.
You are required to design an algorithm to develop a pattern for updating the coordinates. It
is recommended to draw the shape with the first 10 Fibonacci numbers (without using a com-
puter), draw a table and find relationships between the bottom-left coordinates of consecutive
squares. To do this, you need to set a coordinate for the first square you draw. You may set it
as (0,0) or anything else you want.
After generating the rule, you are ready to write your code. The main script (F ibonacci M ain.m)
is provided to you. As can be seen in the main script, the Fibonacci numbers that you will
draw are given in an array f . Therefore, you do not need to calculate the Fibonacci numbers.
You are required to define a recursive function with the name f ibonacciSpiral ID1234567
that draws squares and updates coordinates, side length, and color (you should randomly
generate the color of each square – color is an RGB vector containing 3 numbers between 0 and
1).
2
Your function must insert a value of each square as text to the middle of that square. For this,
you may use text function. This function takes x and y coordinates and the value of the text
(string) as input arguments. For example, text(1,2,num2str(1),’FontSize’,10) sets Font Size as
10 points and inserts the text 1 to the coordinates (1,2) on the current figure. num2str is a
function that converts numbers to strings.
There is a popular short story called “The Ugly Tree,” which is a didactical one. In this
question, you will draw The Ugly Tree’s dream form. Despite the fact that being ugly saves its
life, it always dreamed about being a beautiful tree, which follows a regular pattern repeating
on each sub-branch.
The pattern shape is provided in the top left corner in Figure 3. In this pattern, we have
three lines: Main Branch, Branch A and Branch B. The angle between Main Branch and
Branch A is 40 degrees. The angle between Main Branch and Branch B is 40 degrees as well.
Branch A is on the left of the Main Branch, and Branch B elongates on the right-hand side.
The length of the Main Branch is 60 units. Lengths of Branch A and Branch B are equal to
70% of the length of the Main Branch, so both are 42 units. Connection points of the branches
to the Main Branch are as follows:
• The intersection point of Branch A and Main Branch is 60*1/3 above the bottom point
of the Main Branch.
• Branch B intersects the Main Branch from the middle point.
The same pattern is repeated for each Branch A and Branch B accepting them as the Main
nd rd
Branch (see the 2 and 3 shapes in Figure 3). Then the tree will grow, and he will obtain
the shape that is called“Resulting Shape” in Figure 3.
3
If the process described above is repeated in a recursive way, the shape in Figure 4 will be
drawn. You are required to write a recursive function with the name DrawTree ID1234567 to
obtain the shape that is the same as the one in Figure 4 (You may change the color of the
tree, though). Dimensions of the initial pattern are given in Figure 3 – you may use the same
values. In each iteration, you need to update the length and coordinate values of the pattern
(Coordinates are not provided in the figure, but you may start with the initial coordinates with
(0,0)).
The termination condition for growing the tree is determined by the number of levels in the tree.
For the tree in Figure 4, it is determined as 9. You may try different termination conditions,
such as the length of the last Main Branch or the number of levels, etc.
The main script main.m is provided on ODTUClass. You may define additional variables if
needed.
You should submit two script files, the main script mainQ2 ID1234567.m and DrawTree ID1234567.m,
to ODTUClass.
As industrial engineers, we aim to optimize if possible; in other words, we look for efficiency.
However, we are not the only ones working on efficiency. Electrical and computer engineers
also think about it. In this assignment, you are going to design efficient microprocessors. To
serve this aim, the delay in routing time signal from the center clock to each part (endpoint)
of the network should be the same. Therefore, after many years of research, it is seen that the
following design in Figure 5 is a good and space-efficient layout for microprocessor design.
Let us have a look at this design in detail. Below, you may see the design step by step. First,
we use two vertical lines and one horizontal line of given lengths and place them as in Figure
6 (a). This also resembles the letter H. After that, we decrease the line length and width to
half, and using this length, we draw an H at each end of the previous H. In Figure 6, you may
see this procedure done for 4 iterations.
4
Figure 5: An efficient microprocessor design
You are not going to use loops to implement this problem, but use recursion. Therefore, you
will need a main script and a function which will be called recursively. In other words, the
scripts not using recursion idea will not be graded. The input parameters are provided
in an Excel file called Input P arameters Spr2025.xlsx. You need to import the related values
from this Excel file and assign them to variables accordingly. The structure of this file will be
as in Figure 7.
In the first iteration, you should center your H at the given x and y coordinates. The Length
parameter will refer to the length of H, where the W idth is the width of H. Level parameter is a
non-negative integer indicating the level of the recursion. The last three parameters refer to the
red, green, and blue color components of the figure, respectively. At each level of recursion, you
need to halve the Length and W idth parameters. You will also halve the Color parameters in
a way that when the level increases, the colors become darker. Using the updated parameters,
you must continue drawing the figure recursively.
Please do not forget to use axis equal on comment so that the center of the figure can be seen
clearly. You need to expect a figure having the biggest H in a darker color, i.e., as the level
increases the color of H ’s should be in darker colors. You are going to upload two files, the
main script named HW9 ID1234567 Q3, and a function.