cs1101s Mission - 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS1101S, Semester I, 2014/2015Mission 7 1

National University of Singapore


School of Computing
CS1101S: Programming Methodology
Semester I, 2014/2015
Mission 7
Diagnostics and Dragonize
Start date: 02 September 2014
Due: 06 September 2014, 23:59
Readings:
Textbook Sections 1.3.3
Background:
Grandmaster Martin is delighted that his disciples have all made it thus far. He prepares for his
nal lesson on curves and also gives an encouraging speech to his disciples to prepare them.
Thats it, you are getting the basics of the Force. Before we start on the nal mind strengthening
training, we must rst analyse how your mind can work differently to get the same image. Yes
you must nd the fastest way to get that image...
With this in mind, you will now prepare for the nal training: The Dragon Curve. Use all the
techniques I have taught you earlier to get this right. Once you get it right, you will appreciate
the beauty of being in control of the Force in your mind...
This mission has two tasks.
Task 1:
One of your fellow disciples, Mingyu, isnt entirely happy with the style of several of the denitions
found in this training. In particular, he feels the code goes overboard in inventing names for
values that are used infrequently, and this lengthens the code and burdens someone reading the
code with remembering the invented names. For example, he thinks the denition
function rotate_around_origin(theta) {
var cth = Math.cos(theta);
var sth = Math.sin(theta);
return function (curve) {
return function(t) {
var ct = curve(t); // Mingyu eliminates the declaration of ct
var x = x_of(ct);
var y = y_of(ct);
return make_point((cth
*
x) - (sth
*
y),
CS1101S, Semester I, 2014/2015Mission 7 2
(sth
*
x) + (cth
*
y));
};
};
}
would be a bit more readable if the name ct for the value of curve(t) was dropped. He proposes
instead:
function mingyu_rotate(theta) { // rotates around origin, but less efficiently
var cth = Math.cos(theta);
var sth = Math.sin(theta);
return function (curve) {
return function(t) {
var x = x_of(curve(t)); // Mingyu writes (curve t)
var y = y_of(curve(t)); // twice
return make_point((cth
*
x) - (sth
*
y),
(sth
*
x) + (cth
*
y));
};
};
}
The experienced curve instructor, Minqi, warns Mingyu that the var declarations he uses in
mingyu rotate are actually computationally expensive compared to just using the abbreviation
ct.
1. Does mingyu rotate work and achieve the same purpose as rotate around origin?
2. Briey explain why using mingyu rotate as a subfunction in place of the original rotate around origin
in the denition of gosper curve will turn a process whose time is linear in the level into
one which is exponential in the level.
Task Files
mission 7 1.js
Task 2:
We can now invent other functions like the Gosper process, and use them to generate fractal
curves.
A model of a Heighway dragon curve can be made by repeatedly folding a strip of paper always to
the same side, and then unfolding it so that all angles are at 90 degrees, as shown in the gure
below.
CS1101S, Semester I, 2014/2015Mission 7 3
The above gure presents a way to obtain a dragon curve of order 4. A curve of order of magnitude
200 is presented in Figure 1.
Another fellow disciple, Thien, is trying to write a function that would replicate this process in
order to draw the dragon curve. He says nothing easier, all I need to do to produce a curve
of order n is connecting the curve of order n 1 and its rotation by 90 degrees at their ends,
and then put the resulting curve in standard position. So, Thien comes up with the following
function:
function thien_dragonize(n, curve) {
if(n === 0) {
return curve;
} else {
var c = thien_dragonize(n - 1, curve);
return put_in_standard_position(connect_ends
((rotate_around_origin(-Math.PI / 2))(c), c));
}
}
However, the function doesnt quite work. Thiens good friend, Minqi, suggests that the following
function should also be employed in the code:
function invert(curve) {
return function(t) {
return curve(1 - t);
};
}
This function inverts the curve, in the sense that it traverses the curve in the reverse direction
(the same curve would appear on the screen). To understand the difference, draw in separate
windows the following two curves.
connect_ends(unit_line, unit_circle);
and
connect_ends(invert(unit_line), unit_circle);
(Note: use draw connected full view to make the curves t in the windows. It is used in the
same way as draw connected squeezed to window. Look at the top part of the gure to see the
difference.)
Help Thien solve the problem.
Write a function dragonize that produces Heighways Dragon Curve. The function should be
similar to Thiens, and it should also make use of the invert function. Test your code and make
sure you can reproduce the drawing in Figure 1 with:
(draw_connected_squeezed_to_window(1000))(dragonize(200, unit_line));
Task Files
mission 7 2.js
CS1101S, Semester I, 2014/2015Mission 7 4
Figure 1: Sample Drawing for Task 2.
Submission
To submit your work to the Academy, copy the contents from the template le(s) into the box that
says Your submission on the mission page, click Save Code, then click Finalize Submission.
Note that submission is nal and that any mistakes in submission requires extra effort from a
tutor or the lecturer himself to x.

You might also like