CPSC 120 Principles of Computer Science Fall 2024

Lab 7
Fractals

Due: Fri 10/25 at the start of class

Labs are due at the start of class. It is OK if you show up and copy your files to the handin directory at the very beginning of class, but this should take at most a couple of minutes and you should not spend the next lab period finishing up the previous week's lab. I will check timestamps, and files handed in more than five minutes after the start of lab will be considered late.

See the policy on late work and extensions.


Introduction

Fractals are often used to describe natural things because, like many natural things, fractals have the property of self-similarity — which essentially means that you can zoom in as much as you want and will still see the same basic shape. Realistic modeling of nature is of particular interest in computer graphics.

In this lab, you'll use three different recursive patterns to create fractal models of natural things (mountains and plants) and then incorporate them into a scene.

Successfully completing this lab means that you are able to:

Academic Integrity and Collaboration

The short version:

Review the discussion in previous lab handouts and the full collaboration policy for more details.

Handin

To hand in your work:


Preliminaries

Getting Started

As with lab 6, the exercises in this lab are about applying the right pattern rather than figuring out how to draw each thing from scratch. For each exercise, review the slides and the in-class exercises handouts for a description of the relevant pattern and the examples in the slides, handouts, and posted on the schedule page for examples of what that pattern looks like in code.

Also read through all of each exercise before starting it — there's important information about how to do the task after the initial statement.

Reference

As always, refer to the class materials (slides, in-class exercises handouts, and examples) posted on the schedule page.

You'll need to make use of several potentially-new Processing functions in the exercises below. Look them up in the Processing API.


Exercises

Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in. (Refer back to lab 2 for more on Auto Format if needed.)

Exercise 1

Create a sketch lab7a which draws a fractal canopy as described below. Your drawCanopy function should have all of the parameters described (including b and s). When calling drawCanopy from draw(), use 2*PI/11 for b and 0.75 for s. Size and position the canopy so it fits nicely into your drawing window. (Use -PI/2 as the initial value for a to get the tree standing upright.)

Fractal Canopy

A fractal canopy can be produced by repeatedly adding two shorter line segments at the end of each previous line segment: (click any picture for a larger version)

This process follows the additive pattern — each level adds to what was drawn in the previous level. The elements of the pattern:

Implementation notes:


Exercise 2

Create a sketch lab7b which draws a fractal mountain range as described below. The mountain range should span the full width of the window. Experiment with the initial value for maxd so that the mountains are suitably rugged.

Fractal Terrain (Mountains)

Realistic terrain can be generated using a technique known as the midpoint displacement algorithm:

Start with a line segment connecting two points.
(The y coordinates of the points can be equal, as shown in the example, or not.)
Replace each line segment with two new line segments which connect the original segment's endpoints with the "displaced midpoint". The displaced midpoint is the midpoint of the original segment moved up or down by a small random amount.
Repeat: replace each line segment with two line segments connecting each segment's original endpoints with a displaced midpoint.
Repeat: replace each line segment with two line segments connecting each segment's original endpoints with a displaced midpoint.
Keep going, until the line segments are sufficiently short. At that point, draw the line segment.

To create solid terrain, replace the final "draw a line segment" step (when the line segments are sufficiently short) with "draw a quad whose top corners are the line segment's endpoints and whose bottom corners are at the bottom edge of the window".

The picture below shows an exaggerated view to illustrate this — the red area is the quad associated with the third line segment.

The process follows the replacement pattern — the line segment in each level is replaced by the two shorter segments, and only the final line (or quad) is actually drawn. The elements of the pattern:

Math notes:

Implementation notes:


Exercise 3

Create a sketch called lab7c which draws one of the fractal plants described below. (Your choice of which one.) Choose a reasonable maximum depth. (Start with a small value and increase slowly until you find something you like — it doesn't take a very big depth to produce something complex enough to overwhelm the system!) The plant should also be positioned nicely in the sketch window and the length of the line drawn should be such that the plant fits within the window.

Fractal Plants

L-systems were introduced in class as a way of describing certain kinds of fractal shapes, such as plants. Several examples of plant L-systems are given below.

 
angle 25.7 degrees 20 degrees 20 degrees 25.7 degrees 22.5 degrees
generator F F X X X
production rule(s) F → F[+F]F[-F]F F → F[+F]F[-F][F] X → F[+X]F[-X]+X
F → FF
X → F[+X][-X]FX
F → FF
X → F-[[X]+X]+F[+FX]-X
F → FF

You may get a mirror image of the picture shown — that's OK. (It's a result of different interpretations of whether a positive angle corresponds to a right or left turn.)

Additional specifications:

Implementation notes:


Exercise 4

Create a sketch called lab7d which contains a scene featuring fractals modeling natural things. As usual, there is considerable flexibility in what exactly you create as long as you meet the following requirements:


Extra Credit

You can earn extra credit by going substantially beyond the required elements. Some possibilities:

Include a brief description of what you've done for extra credit in a comment at the beginning of your sketch. More creative and challenging elements will earn more points.