Skip to content
Merged
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Here, instead of tracking children of children, we track a single individual tha
[import:5-12, lang:"python"](code/python/IFS.py)
{% sample lang="c" %}
[import:18-29, lang:"c"](code/c/IFS.c)
{% sample lang="lisp" %}
[import:5-13, lang:"lisp"](code/clisp/ifs.lisp)
{% endmethod %}

If we set the initial points to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below:
Expand Down Expand Up @@ -203,6 +205,8 @@ In addition, we have written the chaos game code to take in a set of points so t
[import, lang:"python"](code/python/IFS.py)
{% sample lang="c" %}
[import, lang:"c"](code/c/IFS.c)
{% sample lang="lisp" %}
[import, lang:"lisp"](code/clisp/ifs.lisp)
{% endmethod %}

### Bibliography
Expand Down
26 changes: 26 additions & 0 deletions contents/IFS/code/clisp/ifs.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
;;;; Iterated Function System implementation

(defstruct (point (:constructor make-point (x y))) x y)

(defun chaos-game (iterations shape-points)
(loop
repeat iterations
for rand-point = (svref shape-points (random (length shape-points)))
for point = (make-point (random 1.0) (random 1.0)) ; starting point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this creates a new point on every iteration of the loop, which none of the other implementations do. Can you change it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the (make-point (random 1.0) (random 1.0)), because that's only run once. I don't exactly understand what you mean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, there must be something here that I don't understand, because the code clearly works. I'm fine with it then.

then (make-point
(* 0.5 (+ (point-x rand-point) (point-x point)))
(* 0.5 (+ (point-y rand-point) (point-y point)))) ; every subsequent point
collect point))

(defparameter *shape-points*
(map
'vector
(lambda (e) (apply #'make-point e))
;; the backquote allows us to selectively evaluate (sqrt 0.75) with the comma
`((0 0) (0.5 ,(sqrt 0.75)) (1 0))))

;; output the data to the "out.dat" file
(with-open-file (out "out.dat" :direction :output :if-exists :supersede)
(flet ((format-point (p)
(format nil "~f~c~f" (point-x p) #\tab (point-y p)))) ; this is a bit ugly, but it works
(format out "~{~a~%~}" (map 'list #'format-point (chaos-game 10000 *shape-points*)))))