Last active
March 12, 2017 09:39
-
-
Save tormaroe/a7dc43f32a4b0214a34dff3262ebce0a to your computer and use it in GitHub Desktop.
Maze experiments with common lisp and processing.js, part I (garbled)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(dolist (x '(:hunchentoot :cl-who :parenscript :cl-fad)) | |
(ql:quickload x)) | |
(defpackage "EXPERIMENTS" | |
(:use "COMMON-LISP" "HUNCHENTOOT" "CL-WHO" "PARENSCRIPT" "CL-FAD")) | |
(in-package "EXPERIMENTS") | |
(defparameter *app* nil) | |
(setf *app* (start (make-instance 'easy-acceptor :port 8081))) | |
(define-easy-handler (page1 :uri "/1") () | |
(with-html-output-to-string (s) | |
(:html | |
(:head (:title "Page 1") | |
; (:script :type "text/javascript" :src "https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.min.js") | |
(:script :type "text/javascript" :src "http://blog.kjempekjekt.com/assets/js/processing-1.4.1.min.js") | |
(:script :type "text/javascript" :src "/script1.js")) | |
(:body (:h1 "Algoritmic art") | |
(:h2 "Experiment 1") | |
(:canvas :id "canvas1"))))) | |
(define-easy-handler (script1-js :uri "/script1.js") () | |
(setf (content-type*) "text/javascript") | |
(ps | |
(defmacro calls (&rest xs) | |
(cons 'progn (mapcar (lambda (x) `(chain processing ,x)) xs))) | |
(defun sketch-proc (processing) | |
(setf (@ processing setup) | |
(lambda () | |
(labels ((line-with-passage (x1 y1 x2 y2) | |
;; TODO: randomly add passage, draw lines | |
(let* ((passage-witdh (/ 380.0 64.0)) | |
(possible-passages (/ (+ (- x2 x1) (- y2 y1)) passage-witdh)) | |
(passage-index (random possible-passages))) | |
(if (= x1 x2) | |
(calls | |
(line x1 y1 x2 (+ y1 (* passage-witdh passage-index))) | |
(line x1 (+ y1 (* passage-witdh (+ passage-index 1))) x2 y2)) | |
(calls | |
(line x1 y1 (+ x1 (* passage-witdh passage-index)) y2) | |
(line (+ x1 (* passage-witdh (+ passage-index 1))) y1 x2 y2))))) | |
(devide (x y w h depth) | |
(when (> depth 0) | |
(let* ((w2 (/ w 2)) | |
(h2 (/ h 2)) | |
(mid-x (+ w2 x)) | |
(mid-y (+ h2 y)) | |
(depth2 (- depth 1))) | |
(line-with-passage x mid-y (+ x w) mid-y) | |
(line-with-passage mid-x y mid-x (+ y h)) | |
;; Make recursive calls for each of the four new quadrants | |
(devide x y w2 h2 depth2) | |
(devide mid-x y w2 h2 depth2) | |
(devide x mid-y w2 h2 depth2) | |
(devide mid-x mid-y w2 h2 depth2))))) | |
(calls | |
(size 400 400) | |
(background 255) | |
(fill 245) | |
(stroke 220 10 10) | |
(stroke-weight 1) | |
(rect 10 10 380 380)) | |
(devide 10 10 380 380 6)))) | |
(setf (@ processing draw) | |
(lambda ()))) | |
;; Create Processing object | |
(defun init-sketch () | |
(let ((canvas (chain document (get-element-by-id 'canvas1)))) | |
(new (-Processing canvas sketch-proc)))) | |
;; Call init-sketch once the DOM has loaded | |
(chain document (add-event-listener "DOMContentLoaded" | |
(lambda () (init-sketch)) | |
FALSE)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sketchProc(processing) { | |
processing.setup = function () { | |
var lineWithPassage = function (x1, y1, x2, y2) { | |
var passageWitdh = 380.0 / 64.0; | |
var possiblePassages = ((x2 - x1) + (y2 - y1)) / passageWitdh; | |
var passageIndex = Math.floor(possiblePassages * Math.random()); | |
if (x1 === x2) { | |
processing.line(x1, y1, x2, y1 + passageWitdh * passageIndex); | |
return processing.line(x1, y1 + passageWitdh * (passageIndex + 1), x2, y2); | |
} else { | |
processing.line(x1, y1, x1 + passageWitdh * passageIndex, y2); | |
return processing.line(x1 + passageWitdh * (passageIndex + 1), y1, x2, y2); | |
}; | |
}; | |
var devide = function (x, y, w, h, depth) { | |
if (depth > 0) { | |
var w2 = w / 2; | |
var h2 = h / 2; | |
var midX = w2 + x; | |
var midY = h2 + y; | |
var depth2 = depth - 1; | |
lineWithPassage(x, midY, x + w, midY); | |
lineWithPassage(midX, y, midX, y + h); | |
devide(x, y, w2, h2, depth2); | |
devide(midX, y, w2, h2, depth2); | |
devide(x, midY, w2, h2, depth2); | |
return devide(midX, midY, w2, h2, depth2); | |
}; | |
}; | |
processing.size(400, 400); | |
processing.background(255); | |
processing.fill(245); | |
processing.stroke(220, 10, 10); | |
processing.strokeWeight(1); | |
processing.rect(10, 10, 380, 380); | |
return devide(10, 10, 380, 380, 6); | |
}; | |
return processing.draw = function () { | |
return null; | |
}; | |
}; | |
function initSketch() { | |
var canvas = document.getElementById('canvas1'); | |
return new Processing(canvas, sketchProc); | |
}; | |
document.addEventListener('DOMContentLoaded', function () { | |
return initSketch(); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Misunderstood algorithm and ended up with a lot of closed chambers. Recording it for posterity.
Sample result: