Python代写:CS170 Turtle Spiral

用Python的Turtle库进行图像绘制,从简单的点线图到复杂的Mickey图样。

Submission instructions

Submit your assignment through the QTest system, using class ID CS170 and exam ID hw4. No email submissions are accepted. No late submissions are accepted.

General instructions and hints

In those problems asking you to write a method, always call the method several times to test that it works properly with a variety of different values for the parameters. Test it on more examples than the ones shown in this handout. Even if the problem asks you for just one method, you can always write additional helper methods to simplify and organize your code. Make sure you write comments to explain your code.

Problem 1: Is reverse

Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if
s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf.

1
2
3
isReverse("happy", "yppah") will return true
isReverse("cool", "loac") will return false
isReverse("", "") will return true

Problem 2: Turtle spiral

Using turtle graphics and recursion, Write a method turtleSpiral(Turtle t, double size, double minSize) that draws squared spirals like the one in the figure. The spiral starts from the outside (the longest side) and ends with the shortest side. The longest side has length equal to size, and each following side is 10% shorter than the previous one. The length of the shortest side is no shorter than minSize.
Then write the same method again, but using iteration instead of recursion. Call this method turtleSpiralIter(Turtle t, double size, double minSize). Make sure the two methods draw exactly the same spirals in all the cases. Take this opportunity to reflect about similarities and differences between iteration and recursion.

Problem 3: Fractal flowers (2 points for simpleFlower; 5 points for fractalFlower)

Using turtle graphics, you will create beautiful fractal flowers.

3.A

Write a method simpleFlower(Turtle t, double size) that takes a Turtle and a number
representing the size of the flower. Your method will draw the following figure:

The position and orientation of the flower will depend on the initial position and orientation of the turtle. The origin of the flower is the bottom part of the stem, and its length develops straight ahead on the current direction of the turtle. When your method terminates, the turtle should be back to the same position and the same orientation it started from.

3.B

Write a method fractalFlower(Turtle t, double size, int level) that takes a Turtle, a
double representing the size of the flower, and an integer representing the maximum level (depth) of recursion.

Your method will paint a recursive fractal flower with the same basic shape outlined above, but with each petal recursively replaced by a scaled down version of the flower itself. For example, a fractal flower with a maximum recursion level of 4 will result in a picture like this one:

Your program should include some test code that draws three flowers, all of them facing up, on three different positions of the screen: (a) a simple flower of size 200; (b) a fractal flower of depth 3 and size 250; and (c) a fractal flower of depth 4 and size 300. Your three test flowers should not overlap.

Notice that the method simpleFlower that you implemented before was only for understanding and practicing with the structure: you don’t need to call that method in fractalFlower.

Problem 4: Fractal Mickey Mouse

Using turtle graphics, you will create a funny fractal cartoon character inspired by Disney’s Mickey Mouse.

4.A

Write a method mickeyFace(Turtle t, double r) that draws a circular-shaped face of a Mickey-like character, without ears. This method takes a Turtle and the radius r of the face. The turtle starts and end drawing in the center of the face. The exact details of the face are up to your creativity (they don’t necessarily
have to be identical to the picture); just make sure it has at least two eyes, a nose, and a mouth.

4.B

Using your previous method mickeyFace, write a method fractalMickeyMouse(Turtle t, double r, int level) that recursively draws a character with the face you previously defined, and a recursively downsized version of that same face as ears. Your method will take as parameter a Turtle, the radius r of the face, and an integer level representing the maximum depth of recursion. The radius of each ear is exactly half the radius of the face. Hint: if you do not manage to complete mickeyFace on time, you can replace it with a plain circle of the correct size. The following figure shows an example of a Fractal Mickey Mouse with maximum recursion level (depth) of 6.

Your program should include some test code that draws a Fractal Mickey Mouse of depth 6.

Hint: If you want to fill areas with colors you can use the method fill() from the Turtle library. This method flood-fills an area with the turtle’s current color. Just move the turtle inside the area you want to color (it won’t work if it is on the border) and call t.fill(), where t is a Turtle object.

Problem 5: Fractal sun

Using turtle graphics, you will paint a majestic fractal sun. The fractal sun is composed of a circle of radius r, and 8 rays of length 2*r originating from the center of the circle and radially equally spaced. The external tip of each ray is also the origin of a recursively downsized fractal sun with radius equal to 1/4 of the radius of the sun at the previous level. Also, the suns originating at the tip of the rays will have different colors, i.e., the color of a sun is a method of the recursion level of the fractal sun itself. You can invent the coloring method you prefer, just make sure that your sun will look good no matter what the maximum level of recursion is going to be. Your fractal sun will be generated by a method fractalSun(Turtle t, double r, int level) which you will write.

Your method will take as parameter a Turtle, the radius r of the sun’s circle, and an integer level representing the maximum depth of recursion of your fractal sun.

The following picture shows a fractal sun with a maximum recursion depth of 5, with colors fading from yellow to red.

Your program should include some test code that draws a fractal sun of depth 5.

Problem 6: Your own fractal

Using turtle graphics, design and implement your own fractal. Be creative! Your program should include:

  • A comment section at the top of the code explaining the structure of the fractal.
  • A creative usage of colors that change as a function of the level of recursion.
  • Some controlled randomness to vary the structure of your fractal and make it more interesting.
  • In the main method, you should draw several versions of your fractal with different depth of recursion.

Bonus points: Early submission

If you submit the entire homework no later than 48 hours before the deadline, and the total score on the rest of this homework assignment is at least 20 points, you will receive 4 bonus points. The bonus point will be added to the total score of this homework assignment.

Good luck and have fun!