2.24.2009

easy-to-use!

I'm beginning to think that Python being really easy to use is not a wholly positive feature. In a language where refactoring your code takes a long time, you're more likely to stop and think before you write something, like you're supposed to do. That's all for now.

Sierpinski Triangle

I've been playing around with Scheme (a dialect of Lisp (a weird language that I can't really describe)) by watching these videos and kind of just playing around. Today, I got a working Sierpinski triangle program. It's not particularly elegant, but it could be worse.

What's the best way to auto-convert spaces to non-breaking spaces?

; Draw a sierpinski triangle

; Get all six coords of vertices of equilateral triangle
; with tip at (x1, y1) and side length 'side'
(define (triangle x1 y1 side)
(let ((x2 (- x1 (/ side 2)))
(x3 (+ x1 (/ side 2)))
(y2 (- y1 (/ (* (sqrt 3) side) 2)))
(y3 (- y1 (/ (* (sqrt 3) side) 2))))
(list x1 y1 x2 y2 x3 y3)))

(define (draw-triangle g x1 y1 side)
(let ((tri (triangle x1 y1 side)))
(graphics-draw-line g (first tri)
(second tri)
(third tri)
(fourth tri))
(graphics-draw-line g (first tri)
(second tri)
(fifth tri)
(sixth tri))
(graphics-draw-line g (third tri)
(fourth tri)
(fifth tri)
(sixth tri))))

; Level-zero Sierpinski triangle is just a triangle
; Level-n triangle is three copies of level (n-1) triangle
(define (sier g n x1 y1 side)
(if (= n 0)
(draw-triangle g x1 y1 side)
(let ((tri (triangle x1 y1 (/ side 2))))
(sier g (- n 1) (first tri) (second tri) (/ side 2))
(sier g (- n 1) (third tri) (fourth tri) (/ side 2))
(sier g (- n 1) (fifth tri) (sixth tri) (/ side 2)))))

(define (new-sier g n x1 y1 side)
(graphics-clear g)
(sier g n x1 y1 side))

(define g (make-graphics-device 'win32 1000 1000))

;Call it like this:
(define side 2)
(new-sier g 6 0 (/ (* side (sqrt 3)) 4) side)