Hilbert curve: Difference between revisions

+Racket
(+Racket)
Line 1,761:
Output image:
[https://www.dropbox.com/s/anwtxtrnqhubh4a/HilbertCurve.jpg?dl=0 Hilbert curve]
 
=={{header|Racket}}==
 
<lang racket>#lang racket
 
(require 2htdp/image)
 
(define N 6)
(define R 8)
(define OFFSET 30)
 
(define the-pen (pen 'orange 2 'solid 'projecting 'round))
 
(define rules '([A . (- B F + A F A + F B -)]
[B . (+ A F - B F B - F A +)]))
 
(define (get-cmds n cmd)
(cond
[(= 0 n) (list cmd)]
[else (append-map (curry get-cmds (sub1 n))
(dict-ref rules cmd (list cmd)))]))
 
(match-define-values (image _ _ _)
(for/fold ([image empty-image]
[x 0]
[y 0]
[theta (/ pi 2)])
([cmd (in-list (get-cmds N 'A))])
(define-values (new-x new-y new-theta)
(match cmd
['F (values (+ x (* R (cos theta)))
(+ y (* R (sin theta)))
theta)]
['+ (values x y (+ theta (/ pi 2)))]
['- (values x y (- theta (/ pi 2)))]
[_ (values x y theta)]))
(values (add-line image x y new-x new-y the-pen)
new-x new-y new-theta)))
 
(overlay image (empty-scene (+ OFFSET (image-width image))
(+ OFFSET (image-height image))
'black))</lang>
 
=={{header|Scala}}==
Anonymous user