Iann Wu
Iann Wu

Reputation: 155

How to draw a recursive hexagon art in scheme?

enter image description here If we want to draw a recursive art in scheme like the one above, how should we approach it?

Here is a rough draft that I have (not quite work as of now) edited from sierpinski triangle in scheme.

Thanks!

(define (hax fn)
    (repeat 6 (lambda () (fn) (lt 60))))

(define (haxa d k)
    (hax (lambda ()
            (if (= k 1) (fd d) (haxaleg d k)))))

(define (haxaleg d k)
    (haxa (/ d 2) (- k 1))
    (penup)
    (fd (* d 1.72))
    (lt 120)
    (pendown))

Upvotes: 1

Views: 1670

Answers (1)

Terje D.
Terje D.

Reputation: 6315

As you seem to want to recursively draw smaller hexagons at each second corner around a hexagon, the following method shoud work.

  1. Draw a hexagon of the desired size, returning to original position and direction
  2. Draw smaller hexagons at current position
  3. Move two sides forward, turning after each move
  4. Repeat point 2 and 3 twice more

In your dialect of scheme, this should be

(define (hexagon length)
    (pendown)
    (repeat 6 (lambda () 
                 (fd length)
                 (lt 60)))
    (penup))

(define (hexagons length levels)
    (hexagon length)   ;; Draw big hexagon
    (if (> levels 1)
        (repeat 3 (lambda ()
                     (hexagons (/ length 2) (- levels 1))  ;; Then smaller ones,
                     (repeat 2 (lambda ()                  ;; move to next corner
                          (fd length)
                          (lt 60)))))))

Upvotes: 3

Related Questions