-
-
Save kvalv/aea0b0fb85a41622c02d6eb9d7141016 to your computer and use it in GitHub Desktop.
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
#lang racket | |
(require racket/draw) | |
(require threading) | |
(define seq "F+-[F]")(define delta-rot (* (/ pi 180) 90)) ; 29 degrees | |
(define d 10) | |
(define (pop lst) | |
(~> lst length sub1 (take lst _))) | |
(define (push lst v) | |
(append lst (list v))) | |
(define (move xy rot d) | |
(cons (+ (car xy) (* d (cos rot))) | |
(+ (cdr xy) (* d (sin rot))) | |
)) | |
; returns a (listof (listof (cons/c real? real?)) | |
(define (parse-instruction xy xy* rot rot* rules [p (list (cons 0.0 0.0))] [p* (list)]) | |
(define parse parse-instruction) | |
(cond | |
[(empty? rules) | |
(push p* p)] | |
[(cons? rules) | |
(match (first rules) | |
[#\F (define new-xy (move xy rot d)) | |
(parse new-xy xy* rot rot* (rest rules) (push p new-xy) p*)] | |
[#\+ (parse xy xy* (+ rot delta-rot) rot* (rest rules) p p*)] | |
[#\- (parse xy xy* (- rot delta-rot) rot* (rest rules) p p*)] | |
[#\[ (parse xy (push xy* xy) rot (push rot* rot) (rest rules) p p*)] | |
[#\] (define new-xy (last xy*)) | |
(define new-rot (last rot*)) | |
(parse new-xy (pop xy*) new-rot (pop rot*) (rest rules) (list new-xy) (push p* p))] | |
)] | |
)) | |
;; | |
(define initial-path "FF+FF[FF-FF][-FF-FF]+FF-FF") | |
(define paths (parse-instruction | |
(cons 0.0 0.0) | |
null | |
0 | |
null | |
(string->list initial-path))) | |
(define-values (height width) (values 300 300)) | |
(define target (make-bitmap height width)) | |
(define dc (new bitmap-dc% [bitmap target])) | |
(define-values (x-min x-max y-min y-max) | |
(let ([points (foldl append (list) paths)]) | |
(values (car (argmin car points)) | |
(car (argmax car points)) | |
(cdr (argmin cdr points)) | |
(cdr (argmax cdr points))))) | |
(define-values (dx dy) | |
(values (/ width (- x-max x-min)) | |
(/ height (- y-max y-min)))) | |
(define (scale-points points dx dy) | |
(map (lambda (x) (cons (* car x) dx | |
(* cdr x) dy)) | |
points)) | |
(define p (new dc-path%)) | |
(for ([coords paths]) | |
(define p (new dc-path%)) | |
(define-values (x0 y0) (values (car (first coords)) | |
(cdr (first coords)))) | |
(send p move-to x0 y0) | |
(send p lines coords) | |
(send dc draw-path p)) | |
target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment