FRACTALS. Week 13 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on a lab by James Barker. Pre-Laboratory Checklist

Similar documents
RECURSION. Week 6 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on material by James Barker. Pre-Laboratory Checklist

Discrete Dynamical Systems: A Pathway for Students to Become Enchanted with Mathematics

Fun with Fractals Saturday Morning Math Group

Symmetric Fractals. Seeking Sangaku Ramanujan, Hardy, and Ono

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Fractals: Self-Similarity and Fractal Dimension Math 198, Spring 2013

<The von Koch Snowflake Investigation> properties of fractals is self-similarity. It means that we can magnify them many times and after every

An Introduction to Fractals

Koch Snowflake Go Figure The Koch Snowflake is a fractal based on a very simple rule.

On Fractal Colouring Algorithms

Lecture 3: Some Strange Properties of Fractal Curves

Exploring Fractals through Geometry and Algebra. Kelly Deckelman Ben Eggleston Laura Mckenzie Patricia Parker-Davis Deanna Voss

TREES. Week 8 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on material by James Barker. Pre-Laboratory Checklist

Fractals. Materials. Pencil Paper Grid made of triangles

Koch-Like Fractal Images

Hopalong Fractals. Linear complex. Quadratic complex

MITOCW 2. IV: Consistency, Completeness, and Geometry

Generation of 3D Fractal Images for Mandelbrot and Julia Sets

Grade 6 Math Circles. Shapeshifting

Chapel Hill Math Circle: Symmetry and Fractals

Introduction to Programming

ITERATIVE OPERATIONS IN CONSTRUCTION CIRCULAR AND SQUARE FRACTAL CARPETS

Exploring the Effect of Direction on Vector-Based Fractals

In this lesson, students build fractals and track the growth of fractal measurements using tables and equations. Enduring Understanding

Fun with Fractals and Functions. CHAMP at University of Houston March 2, 2015 Houston, Texas

1

A Discussion of Julia and Mandelbrot Sets. In this paper we will examine the definitions of a Julia Set and the Mandelbrot Set, its

Mathematics 350 Section 6.3 Introduction to Fractals

CS 543: Computer Graphics Lecture 3 (Part I): Fractals. Emmanuel Agu

CS 106 Winter 2016 Craig S. Kaplan. Module 07 Recursion and fractals Topics. Recursion as an extension of hierarchical modelling Simple fractals

Graphics in IT82. Representing Graphical Data. Graphics in IT82. Lectures Overview. Representing Graphical Data. Logical / Physical Representation

Examples of Chaotic Attractors and Their Fractal Dimension

CS 463 Project 1 Imperative/OOP Fractals

Prezi Quick Guide: Make a Prezi in minutes

Session 27: Fractals - Handout

XaoS Notes. That means that I can give you a copy as long as I tell you the following:

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Gentle Introduction to Fractals

CS 4300 Computer Graphics. Prof. Harriet Fell Fall 2012 Lecture 28 November 8, 2012

Computer Graphics 4731 Lecture 5: Fractals. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Animations involving numbers

Projective geometry and the extended Euclidean plane

Representing Graphical Data

Computer Graphics (CS 543) Lecture 2c: Fractals. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Learning Plane Geometry Alvaro Briz Redon, Angel Serrano Aroca

p x i 1 i n x, y, z = 2 x 3 y 5 z

Filling Space with Random Line Segments

CS179: GPU Programming Recitation 5: Rendering Fractals

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Visualization Insider A Little Background Information

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

3D Hyperbolic Tiling and Horosphere Cross Section

the NXT-G programming environment

The (Math) Problem With Pentagons

Scientific Calculation and Visualization

Fixed Point Iterative Techniques An Application to Fractals

2,500 YEARS TOO LATE CLEANING UP THE MESS OF ZENO

Let s Make a Front Panel using FrontCAD

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Lecture 1: Overview

Some geometries to describe nature

Scope and Sequence for the New Jersey Core Curriculum Content Standards

Escape-Time Fractals

CS 106 Winter 2016 Craig S. Kaplan. Module 08 Randomness and noise Topics

Fractals. Investigating task farms and load imbalance

Fractals and the Chaos Game

Total Choas... Total Chosa

Lecture 6: Fractals from Iterated Function Systems. He draweth also the mighty with his power: Job 24:22

Order from Chaos. Nebraska Wesleyan University Mathematics Circle

The Beauty and Joy of Computing 1 Lab Exercise 9: Problem self-similarity and recursion - Python version

Self-Similar Snowflakes with Optional Fractal Extension

Math Dr. Miller - Constructing in Sketchpad (tm) - Due via by Friday, Mar. 18, 2016

Planar Graphs and Surfaces. Graphs 2 1/58

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

DISTRIBUTION. Week 11 Laboratory for Systems, Networks and Concurrency Uwe R. Zimmer. Pre-Laboratory Checklist

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

Geometry Scope & Sequence - Charles A. Dana Center, March 2006

Applications. 44 Stretching and Shrinking

On a coordinate plane, such a change can be described by counting the number of spaces, vertically and horizontally, that the figure has moved.

textures not patterns

GTPS Curriculum Mathematics Grade 8

EEN118 LAB FOUR. h = v t ½ g t 2

ME 442. Marc/Mentat-2011 Tutorial-1

Clouds, biological growth, and coastlines are

Game Mathematics. (12 Week Lesson Plan)

Creating a Poster in Google SketchUp

Fractal Geometry. Prof. Thomas Bäck Fractal Geometry 1. Natural Computing Group


View Frustum Culling with Octree

Fractals: a way to represent natural objects

Discovering. Algebra. An Investigative Approach. Condensed Lessons for Make-up Work

Fractal Dimension of Julia Sets

Textures and UV Mapping in Blender

Fractal Coding. CS 6723 Image Processing Fall 2013

Typing Control. Chapter Conditionals

PowerPoint Basics: Create a Photo Slide Show

Mathematical Cut-and-Paste: An Introduction to the Topology of Surfaces

Use the Move tool to drag A around and see how the automatically constructed objects (like G or the perpendicular and parallel lines) are updated.

Doyle Spiral Circle Packings Animated

Robust Stencil Shadow Volumes. CEDEC 2001 Tokyo, Japan

Transcription:

FRACTALS Week 13 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on a lab by James Barker Pre-Laboratory Checklist vvskills: You can handle any recursive, or higher order function. vvknowledge: You understood basic computational complexities and can design programs accordingly. vvyou have read the laboratory text below. Introduction The notion of recursion has been fundamental to the course so far. You have learnt to use recursive functions and recursive data types to solve problems. In this lab, you will use this knowledge to explore and create images of fractals, geometrical objects that are deeply recursive in nature and are therefore amenable to work with in Haskell. Important Note This week s lab is intended as an enjoyable exercise, and is not directly examinable. As this may be one of your final chances to speak to your tutor face-to-face, you should also ensure that you raise with them any issues or problems you have encountered in any of the previous labs. Interlude: The Koch Snowflake Many of you will have seen pictures of fractals before. They are a standard trope of computergenerated art and imagery beautiful, complex, and mysterious, and yet they can be created through the repeated application of very simple rules. (This kind of behaviour is often referred to as emergence the Wireworld assignment is another example of emergent complexity derived from simple rules.) But what exactly is a fractal? A proper mathematical description is difficult, and well beyond the scope of this course (those of you who go on to study advanced mathematical analysis topics like measure theory will find fractal ideas popping up). For our purposes, a fractal is a geometrical object that exhibits self-similarity at different scales. If you look closely at a fractal, you will see the same or similar kind of structure and shape as you would see looking at it from a distance. In this lab, we will use some simple sets of rules to progressively transform simple geometric objects (lines, triangles, etc.) into self-similar images of fractals. (Note that this is certainly not 1 ANU College of Engineering and Computer Science May 2015

the only way to produce fractal images!) The first example we will construct is the so-called Koch Snowflake. To see how this works, we begin with a straight line, like so: Although trivial, this will be the starting point that we will use repeatedly to build up a more interesting image by applying a transformation rule. This rule can be stated as: Dilate (i.e. shrink) the picture by a factor of 1/3. Make three more copies of the dilated picture (so there are a total of four copies of the same image). Translate and rotate these copies according to the following diagram: (0,0) (1/3, 0) (1/2, ( 3) / 6) (0,0) 60 (1/3, 0) (2/3, 0) (1, 0) After this rule has been applied once, we have obtained a degree-1 fractal. (The original line we started with is, therefore, a degree-0 fractal.) To obtain a degree-2 fractal, we recursively apply the same transformation rule to the degree-1 fractal. Higher-degree fractals can be generated by this recurrence relationship, with the degree-0 fractal as a base case. I have provided code to produce and display the Koch snowflake at varying degrees of complexity. Obtain the Snowflake.hs file from the course website. Compile and run this code, and have a look through it if you like. Do not attempt to run it through GHCi, as the code s performance will be poor. (You will need to ensure you have an up-todate version of the Gloss graphics library installed on your machine. If you are working in Windows, you will need to place the glut32. dll file from Assignment 1 in your working directory. If you experience problems, talk to your tutor.) 2 ANU College of Engineering and Computer Science May 2015

Experiment with different degrees of complexity. What do you notice about the time it takes to calculate higher-degree fractals? How might this algorithm scale, informally or in Big-O notation? (The actual Koch Snowflake of degree n is created by first producing three fractals of degree n starting from straight lines, as above. These are then glued together in a triangle shape. I didn t mention this above to avoid confusion, but you can see it immediately from the degree-0 case in the code provided.) It is interesting to note that technically, the images you are producing are only approximations to the Koch Snowflake fractal. The actual (infinitely complex) fractal is only obtained when you take, in some sense, the limit of the shape as the degree approaches infinity. If you are interested in a more detailed derivation of what a fractal really is and how it can be manipulated and reasoned about, you might like to investigate the later-year mathematics course Math3062: Fractal Geometry and Chaos Dynamics 1. Interlude: Gloss Data Types A Quick Primer This lab revolves heavily around the use of Gloss, a Haskell library for producing and displaying simple images and animations 2. You have already seen Gloss used to display the Wireworld simulation in the first assignment, but this is the first time that you have actually had to work with it programmatically. Although simple by real-world standards, Gloss is still substantially larger than any library you have yet had to work with. It is not necessary for you to understand most of what offers in order to complete this lab; however, it is necessary to discuss one particular recursive data type the Picture. Here is the standard documentation for the Picture type: http://hackage.haskell.org/package/gloss-1.7.8.4/docs/graphics-gloss-data-picture.html The first few constructor entries will make obvious sense polygons, lines, circles, etc. To specify a circle with radius 1, for instance, you would write the Haskell literal expression Circle 1.0. (You might like to load Graphics.Gloss.Data.Picture up in GHCi, and investigate the types of some simple Picture expressions.) The next three (Text, Bitmap, and Color) can be ignored by you for now. The final four are recursive, and more interesting. They provide a way for you to manipulate and combine Pictures into other Pictures. The Translate, Rotate, and Scale constructors represent exactly what they say on the tin, and the Pictures constructor wraps a list of other Picture elements. So the following is a completely legitimate expression of type Picture: Pictures [Translate 10.0 10.0 (Circle 1.6), Scale 2.0 3.0 (Translate (-10.0) 10.0 (ThickCircle 2.0 9.0))] You should deconstruct this expression and work out what it specifies. Also, those of you with some experience in geometry and/or linear algebra will recognise that the order of operations is important something translated and then rotated will probably not be the same if the rotation is performed before the translation. Why do we need this data type? To eventually display an image on the screen, it must be passed into a particular function as a single element of type Picture. 1 This is a highly-recommended general-interest course, and not one that requires a great deal of mathematical knowledge to enjoy although HPO students will certainly have the opportunity to flex their muscles. The lecturer, Michael Barnsley, was one of the leading figures in the rise of fractal geometry during the 70's and 80's; he, along with John Hutchinson (another maths lecturer/researcher at the ANU) were responsible for the theoretical development and practical application of various fractal-based techniques, including a fascinating method of image/data compression that offered insane compression ratios, at the cost of highly-expensive encoding algorithms. This system fell by the wayside (and provides an interesting example of early problems with software patents), but remains an interesting component of the history of information compression. 2 Gloss was originally developed at the ANU by Ben Lippmeier, a former researcher here. He still actively maintains and improves the software. 3 ANU College of Engineering and Computer Science May 2015

Exercise 1: The Sierpinski Sieve Your first task is to produce code to display another family of fractal approximations. The fractal being approximated is the Sierpinski Sieve one of the most common and elegant fractals around, and quite possibly one that you have seen before. The degree-0 case for the Sierpinski Sieve is a single, filled triangle, traditionally oriented to point upwards. The transformation rule is simple: Dilate the ^n -1h degree fractal by a factor of 1 2. Create two additional dilated copies of the ^n -1h degree fractal. Translate these copies so that their left-hand points touch the upper- and right-hand point of the first copy, respectively. Visually, this can be displayed as shown: (1/2, ( 3) / 2) (0,0) (1,0) To begin, take a copy of my Snowflake.hs code above, and give it a sensible name (i.e. Sieve.hs). With the exception of the function named main, which you probably won t be able to make heads or tails of, you should be able to read and understand all of my code. (As explained previously, IO in Haskell is complex and we will not be discussing it here.) Create new code, similar to mine, to produce images of the Sierpinski Sieve at varying degrees of complexity. Check with your tutor that your solution is correct. Exercise 2: The Heighway Dragon Having implemented an easy fractal in the form of the Sierpinski Sieve, we now turn to a more complicated example: the Heighway Dragon. (Those of you who ve read Jurassic Park will recognise this fractal.) 4 ANU College of Engineering and Computer Science May 2015

The degree-n Heighway Dragon is, again, a simple straight line. The transition rule is as follows: Dilate the (n-1) degree fractal by a factor of 1 4. Create three additional copies of the (n-1) degree fractal. Rotate the (four!) copies of the (n-1) degree fractal by: 90c counter-clockwise, 180c clockwise, 90c clockwise, and 180c clockwise. Rearrange the four copies as shown in the diagram below. The distance to translate the sub-fractals of degree ^n -1h during production of a fractal of degree n is in units that are dilated by 1 2 each time. The base case for this (i.e. the degree 1 fractal) requires translation in units of the original line length divided by 4. This isn t as complicated as it sounds. Note that the orientation and order of the copies is important. This diagram shows the creation of fractals up to order 3. The arrow-heads are included to make the process clear. Create a new script file, Dragon.hs, that can draw images of the Heighway Dragon. Again, this code will be very similar to that which you ve worked on before. Check your answer with your tutor there are several subtle ways to get this wrong, so be careful. Submit your code to the SubmissionApp under Lab 13 Dragon. 5 ANU College of Engineering and Computer Science May 2015

Interlude: The Mandelbrot Set and Escape-Time Algorithms Most of you will probably have seen an image of the famous Mandelbrot Set, a fractal that was discovered by (and named after) Benoit Mandelbrot. Mandelbrot was arguably the most important pioneer of fractal geometry, and his recent death was a great loss to mathematics. However, whilst almost everybody recognises the Mandelbrot Set, few people actually understand how it s derived. I have written some simple Haskell code to allow you to visualise and explore the Mandelbrot set. Obtain the script files Diver.hs and Mandelbrot.hs from the course website. Compile and run them with the following command: ghc -O3 Diver.hs Execute the compiled binary. What you are looking at is a visualisation of a region of the complex plane, bounded by! 20. and! 2i (as you are informed by the text in the corner). So where do the pretty patterns come from? Informally, a complex point c is within the Mandelbrot set if (and only if) the sequence obtained by repeated iteration of the recurrence relationship zn = z 2 n 1 + c z0 = 0 -, is bounded (i.e. can always be contained within an origin-centred circle of finite radius). Of course, we are talking here about complex multiplication and addition. For example, picking c = 05. + 025. i provides the following infinite sequence of terms, called an orbit: z0 = 0 z1 = 05. + 025. i z2 = 06875. + 0. 5i z3 = 072265625. + 0. 9375i z 4 = f If you continue repeating this process, you will find that the points z n in the orbit diverge quite quickly to infinity, in the sense that the distance between them and the origin grows rapidly larger. In fact, it can be proved that if this orbit ever leaves the circle in the complex plane with radius 2 and centred on the origin, then it cannot be bounded and is therefore not in the Mandelbrot set. On the other hand, it is rather easy to see that c = 0+ 0i must be in the Mandelbrot set, because the sequence of terms obtained by infinitely iterating z n is 0, 0, 0,... and therefore bounded. Less trivially, setting c = i (the imaginary unit) provides the orbit 0, i, ^- 1+ ih, - i, ^- 1+ ih, - i,..., which cycles and can be bounded by an origin-centred circle of radius 2. So, returning to the actual image, each pixel corresponds to a sampled point on that plane that is used as a value for c in the above recurrence relationship. Pixels are usually coloured black if they are within the Mandelbrot set. As you can see, the interesting fractal stuff happens on the boundary of the black area. If you left-click your mouse anywhere on the screen, the image will redraw and zoom in on the point you clicked. (Be patient, it takes a few seconds to do each redraw.) Right-click to zoom out again. Try diving down on the boundary, and observe that even as the observation window onto the complex plane gets smaller and smaller, the complexity of the boundary only increases. 6 ANU College of Engineering and Computer Science May 2015

But what do those colours actually mean? Where do they come from? The standard way to colour a Mandelbrot visualisation is by something called an escape-time algorithm. Each pixel is coloured according to how quickly the complex value associated with that pixel diverges to infinity (and black if it doesn t, i.e. if it s within the Mandelbrot set). Pixels far away from the boundary of the set diverge very quickly, and are coloured lightly. Pixels very close to the boundary often tend to oscillate around that boundary for a while before packing their bags and heading to infinity, so they are coloured darker3. If you re paying attention, you might be wondering: we ve worked out how to identify points that are not in the Mandelbrot set, but can we always identify those that are? There is no obvious guarantee that their orbits will ever cycle, as in the case of c = i above. If we kept testing recurrence sequence terms for a point in the Mandelbrot set, we would never observe them heading to infinity but we would never run out of points in those sequences, and could never be quite sure that the next one wouldn t finally high-tail it towards infinity, so the computation time might take quite a while(!). We get around this by establishing a maximum iteration count, and conclude that if an orbit hasn t left the immediate neighbourhood of the Mandelbrot set by that many iterations, we can just assume that it s in the Mandelbrot set. Feel free to spend some time playing with my code. You might like to inspect it and try and work out how it works most of it should make reasonable sense to you. (Note that the infix :+ operator assembles a complex number z out of two real numbers x and y.) As is often the case with this kind of program, the amount of code required to actually do the Mandelbrot set calculations is minimal, only a few lines; most of the code goes towards defining and maintaining the sample region displayed in the window. Exercise 3: Julia Sets A final exercise for the very keen, and indeed, an open extension. Spend some time investigating my Mandelbrot set code. There are other fractals that can be visualised using an escapetime algorithm. One collection of such fractals are the so-called Julia sets. A Julia set can be obtained by reversing the logic of the Mandelbrot code. The same recurrence is used; however, escape times are not calculated by choosing the constant c according to the pixel being investigated and starting the recurrence from z0 = 0. Rather, the constant c is fixed to some arbitrary value before-hand (to be the same for each pixel), and each pixel is coloured according to the escape-time of the recurrence starting with z 0 as the pixel s value. There are 3 There is a stunningly beautiful alternative rendering of the Mandelbrot set that is known as the Buddhabrot Google it. Rather than colour pixels according to the speed with which they diverge to infinity, pixels are coloured according to how many times they are landed upon by the various orbits of points around the Mandelbrot set before they leave. This provides an intricate glimpse of the dynamical behaviour of points near the set. Although I don t have time to develop it further here, I would say that the Buddhabrot is my single favourite fractal rendering technique. 7 ANU College of Engineering and Computer Science May 2015

infinitely many choices for c, and each will produce a different result. Some values of c producing boring, ugly, not-really-fractal images. Some values of c produce stunning pictures that render increasing complexity on zooming. The task of predicting a priori which you ll get is far, far beyond the scope of this course. (A phrase you re probably sick of hearing by now.) Your task is to modify my code and to explore the parameter space of c. I don t anticipate that you will need to modify my rendering code, i.e Diver.hs you should be able to create a new module Julia.hs that exports a function escape_time (with the same type signature as in my Mandelbrot module), and simply replace the relevant import statement in Diver.hs. If you come across any values of c that produce particularly attractive renderings, please feel free to email me about them and discuss! Submit your code to the SubmissionApp under Lab 13 Julia Sets. Changes are that the results are so beautiful that I have to show them in class (if you agree to it of course). Make Sure You Logout to Terminate Your Session! 8 ANU College of Engineering and Computer Science May 2015