Skip to content

IFS Common Lisp implementation #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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-14, lang:"lisp"](code/clisp/ifs.lisp)
{% sample lang="coco" %}
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
{% sample lang="java" %}
Expand Down Expand Up @@ -224,6 +226,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)
{%sample lang="coco" %}
[import, lang:"coconut"](code/coconut/IFS.coco)
{%sample lang="java" %}
Expand Down
28 changes: 28 additions & 0 deletions contents/IFS/code/clisp/ifs.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
;;;; Iterated Function System implementation

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

(defun chaos-game (iterations shape-points)
"Plays a chaos game with a certain shape for a determined amount of iterations"
(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)
;; this is not very clean, but it's the simplest way to insert a tab into a string.
(format nil "~f~c~f" (point-x p) #\tab (point-y p))))
(format out "~{~a~%~}" (map 'list #'format-point (chaos-game 10000 *shape-points*)))))