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 traingle 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.
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. Factories encapsulate and abstract the construction (instantiations) of structures and functions. Through the use of closures, a factory can produce multiple outputs (.e.g. lambdas) that can remain connected and thus exhibit cohesive and correlated behavior. 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 a solution. The solution depends on the class discussion.
©2002 Stephen Wong