This assignment makes use of the files contained in this zip file. This assignment is due Tuesday, October 18.
This assignment and your previous assignment are about the Scene
data structure which describes the geometry of what the renderer should draw. For this assignment, you will define several Model
data structures that represent fractal shapes and then test your models in an interactive program.
As we have said in class, a Scene
is a list of positions that hold models. A Model
is a list of vertices in 3-dimensional space (which we call "camera space") and a list of line segments. Each Vertex
contains three doubles (for the x
, y
, and z
coordinates of a point). Each LineSegment
contains the (integer) index for each of two Vertex
objects from the Model
's vertex list. The vertices and line segments combine to form a wireframe shape that we see as a geometric object in the scene.
In the zip file there is a package, fractals
, of seven Java source files. Each of those files defines a sub-class of the Model
class from the scene
package. The files Canopy.java
and KochCurve.java
are complete. You need to complete the other five files in the package. These are two-dimensional models (all the vertices are in the xy-plane). For each model, you need to determine the Vertex
and LineSegment
objects that the model needs and then write the code that instantiates those objects and puts them into the model. Use the two completed models to help you figure out how to complete the other five models. Each model in the package has completed Javadocs. The Javadocs include all the formulas that you need to define the fractal shapes.
If you want to see more examples of Model
classes, look at the files in the models
package in renderer_2.zip. In particular, look at the files Square.java
, ,
Circle.javaCube.java
, and Tetrahedron.java
, because those are the simplest models.
In the zip file there is a program InteractiveModelsAll.java
that instantiates examples of all the fractal models. Use this program to interactively test your definitions of the models. Work on one model at a time. Comment out from InteractiveModelsAll.java
the code that instantiates models that you have not completed. The program InteractiveModelsAll.java
tests the two completed models first, so you can run InteractiveModelsAll.java
even before you write any code.
This assignment uses renderer_2 (instead of renderer_1). Renderer 2 has code in it that supports writing interactive programs (like InteractiveModelsAll.java
). You do not need to understand how the interactive code works. You don't need to look at the code in InteractiveModelsAll.java
. Next week we will start to talk about renderer_2 and interactive code (and in the next programming assignment you will write some interactive code). For this assignment, you only need to understand the scene data structure and how to define a Model class.
In the zip file there is a folder called demo
which contains a demo version of the completed assignment. Double click on the file hw3_demo.cmd
to run the demo program (you may be able to run the program by double clicking on the hw3_demo.jar
file).
Fractals are examples of recursive shapes. Like recursive data structures, recursive shapes are best defined using recursive functions. Defining recursive shapes and recursive data structures using recursive functions is often referred to as "structural recursion".
Recursion can be difficult, but structural recursion is the most accessible kind of recursion and it is often used as a way to introduce the idea of recursion. And fractals make the recursive definitions visible.
The trick to figuring out a fractal recursion is to describe the geometry of the recursion in words, and then translate the words into code.
For example, consider the Sierpinski Triangle. Given three corners, p0
, p1
, p2
, of a triangle, you need to create three new triangles.
+ p0 / \ / \ / \ /_______\ /\ /\ / \ / \ / \ / \ / \ / \ +--------|--------+ p2 p1
The first triangle has corners p0
, the midpoint between p0
and p1
, and the midpoint between p0
and p2
. The second triangle has corners p1
, the midpoint between p1
and p2
, and the midpoint between p1
and p0
. The third triangle has corners p2
, the midpoint between p2
and p0
, and the midpoint between p2
and p1
. So the recursion would look something like this.
triangle(p0, p1, p2, n) { if (n > 0) { triangle(p0, (p0 + p1)/2, (p0 + p2)/2, n-1); // recursion triangle(p1, (p1 + p2)/2, (p1 + p0)/2, n-1); // recursion triangle(p2, (p2 + p0)/2, (p2 + p1)/2, n-1); // recursion } else { addLineSegment(p0, p1); addLineSegment(p1, p2); addLineSegment(p2, p0); } }
For another example, consider the H tree. Given the two endpoints, p0
and p1
, of a line segment, you need to create two new line segments perpendicular to the given line segment and each with half the length.
p2 p4 + + | | | | p0 +---------------------+ p1 | | | | + + p3 p5
The recursion would look something like this (the formulas for p2, p3, p4, and p5 are in the Javadocs for H_Tree.java
)
hTree(p0, p1, n) { addLineSegment(p0, p1); if (n > 0) { // use p0 and p1 to compute p2, p3, p4, p5 hTree(p2, p3, n-1); // recursion hTree(p4, p5, n-1); // recursion } }
Turn in a zip file called CS455Hw3Surname.zip
(where Surname
is your last name) containing InteractiveModelsAll.java
and your sub folder fractals
.
This assignment is due Tuesday, October 18.