Skip to content

Commit f04abdc

Browse files
Trashtalk217leiosberquist
authored
IFS Common Lisp implementation (#722)
* first working version * cleaned up the chaos-game function and added some helpfull comments * changed the .md file and upped iterations * Skeleton of the final solution * Update contents/IFS/IFS.md Co-authored-by: Eric Berquist <[email protected]> Co-authored-by: James Schloss <[email protected]> Co-authored-by: Eric Berquist <[email protected]>
1 parent b86d137 commit f04abdc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

contents/IFS/IFS.md

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Here, instead of tracking children of children, we track a single individual tha
140140
[import:5-12, lang:"python"](code/python/IFS.py)
141141
{% sample lang="c" %}
142142
[import:18-29, lang:"c"](code/c/IFS.c)
143+
{% sample lang="lisp" %}
144+
[import:5-14, lang:"lisp"](code/clisp/ifs.lisp)
143145
{% sample lang="coco" %}
144146
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
145147
{% sample lang="java" %}
@@ -224,6 +226,8 @@ In addition, we have written the chaos game code to take in a set of points so t
224226
[import, lang:"python"](code/python/IFS.py)
225227
{% sample lang="c" %}
226228
[import, lang:"c"](code/c/IFS.c)
229+
{% sample lang="lisp" %}
230+
[import, lang:"lisp"](code/clisp/ifs.lisp)
227231
{%sample lang="coco" %}
228232
[import, lang:"coconut"](code/coconut/IFS.coco)
229233
{%sample lang="java" %}

contents/IFS/code/clisp/ifs.lisp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
;;;; Iterated Function System implementation
2+
3+
(defstruct (point (:constructor make-point (x y))) x y)
4+
5+
(defun chaos-game (iterations shape-points)
6+
"Plays a chaos game with a certain shape for a determined amount of iterations"
7+
(loop
8+
repeat iterations
9+
for rand-point = (svref shape-points (random (length shape-points)))
10+
for point = (make-point (random 1.0) (random 1.0)) ; starting point
11+
then (make-point
12+
(* 0.5 (+ (point-x rand-point) (point-x point)))
13+
(* 0.5 (+ (point-y rand-point) (point-y point)))) ; every subsequent point
14+
collect point))
15+
16+
(defparameter *shape-points*
17+
(map
18+
'vector
19+
(lambda (e) (apply #'make-point e))
20+
;; the backquote allows us to selectively evaluate (sqrt 0.75) with the comma
21+
`((0 0) (0.5 ,(sqrt 0.75)) (1 0))))
22+
23+
;; output the data to the "out.dat" file
24+
(with-open-file (out "out.dat" :direction :output :if-exists :supersede)
25+
(flet ((format-point (p)
26+
;; this is not very clean, but it's the simplest way to insert a tab into a string.
27+
(format nil "~f~c~f" (point-x p) #\tab (point-y p))))
28+
(format out "~{~a~%~}" (map 'list #'format-point (chaos-game 10000 *shape-points*)))))

0 commit comments

Comments
 (0)