src/looms/005-simple-triangulations.lisp @ d5b02d8c4803

Add stippling, tests
author Steve Losh <steve@stevelosh.com>
date Fri, 23 Mar 2018 23:14:07 -0400
parents b098ec32e059
children 595e16056317
(in-package :flax.looms.005-simple-triangulations)

;; https://mattdesl.svbtle.com/pen-plotter-1

(defparameter *point-size* 0.003)

(defun convert-point (point)
  (flax.drawing:circle point (random-gaussian *point-size*
                                              (* 0.15 *point-size*)
                                              #'rand)))

(defun convert-triangle (ratio tri)
  (when (randomp ratio #'rand)
    (destructuring-bind (a b c) tri
      (list (flax.drawing:triangle a b c)))))

(defun convert (points ratio)
  (append
    (map 'list #'convert-point points)
    (mapcan (curry #'convert-triangle ratio) (triangulate points))))

(defun triangulate (points)
  (mapcar (lambda (indexes)
            (map 'list (curry #'aref points) indexes))
          (lofi.tri:triangulate (map 'vector #'coord-to-cons points))))

(defun gauss ()
  (clamp 0.0 1.0 (random-gaussian 0.5 0.15 #'rand)))

(defun generate-point-uniform ()
  (coord (rand 1.0) (rand 1.0)))

(defun generate-point-gaussian ()
  (coord (gauss) (gauss)))

(defun generate-point-gaussian-vertical ()
  (coord (rand 1.0) (gauss)))

(defun generate-point-gaussian-horizontal ()
  (coord (gauss) (rand 1.0)))

(defun generate (generator n)
  (iterate (repeat n)
           (collect (funcall generator) 
                    :result-type 'vector)))

(defun select-generator ()
  (random-elt '((generate-point-uniform "Uniform")
                (generate-point-gaussian "Gaussian")
                (generate-point-gaussian-vertical "Vertical Gaussian")
                (generate-point-gaussian-horizontal "Horizontal Gaussian"))
              #'rand))

(defun loom (seed filename filetype width height &key ratio points)
  (nest
    (with-seed seed)
    (flax.drawing:with-rendering (canvas filetype filename width height
                                         :background (hsv 0.09 0.05 0.975)))
    (destructuring-bind (generator generator-name) (select-generator))
    (randomly-initialize
      ((ratio (if (randomp 0.5 #'rand)
                1
                (random-range 0.05 0.2 #'rand)))
       (points (round-to (random-range-inclusive 200 700 #'rand)
                         10))))
    (progn
      (-<> (generate generator points)
        (convert <> ratio)
        (flax.drawing:render canvas <>))
      (values generator-name points ratio))))


;; (time (loom 55 "out" :svg 800 800 ))