<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: demo code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 20 Aug 2008 15:40:41 GMT</pubDate>
    <description>DZone Snippets: demo code</description>
    <item>
      <title>magic square demo</title>
      <link>http://snippets.dzone.com/posts/show/1155</link>
      <description>&lt;code&gt;&lt;br /&gt;REBOL [&lt;br /&gt;    Title:  "Magic Square program"&lt;br /&gt;    Author: "Gregg Irwin"&lt;br /&gt;    Comment: {&lt;br /&gt;        Credit to Bob Kurosaka and Martin Gardener for inspiration.&lt;br /&gt;    }&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;get-size: has [result] [&lt;br /&gt;    print "Enter the size of the square's side."&lt;br /&gt;    print ""&lt;br /&gt;    result: to-integer ask "It must be an odd number larger than 1 : "&lt;br /&gt;    if any [(result &lt; 3) (even? result)] [get-size]&lt;br /&gt;    result&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;edge-guard: does [&lt;br /&gt;    if row &lt; 1    [row: row + side]&lt;br /&gt;    if col &gt; side [col: col - side]&lt;br /&gt;    if row &gt; side [row: row - side]&lt;br /&gt;    if col &lt; 1    [col: col + side]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;print-square: func [square side] [&lt;br /&gt;    print ["Magic Square, Order " side]&lt;br /&gt;    print ["Each row, col, and Diagonal add up to " side * (side ** 2 + 1) / 2]&lt;br /&gt;    print ""&lt;br /&gt;    repeat row side [&lt;br /&gt;        repeat col side [&lt;br /&gt;            prin [pick square/:row col tab]&lt;br /&gt;        ]&lt;br /&gt;        print ""&lt;br /&gt;    ]&lt;br /&gt;    print ""&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;magic-square: func [/size sz [integer!]] [&lt;br /&gt;    side: either size [sz][get-size]&lt;br /&gt;    row: 1&lt;br /&gt;    col: (side + 1) / 2         ;Locate the starting cell&lt;br /&gt;    square: array/initial reduce [side side] 0&lt;br /&gt;    poke square/:row col 1      ;Initialize the first value&lt;br /&gt;&lt;br /&gt;    for i 2 to-integer (side ** 2) 1 [&lt;br /&gt;        row: row - 1&lt;br /&gt;        col: col + 1                  ;Northeast move&lt;br /&gt;        edge-guard&lt;br /&gt;        if 0 &lt;&gt; pick square/:row col [&lt;br /&gt;            row: row + 1              ;otherwise retreat&lt;br /&gt;            col: col - 1&lt;br /&gt;            row: row + 1              ;Break move&lt;br /&gt;            edge-guard&lt;br /&gt;        ]&lt;br /&gt;        poke square/:row col i&lt;br /&gt;    ]&lt;br /&gt;    print-square square side&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;;-- 0 based version --&lt;br /&gt;&lt;br /&gt;; print-square: func [square side] [&lt;br /&gt;;     print ["Magic Square, Order " side]&lt;br /&gt;;     print ["Each row, col, and Diagonal add up to " side * (side ** 2 + 1) / 2  - side]&lt;br /&gt;;     print ""&lt;br /&gt;;     repeat row side [&lt;br /&gt;;         repeat col side [&lt;br /&gt;;             prin [pick square/:row col tab]&lt;br /&gt;;         ]&lt;br /&gt;;         print ""&lt;br /&gt;;     ]&lt;br /&gt;;     print ""&lt;br /&gt;; ]&lt;br /&gt;; &lt;br /&gt;; magic-square: func [/size sz [integer!]] [&lt;br /&gt;;     side: either size [sz][get-size]&lt;br /&gt;;     row: 1&lt;br /&gt;;     col: (side + 1) / 2         ;Locate the starting cell&lt;br /&gt;;     square: array/initial reduce [side side] none&lt;br /&gt;;     poke square/:row col 0      ;Initialize the first value&lt;br /&gt;; &lt;br /&gt;;     repeat i to-integer (side ** 2) - 1 [&lt;br /&gt;;         row: row - 1&lt;br /&gt;;         col: col + 1                  ;Northeast move&lt;br /&gt;;         edge-guard&lt;br /&gt;;         if none &lt;&gt; pick square/:row col [&lt;br /&gt;;             row: row + 1              ;otherwise retreat&lt;br /&gt;;             col: col - 1&lt;br /&gt;;             row: row + 1              ;Break move&lt;br /&gt;;             edge-guard&lt;br /&gt;;         ]&lt;br /&gt;;         poke square/:row col i&lt;br /&gt;;     ]&lt;br /&gt;;     print-square square side&lt;br /&gt;; ]&lt;br /&gt;&lt;br /&gt;;-- End 0 based version --&lt;br /&gt;&lt;br /&gt;magic-square&lt;br /&gt;;magic-square/size 5&lt;br /&gt;&lt;br /&gt;halt&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Jan 2006 04:29:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1155</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>Pythagoras tree demo</title>
      <link>http://snippets.dzone.com/posts/show/1154</link>
      <description>&lt;code&gt;&lt;br /&gt;REBOL [&lt;br /&gt;    Title: "Tree of Pythagoras"&lt;br /&gt;    Comment: {&lt;br /&gt;        Based on an old E example by Raymond Hoving.&lt;br /&gt;        Converted to REBOL by Gregg Irwin for testing purposes.&lt;br /&gt;        Some speed mods. Pre-allocated block size, REBOLised the maths. Allen K&lt;br /&gt;    }&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;pyth-tree: func [&lt;br /&gt;    a[pair!] b[pair!]&lt;br /&gt;    depth[integer!] face&lt;br /&gt;    /local c d e color&lt;br /&gt;][&lt;br /&gt;    c: d: e: 0x0&lt;br /&gt;    ; Darken the color slightly at each level.&lt;br /&gt;    color: depth * -10 + 0.255.0&lt;br /&gt;    c/x: a/x - a/y + b/y&lt;br /&gt;    c/y: a/x + a/y - b/x&lt;br /&gt;    d/x: b/x + b/y - a/y&lt;br /&gt;    d/y: a/x - b/x + b/y&lt;br /&gt;    ; Not sure where the drift comes in, but it does. I.e. the tree&lt;br /&gt;    ; is asymmetrical.&lt;br /&gt;    e/x: c/x - c/y + d/x + d/y * 0.5 ;+ .49999999999999&lt;br /&gt;    e/y: c/x + c/y - d/x + d/y * 0.5 ;+ .49999999999999&lt;br /&gt;    append draw-cmds compose [pen (color) line (c) (a) (b) (d) (c) (e) (d)]&lt;br /&gt;    ;-- Uncomment the 'show and 'wait lines to see it in action.&lt;br /&gt;    ;show face&lt;br /&gt;    if depth &lt; 12 [&lt;br /&gt;        pyth-tree c e depth + 1 face&lt;br /&gt;        pyth-tree e d depth + 1 face&lt;br /&gt;    ]&lt;br /&gt;    ;wait 0&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;world-size: 320x280 ;640x520&lt;br /&gt;start-pt-1: 133x235 ;266x450&lt;br /&gt;start-pt-2: 187x235 ;374x450&lt;br /&gt;&lt;br /&gt;; Link/IOS seems to solve the speed problem caused by all the incremental&lt;br /&gt;; allocations my original implementation caused. Version (A) lines are my&lt;br /&gt;; original lines, and the (B) lines are Allen's speed mods.&lt;br /&gt;lay: layout [&lt;br /&gt;    size world-size&lt;br /&gt;    backdrop black&lt;br /&gt;    origin 0x0&lt;br /&gt;    ;canvas: image 640x480 effect [draw []]  ;(A)&lt;br /&gt;    canvas: image 320x240   ;(B)&lt;br /&gt;    across&lt;br /&gt;    button "go" [&lt;br /&gt;        clear draw-cmds&lt;br /&gt;        show canvas&lt;br /&gt;        ;print now/precise&lt;br /&gt;        pyth-tree start-pt-1 start-pt-2 0 canvas&lt;br /&gt;        ;print now/precise&lt;br /&gt;        show canvas&lt;br /&gt;    ]&lt;br /&gt;    button "quit" [quit]&lt;br /&gt;]&lt;br /&gt;;draw-cmds: second canvas/effect ;(A)&lt;br /&gt;; preallocate the space needed&lt;br /&gt;canvas/effect: reduce ['draw draw-cmds: make block! 90000]  ;(B)&lt;br /&gt;&lt;br /&gt;;print ""&lt;br /&gt;view lay&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Jan 2006 04:27:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1154</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>spirograph demo</title>
      <link>http://snippets.dzone.com/posts/show/1153</link>
      <description>&lt;code&gt;&lt;br /&gt;REBOL [&lt;br /&gt;    Title:  "Simple Spirograph"&lt;br /&gt;    Author: "Gregg Irwin"&lt;br /&gt;    EMail:  greggirwin@acm.org&lt;br /&gt;&lt;br /&gt;    Comment: {&lt;br /&gt;        Set max number of lines&lt;br /&gt;        Generate random start point and velocity&lt;br /&gt;        at each iteration&lt;br /&gt;            for each set&lt;br /&gt;                calculate next set of end-points from previous end-points&lt;br /&gt;                append line command to draw effect, wrap if at max-lines&lt;br /&gt;&lt;br /&gt;        TBD&lt;br /&gt;            Allow for multiple line "sets".&lt;br /&gt;            Figure out why it's so slow for large canvases.&lt;br /&gt;            Use color-shifting lines, rather than restarting with a new color.&lt;br /&gt;    }&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;num-lines: 30   ; change this for longer, or shorter, line sets.&lt;br /&gt;end-points: make block! 128&lt;br /&gt;pt-1: pt-2: 0x0&lt;br /&gt;delta-1: delta-2: 0x0&lt;br /&gt;direction-1: direction-2: 'SE&lt;br /&gt;color: 0.0.0&lt;br /&gt;&lt;br /&gt;directions: [&lt;br /&gt;    SE   1x1&lt;br /&gt;    NW  -1x-1&lt;br /&gt;    NE   1x-1&lt;br /&gt;    SW  -1x1&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;initialize: func [&lt;br /&gt;    "Initialize starting coordinates, directions, and colors."&lt;br /&gt;    face&lt;br /&gt;    i [integer!]&lt;br /&gt;][&lt;br /&gt;    random/seed now&lt;br /&gt;    ; Starting locations for line end points&lt;br /&gt;    pt-1: random face/size&lt;br /&gt;    pt-2: random face/size&lt;br /&gt;    change/only at end-points i [pt-1 pt-2]&lt;br /&gt;    ; Direction of travel&lt;br /&gt;    direction-1: pick [SE NW NE SW] random 4&lt;br /&gt;    direction-2: pick [SE NW NE SW] random 4&lt;br /&gt;    ; Angle of travel. Bigger numbers move things faster.&lt;br /&gt;    ;delta-1: 2 + (random 3x3)&lt;br /&gt;    ;delta-2: 2 + (random 3x3)&lt;br /&gt;    delta-1: random 3x3&lt;br /&gt;    delta-2: random 3x3&lt;br /&gt;    ; Colors. The + 32 keeps it from being too dark.&lt;br /&gt;    color: (random 200.200.200) + 32&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;move-points: func [face] [&lt;br /&gt;    ; direction-1 = Direction of Travel for Point 1&lt;br /&gt;    ; direction-2 = Direction of Travel for Point 2&lt;br /&gt;&lt;br /&gt;    ;TBD Should be able to map these so the code isn't duped for each point.&lt;br /&gt;    pt-1: pt-1 + (delta-1 * (select directions direction-1))&lt;br /&gt;    switch direction-1 [&lt;br /&gt;        NE [if pt-1/x &gt; face/size/x [direction-1: 'NW]&lt;br /&gt;            if pt-1/y &lt; 1 [direction-1: 'SE]]&lt;br /&gt;        SE [if pt-1/x &gt; face/size/x [direction-1: 'SW]&lt;br /&gt;            if pt-1/y &gt; face/size/y [direction-1: 'NE]]&lt;br /&gt;        SW [if pt-1/x &lt; 1 [direction-1: 'SE]&lt;br /&gt;            if pt-1/y &gt; face/size/y [direction-1: 'NW]]&lt;br /&gt;        NW [if pt-1/x &lt; 1 [direction-1: 'NE]&lt;br /&gt;            if pt-1/y &lt; 1 [direction-1: 'SW]]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    pt-2: pt-2 + (delta-2 * (select directions direction-2))&lt;br /&gt;    switch direction-2 [&lt;br /&gt;        NE [if pt-2/x &gt; face/size/x [direction-2: 'NW]&lt;br /&gt;            if pt-2/y &lt; 1 [direction-2: 'SE]]&lt;br /&gt;        SE [if pt-2/x &gt; face/size/x [direction-2: 'SW]&lt;br /&gt;            if pt-2/y &gt; face/size/y [direction-2: 'NE]]&lt;br /&gt;        SW [if pt-2/x &lt; 1 [direction-2: 'SE]&lt;br /&gt;            if pt-2/y &gt; face/size/y [direction-2: 'NW]]&lt;br /&gt;        NW [if pt-2/x &lt; 1 [direction-2: 'NE]&lt;br /&gt;            if pt-2/y &lt; 1 [direction-2: 'SW]]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;do-spiro: func [&lt;br /&gt;    face&lt;br /&gt;    max-interval [time!]&lt;br /&gt;    /local i t interval&lt;br /&gt;][&lt;br /&gt;    i: j: 0&lt;br /&gt;    t: now/time/precise&lt;br /&gt;    interval: 0:0:3 + random max-interval&lt;br /&gt;    initialize face i&lt;br /&gt;    forever [&lt;br /&gt;        if now/time/precise &gt; (t + interval) [&lt;br /&gt;            clear draw-cmds&lt;br /&gt;            interval: 0:0:3 + random max-interval&lt;br /&gt;            t: now/time/precise&lt;br /&gt;            initialize face i&lt;br /&gt;            wait .05&lt;br /&gt;        ]&lt;br /&gt;&lt;br /&gt;        append draw-cmds compose [pen (color) line (pt-1) (pt-2)]&lt;br /&gt;        ; Erase oldest line&lt;br /&gt;        if (length? draw-cmds) &gt; (num-lines * 5) [&lt;br /&gt;            remove/part head draw-cmds 5&lt;br /&gt;        ]&lt;br /&gt;&lt;br /&gt;        ;print mold draw-cmds&lt;br /&gt;        move-points face&lt;br /&gt;        i: either i &lt; 127 [i + 1][0]&lt;br /&gt;        j: either j &lt; 127 [j + 1][0]&lt;br /&gt;&lt;br /&gt;        show face&lt;br /&gt;        ; wait 0 isn't as system friendly as wait .001, but it's faster.&lt;br /&gt;        wait 0 ;.001&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;lay: layout [&lt;br /&gt;    origin 0x0&lt;br /&gt;    backdrop black&lt;br /&gt;    ; Using a larger canvas really slows things down&lt;br /&gt;    ;canvas: image black 640x480&lt;br /&gt;    canvas: image black 320x240&lt;br /&gt;    across&lt;br /&gt;    button "go" [do-spiro canvas 0:0:10]&lt;br /&gt;    button "quit" [quit]&lt;br /&gt;]&lt;br /&gt;canvas/effect: reduce ['draw draw-cmds: make block! (num-lines * 5)]&lt;br /&gt;&lt;br /&gt;view lay&lt;br /&gt;;view/offset lay 0x0&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Jan 2006 04:26:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1153</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
