## Archive for the ‘**fractal**’ Tag

If you’ve been watching my pyafg github repo of late, you will have noticed that I’ve been very busy hacking on pyafg in the last few weeks, and have managed to add a heap of cool new features.

**What’s New**

- Added a graphical transform editor
- Added a gradient coloring system
- Added more features!
- Added some more fractals
- Fixed many bugs
- Made the pygame interface cleaner
- You can now save images in the pygame interface
- Added gradient demo

The transform editor was written using tkinter, and so for that reason it is neither pretty nor quick (for some reason tkinter does not support drawing directly to a canvas, you have to make objects on the canvas), but it does the job well, and I’ve managed to create quite a few neat looking fractals with it. In the future I hope to make a gtk or qt interface, but until then, it should do the job.

pyafg's tkinter transform editor

I’ll need to tidy up and standardize the command line arguments to pyafg, because I’ve added a lot more options. Here is an example of gradient coloring.

Here is a shot of the gradient for the above image.

-34.235505
142.170783

Being bored this morning, I decided to implement the ifs algorithm on my TI-89 graphics calculator. It was not particularly difficult, and after 23 lines of TI-BASIC, I had this image on my screen.

Yes, rather low res, I know...

The program takes a n*7 matrix, like this

Where a, b, c, and d are the elements of a 2*2 transformation matrix, e and f are elements of an offset vector, and p is the probability of the rule occuring. Note that it runs extremely slowly, I waited about ten minutes for the picture shown to be generated. I’ve added it to the repo though; in case anyone else is also bored ;)

`Prgm`

rand()->a

rand()->b

0->i

1000000->iters

100->s

While ic

rand()->l

While pc

r[c,7]+p->p

EndWhile

r[c,1]*a+r[c,2]*b+r[c,5]->t

r[c,3]*a+r[c,4]*b+r[c,6]->b

t->a

If i>s

PtOn a,b

1+i->i

EndWhile

EndPrgm

-34.235505
142.170783

This section is a powerful demonstration of abstraction, using a “picture language”. For this section I used the sicp package from the PLaneT repository in DrScheme. Normally, I just use mzscheme and vim, but apparently you need DrScheme open to use that particular (graphical) package. Anyway, I found it pretty enjoyable, and even made some fractals with it.

**;;Exercise 2.44**

They could have made this one a little tougher ;)

Telling us to flip the arguments to right-split took any challenge out of it…

```
(define (up-split painter n)
(if (= n 0)
painter
(let ((smaller (up-split painter (- n 1))))
(below painter (beside smaller smaller)))))
```

**;;Exercise 2.45**

We follow the usual patter of making abstractions.

```
(define (split op1 op2)
(define (rec painter n)
(if (= n 0)
painter
(let ((smaller (rec painter (- n 1))))
(op1 painter (op2 smaller smaller)))))
rec)
(define up-split (split below beside))
(define right-split (split beside below))
```

**;;Exercise 2.46**

Not much to it, I used a list, but a cons would do fine too, however, I decided on list, because perhaps we’ll want to add a z coordinate for 3D vectors.

```
(define (make-myvect x y)
(list x y))
(define (xcor-myvect vect)
(car vect))
(define (ycor-myvect vect)
(cadr vect))
(define (add-myvect a b)
(make-myvect (+ (xcor-myvect a) (xcor-myvect b))
(+ (ycor-myvect a) (ycor-myvect b))))
(define (sub-myvect a b)
(make-myvect (- (xcor-myvect a) (xcor-myvect b))
(- (ycor-myvect a) (ycor-myvect b))))
(define (scale-myvect s v)
(make-myvect (* s (xcor-myvect v))
(* s (ycor-myvect v))))
```

**;;Exercise 2.47**

First definition

```
(define (make-myframe origin edge1 edge2)
(list origin edge1 edge2))
(define (origin-myframe f) (car f))
(define (edge1-myframe f) (cadr f))
(define (edge2-myframe f) (caddr f))
```

Second definition

```
(define (make-myframe origin edge1 edge2)
(cons origin (cons edge1 edge2)))
(define (origin-myframe f) (car f))
(define (edge1-myframe f) (cadr f))
(define (edge2-myframe f) (cddr f))
```

**;;Exercise 2.48**

```
(define (make-mysegment start end) (cons start end))
(define (start-mysegment seg) (car seg))
(define (end-mysegment seg) (cdr seg))
```

**;;Exercise 2.49**

```
;;a. The painter that draws the outline of the designated frame.
(define outline (segments->painter (list
(make-segment (make-vect 0 0)
(make-vect 0 1))
(make-segment (make-vect 0 0)
(make-vect 1 0))
(make-segment (make-vect .99 0)
(make-vect .99 1))
(make-segment (make-vect 0 .99)
(make-vect 1 .99)))))
;;b. The painter that draws an ``X'' by connecting opposite corners of the frame.
(define X (segments->painter (list
(make-segment (make-vect 0 0)
(make-vect 1 1))
(make-segment (make-vect 1 0)
(make-vect 0 1)))))
;;c. The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.
(define diamond (segments->painter (list
(make-segment (make-vect 0 .5)
(make-vect .5 0))
(make-segment (make-vect .5 0)
(make-vect 1 .5))
(make-segment (make-vect 1 .5)
(make-vect .5 1))
(make-segment (make-vect .5 1)
(make-vect 0 .5)))))
;;d. The wave painter.
(define wave (segments->painter (list
(make-segment (make-vect 0 .84) (make-vect .13 .6))
(make-segment (make-vect .13 .6) (make-vect .3 .66))
(make-segment (make-vect .3 .66) (make-vect .4 .66))
(make-segment (make-vect .4 .66) (make-vect .36 .84))
(make-segment (make-vect .36 .84) (make-vect .4 1))
(make-segment (make-vect .6 1) (make-vect .64 .84))
(make-segment (make-vect .64 .84) (make-vect .6 .66))
(make-segment (make-vect .6 .66) (make-vect .71 .66))
(make-segment (make-vect .71 .66) (make-vect 1 .34))
(make-segment (make-vect 1 .16) (make-vect .6 .44))
(make-segment (make-vect .6 .44) (make-vect .74 0))
(make-segment (make-vect .6 0) (make-vect .5 .3))
(make-segment (make-vect .5 .3) (make-vect .4 0))
(make-segment (make-vect .24 0) (make-vect .34 .5))
(make-segment (make-vect .34 .5) (make-vect .3 .6))
(make-segment (make-vect .3 .6) (make-vect .14 .4))
(make-segment (make-vect .14 .4) (make-vect 0 .64)))))
```

**;;Exercise 2.50**

The key to figuring these out is to draw a diagram of the unit square, draw the original picture, and then draw the rotated one, and note the coordinates of the origin, edge1, and edge2.

```
(define (myflip-horiz painter)
((transform-painter (make-vect 1 0)
(make-vect 0 0)
(make-vect 1 1)) painter))
(define (rotate-90 painter)
((transform-painter (make-vect 1 0)
(make-vect 1 1)
(make-vect 0 0)) painter))
(define (rotate-180 painter)
((transform-painter (make-vect 1 1)
(make-vect 0 1)
(make-vect 1 0)) painter))
(define (rotate-270 painter)
((transform-painter (make-vect 0 1)
(make-vect 0 0)
(make-vect 1 1)) painter))
```

**;;Exercise 2.51**

```
(define (my-below painter1 painter2)
(let ((split-point (make-vect 0 .5)))
(let ((paint-top
((transform-painter split-point
(make-vect 1 .5)
(make-vect 0 1)) painter1))
(paint-bottom
((transform-painter (make-vect 0 0)
(make-vect 1 0)
split-point) painter2)))
(lambda (frame)
(paint-top frame)
(paint-bottom frame)))))
(define (my-below2 painter1 painter2)
(rotate-90 (beside (rotate-270 painter1) (rotate-270 painter2))))
```

Okay, this has been a pretty boring article… lack of pictures, &c. However, I do have a couple pictures of some nice fractals that I generated with some functions I wrote in this picture language.

The sierpinski painter generates a sierpinski triangle with n iterations using an MRCM (Multiple Reduction Copy Machine) process. This is based on the idea from the book, Computational Beauty of Nature.

```
(define (sierpinski painter n)
(if (= n 0)
painter
(let ((top ((transform-painter (make-vect .25 0)
(make-vect .75 0)
(make-vect .25 1))
painter)))
(sierpinski (below (beside painter painter) top) (- n 1)))))
```

And here it is generating it with the `einstein`

painter.

There’s also another neat fractal, which is defined as

```
(define (crystal painter n)
(if (= n 0)
painter
(let ((top ((transform-painter (make-vect .25 0)
(make-vect .75 0)
(make-vect .25 1))
painter)))
(crystal (below (beside (rotate-90 painter)
(rotate-270 painter))
top)
(- n 1)))))
```

And looks like

As you can see, it’s pretty easy to define MRCM fractals using this picture language.

-34.235505
142.170783

I was at school on thursday, and had IT Software Development in the afternoon. Surprisingly, it turned out to be quite enjoyable. I tested out pyafg on the school computers running windows xp, and it worked quite nicely. I can now say that pyafg is cross platform! (w00t)

Since we were being “taught” how to use tkinter, I decided to hack up a little gui for pyafg using it. It’s a rather simple affair, but is both a testament to the power of tkinter, and the success of my abstraction in libifs. I plan to turn run_IFS into a generator, and generalize it even further. While I was working on the (simplistic) gui, I had the idea to make a transform editor for pyafg. I think it should be pretty easy, and for those who’ve seen transform_viewer.py in the git repo, you’ll know that I already have the means to do so :)

I’ve also updated the pyafg page into something looking a little more like a project page, and plan to add a TODO list to that. Transform Editor will be first on the list :)

-34.235505
142.170783

I’ve started work on a new project, pyafg (pyafg is an affine fractal generator). It’s a little python program that generates affine fractals using the iterated function system algorithm. I was inspired to start it whilst reading the computational beauty of nature; I’ll write a post up about my experiences hacking it later. For now, download it, play around with it, and enjoy!

**(Note, I did only hack this up in 2 days. It’s nowhere near complete, but I’ve made a very good start to it. Expect more features!)**

A Fern I generated

-34.235505
142.170783