Previous solution induce that the turtle teleports to other location, or at
the very least, that it moves its pen up during the drawing. Indeed, the end
of the drawing of the first recursive call does not match the beginning of
the second recursive call. That is why we had to use the method
setPos()
In this lesson, you will write a recursive method allowing to draw the dragon curve without taking the pen up. For that, we need another recursive method drawing the mirror side of the curve.
The method dragon()
is then recursively defined using itself
and dragonReverse()
. Likewise, the method
dragonReverse()
is defined recursively using itself and
dragon()
. This is thus an example of mutual recursion.
The prototype of the dragon()
method remains unchanged from
previous exercise:
[!java|c]void [/!]dragão([!java|c]int [/!]ordem[!scala]:Int[/!], [!java|c]double [/!]x[!scala]:Double[/!], [!java|c]double [/!]y[!scala]:Double[/!], [!java|c]double [/!]z[!scala]:Double[/!], [!java|c]double [/!]t[!scala]:Double[/!])
The new point's coordinate (u, v) introduced by the dragon()
are:
u = (x + z)/2 + (t - y)/2 v = (y + t)/2 - (z - x)/2
The prototype of the method dragonReverse
is similar:
[!java|c]void [/!]inverterDragão([!java|c]int [/!]order[!scala]:Int[/!], [!java|c]double [/!]x[!scala]:Double[/!], [!java|c]double [/!]y[!scala]:Double[/!], [!java|c]double [/!]z[!scala]:Double[/!], [!java|c]double [/!]t[!scala]:Double[/!])
The new point's coordinate (u, v) introduced by the
dragonReverse()
are:
u = (x + z)/2 - (t - y)/2 v = (y + t)/2 + (z - x)/2
To make the work of each method recursiv more visible, the line painted by
the dragon()
must be red (Color.red
) while the
line painted by the dragonReverse()
must be blue
(Color.blue
).
Have a look at each world's objective view to understand how to write the function.