Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created June 10, 2013 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehrmann/5750997 to your computer and use it in GitHub Desktop.
Save hoehrmann/5750997 to your computer and use it in GitHub Desktop.
; Bjoern Hoehrmann <bjoern@hoehrmann.de> <http://bjoern.hoehrmann.de>
(require (lib "graphics.ss" "graphics"))
; Konstanten
(define width 720) ; Fensterbreite
(define height 570) ; Fensterhoehe
(define substlist (list 'a 60 'a -120 'a 60 'a)) ; Koch-Ersetzungsliste
; Globale Variablen
(define (mit) (open-graphics) (open-viewport "fenster" width height))
; Oeffne ein Fenster und weise den Viewport an 'vp' zu.
(define vp (mit))
; Konvertiert Winkelgrad in Radianten
(define (d2r r) (/ (* 3.14159 r) 180))
; Zeichnet einen Linienzug, der durch die Liste '1' definiert ist.
; Dabei wird an Position 'x','y' mit dem Winkel 'a' begonnen.
; Jedes Linienstueck hat die Laenge 'len'.
(define (drawlist l x y a len)
(cond ((null? l)) ; Liste leer? Dann abbrechen.
; Falls erstes Listenelement eine Zahl, dann drehen und
; mit Rest der Liste weitermachen.
((number? (car l)) (drawlist (cdr l) x y (+ a (car l)) len))
; ansonsten ('a) einen Schritt voraus machen (an Position x2,y2)
; und von da aus mit Rest der Liste weitermachen.
(else (let ((x2 (+ x (* len (cos (d2r a)))))
(y2 (+ y (* len (sin (d2r a))))))
((draw-line vp) (make-posn x y) (make-posn x2 y2)
(make-rgb 0 0 0))
(drawlist (cdr l) x2 y2 a len)))))
; 1a, Ersetzt jedes Auftreten von 'a in liste durch Elemente von substlist
(define (list-replace liste substlist)
(cond
((null? liste)
())
((number? (car liste))
(cons (car liste) (list-replace (cdr liste) substlist)))
(else
(append substlist (list-replace (cdr liste) substlist)))
)
)
; 1b, Iter mal (func liste) auf sich selbst anwenden
(define (fractal func liste iter)
(if (> iter 1)
(func (fractal func liste (- iter 1)))
(func liste)
)
)
; Hilfsfunktion ohne Hilfsfunktion
(define (koch liste)
(list-replace
(list-replace
(list-replace
(list-replace liste substlist)
substlist)
substlist)
substlist)
)
; 1c, Loesung 1, Aufruf mit
; (drawlist (koch (list 'a)) 0 0 0 9)
; 1c, Loesung 2, Aufruf mit
; (drawlist
; (fractal (lambda (x) (list-replace x substlist)) (list 'a) 4)
; 0 0 0 9)
; 1d, Aufruf mit
; (drawlist (append
; (koch (list 'a)) (list 90)
; (koch (list 'a)) (list 90)
; (koch (list 'a)) (list 90)
; (koch (list 'a))
; ) 0 0 0 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment