![]() |
|
Sierpinski gaskets are a type of fractal. They have the property of being "self-similar", that is, they are made of sub-parts that are similar (in the Euclidean geometry sort of way) to the whole. The Sierpinski gasket is a triangle formed of triangles formed of triangles, etc. etc..
In this and the following lecture, we will explore how we would go about writing code to display a Sierpinski gasket and how we can use the tools of abstraction we've already developed to create a more general fractal generator.
The most important parts of this whole process are the questions that are asked at each stage. Be sure to record what questions were raised as this may be more important than the answers. The answers pertain only to today's problem, while the questions apply for every problem.
We need to understand what we are working with before we can possibly write code for it. What is a Sierpinski gasket anyway?
A Sierpinski gasket is either
-- a triangle made of straight lines, or
-- a triangle made of Sierpinski gaskets.
If that don't reek of recursion, I don't know what does!
We can clearly see that a Sierpinski gasket is expressible in terms of a base case and an inductive case.
What we'd like to do is to write a functions that will enable us to
Choices: Create and draw separately or create and draw all at once. We'll first try them separately, then we'll look at doing it all at once.
Does this all really about just Sierpinski gaskets? Does it really matter that we are dealing with triangles and triangles-of-triangles? Is that what we should be programming for?
We will write some simple code first, to get a feel for what is going on:
FactoriesFactories are functions that make other structures or functions for the purposes of abstracting or encapsulating the construction process.
Factories often involve curried functions. Just returning a function or a structure from a function doesn't make it a factory. Being a factory also involves the semantics (the meaning) in which it is being used. For more information, see this web page on the Abstract Factory Design Pattern |
The day's code can be found here: lec26.scm
Pre-written code that might be useful: sierpinski.scm <-- this is not "the solution". The solution depends on the class discussion.
©2003 Stephen Wong