Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Pythagoras tree demo

   1  
   2  REBOL [
   3      Title: "Tree of Pythagoras"
   4      Comment: {
   5          Based on an old E example by Raymond Hoving.
   6          Converted to REBOL by Gregg Irwin for testing purposes.
   7          Some speed mods. Pre-allocated block size, REBOLised the maths. Allen K
   8      }
   9  ]
  10  
  11  pyth-tree: func [
  12      a[pair!] b[pair!]
  13      depth[integer!] face
  14      /local c d e color
  15  ][
  16      c: d: e: 0x0
  17      ; Darken the color slightly at each level.
  18      color: depth * -10 + 0.255.0
  19      c/x: a/x - a/y + b/y
  20      c/y: a/x + a/y - b/x
  21      d/x: b/x + b/y - a/y
  22      d/y: a/x - b/x + b/y
  23      ; Not sure where the drift comes in, but it does. I.e. the tree
  24      ; is asymmetrical.
  25      e/x: c/x - c/y + d/x + d/y * 0.5 ;+ .49999999999999
  26      e/y: c/x + c/y - d/x + d/y * 0.5 ;+ .49999999999999
  27      append draw-cmds compose [pen (color) line (c) (a) (b) (d) (c) (e) (d)]
  28      ;-- Uncomment the 'show and 'wait lines to see it in action.
  29      ;show face
  30      if depth < 12 [
  31          pyth-tree c e depth + 1 face
  32          pyth-tree e d depth + 1 face
  33      ]
  34      ;wait 0
  35  ]
  36  
  37  world-size: 320x280 ;640x520
  38  start-pt-1: 133x235 ;266x450
  39  start-pt-2: 187x235 ;374x450
  40  
  41  ; Link/IOS seems to solve the speed problem caused by all the incremental
  42  ; allocations my original implementation caused. Version (A) lines are my
  43  ; original lines, and the (B) lines are Allen's speed mods.
  44  lay: layout [
  45      size world-size
  46      backdrop black
  47      origin 0x0
  48      ;canvas: image 640x480 effect [draw []]  ;(A)
  49      canvas: image 320x240   ;(B)
  50      across
  51      button "go" [
  52          clear draw-cmds
  53          show canvas
  54          ;print now/precise
  55          pyth-tree start-pt-1 start-pt-2 0 canvas
  56          ;print now/precise
  57          show canvas
  58      ]
  59      button "quit" [quit]
  60  ]
  61  ;draw-cmds: second canvas/effect ;(A)
  62  ; preallocate the space needed
  63  canvas/effect: reduce ['draw draw-cmds: make block! 90000]  ;(B)
  64  
  65  ;print ""
  66  view lay
  67  
  68  
  69  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS