DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Pythagoras Tree Demo

01.13.2006
| 2539 views |
  • submit to reddit
        
REBOL [
    Title: "Tree of Pythagoras"
    Comment: {
        Based on an old E example by Raymond Hoving.
        Converted to REBOL by Gregg Irwin for testing purposes.
        Some speed mods. Pre-allocated block size, REBOLised the maths. Allen K
    }
]

pyth-tree: func [
    a[pair!] b[pair!]
    depth[integer!] face
    /local c d e color
][
    c: d: e: 0x0
    ; Darken the color slightly at each level.
    color: depth * -10 + 0.255.0
    c/x: a/x - a/y + b/y
    c/y: a/x + a/y - b/x
    d/x: b/x + b/y - a/y
    d/y: a/x - b/x + b/y
    ; Not sure where the drift comes in, but it does. I.e. the tree
    ; is asymmetrical.
    e/x: c/x - c/y + d/x + d/y * 0.5 ;+ .49999999999999
    e/y: c/x + c/y - d/x + d/y * 0.5 ;+ .49999999999999
    append draw-cmds compose [pen (color) line (c) (a) (b) (d) (c) (e) (d)]
    ;-- Uncomment the 'show and 'wait lines to see it in action.
    ;show face
    if depth < 12 [
        pyth-tree c e depth + 1 face
        pyth-tree e d depth + 1 face
    ]
    ;wait 0
]

world-size: 320x280 ;640x520
start-pt-1: 133x235 ;266x450
start-pt-2: 187x235 ;374x450

; Link/IOS seems to solve the speed problem caused by all the incremental
; allocations my original implementation caused. Version (A) lines are my
; original lines, and the (B) lines are Allen's speed mods.
lay: layout [
    size world-size
    backdrop black
    origin 0x0
    ;canvas: image 640x480 effect [draw []]  ;(A)
    canvas: image 320x240   ;(B)
    across
    button "go" [
        clear draw-cmds
        show canvas
        ;print now/precise
        pyth-tree start-pt-1 start-pt-2 0 canvas
        ;print now/precise
        show canvas
    ]
    button "quit" [quit]
]
;draw-cmds: second canvas/effect ;(A)
; preallocate the space needed
canvas/effect: reduce ['draw draw-cmds: make block! 90000]  ;(B)

;print ""
view lay