Barnsley fern: Difference between revisions

Add Common Lisp implementation
(→‎{{header|Go}}: adding sample in gnuplot)
(Add Common Lisp implementation)
Line 207:
}
}</lang>
 
=={{header|Common Lisp}}==
This code uses the <code>opticl</code> package for generating an image and saving it as a PNG file.
<lang lisp>(defpackage #:barnsley-fern
(:use #:cl
#:opticl))
 
(in-package #:barnsley-fern)
 
(defparameter *width* 800)
(defparameter *height* 800)
(defparameter *factor* (/ *height* 13))
(defparameter *y-offset* (/ *height* 10))
 
(defun f1 (x y)
(declare (ignore x))
(values 0 (* 0.16 y)))
 
(defun f2 (x y)
(values (+ (* 0.85 x) (* 0.04 y))
(+ (* -0.04 x) (* 0.85 y) 1.6)))
 
(defun f3 (x y)
(values (+ (* 0.2 x) (* -0.26 y))
(+ (* 0.23 x) (* 0.22 y) 1.6)))
 
(defun f4 (x y)
(values (+ (* -0.15 x) (* 0.28 y))
(+ (* 0.26 x) (* 0.24 y) 0.44)))
 
(defun choose-transform ()
(let ((r (random 1.0)))
(cond ((< r 0.01) #'f1)
((< r 0.86) #'f2)
((< r 0.93) #'f3)
(t #'f4))))
 
(defun set-pixel (image x y)
(let ((%x (truncate (+ (* *factor* x) (floor *width* 2))))
(%y (truncate (- *height* (* *factor* y) *y-offset*))))
(setf (pixel image %y %x) (values 0 255 0))))
 
(defun fern (filespec &optional (iterations 10000000))
(loop with image = (make-8-bit-rgb-image *height* *width* :initial-element 0)
with x = 0
with y = 0
do (set-pixel image x y)
repeat iterations
do (multiple-value-setq (x y) (funcall (choose-transform) x y))
finally (write-png-file filespec image)))</lang>
 
=={{header|Delphi}}==
68

edits