CPSC 120 Principles of Computer Science Fall 2025

Topics: Fractals

Due: Fri 12/5 11:59pm


Introduction

Topics showcase applications of the core concepts, in this case drawing functions and recursion.

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 two different recursive patterns to create fractal models of natural things (tree canopies and mountains).

Successfully completing this topic means that you are able to:


Handin

To hand in your sketches:


Policies

Like labs, topics are individual assignments — what you hand in must be your own work, your own ideas, your own effort. You may get help in office hours and from Teaching Fellows, but you may not work together with a partner or in a group with others to create solutions or write code.

The policies on late work and extensions, academic integrity, and the use of AI for topics are the same as for lab 2. Review the policies there. One extension token is needed for revise-and-resubmit without an initial handin.

Also review assignments and evaluation on the Policies page for how topics factor into the final grade. The short version: topics are optional for a passing grade (C-), but achieving proficiency for at least some topics is required for a higher grade.


Preliminaries

Reference

Review the slides, examples, and in-class exercises from 11/14 for more about fractals and the additive and substitution patterns.

Processing API

Visit the Processing API to find out more about specific Processing functions mentioned below, such as dist. You can scroll through the list of all functions on the main API page or use the "filter by keywords..." box to quickly locate a specific function.


Exercises

Canopy

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:

Mountains

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:

Extra Credit

You can earn extra credit by going substantially beyond the required elements. If you do extra credit, save a copy of your canopy or mountains sketch with the name fractals_ec as a starting point, then modify the copy — don't change your original sketches.

Some possibilities: