Thursday 28 June 2012

Fractal generation

Here an example of fractal generation with Rebol:
Here the source: REBOL [     title:   "Barnsley formula"     author: [cyphre@rebol.com "Massimiliano Vessi"]     type:   'graphic     Purpose: {         Michael Barnsley generated the fractal pattern that's now known         as Barnsley's Fern. It's supposed to resemble the Black Spleenwort,         Asplenium adiantum-nigrum. }     ] time: func [b /local start] [     start: now/precise     do b     print ["Time:" difference now/precise start]     ] img-size: 512x384 oftx: 200 isx: img-size/x img: make image! img-size img/rgb: 72.96.120 view/new layout [         across         ii: image img         return         aa: text "xxxxxxx"         text " / 10000"             ] random/seed now barnsley: func [/local x y nb t px py ry pxi pyi idx1 idx2 ][     x: 64.0     y: 0.0     px: 0.0     py: 0.0     ry: 0.0     nb: 10000       repeat k nb [               aa/text: to-string k         show [ii aa]             t:   ( (random 100 ) - 1 )   / 100                       either   (t >= 0.3 ) [             px: (x * 0.781)   + (y   * 0.034) + 13.76                 py: (x * -0.032) + ( y * 0.739) + 70.2             ;draw             x: px             y: py                       pxi: to-integer   x             pyi: to-integer y             idx1: ((320 - pyi) * isx) + pxi + oftx                       idx2: ((320 - pyi + 4 ) * isx ) + 4 + pxi + oftx                         poke img idx1 white             poke img idx2 gray             ;end draw             ] [             either   (t >= 0.17)   [                             px: (x * 0.17) - (y * 0.215) + 52.224                               py: (x * 0.222) + (y * 0.176) + 23.218                               ;draw                 x: px                 y: py                 pxi: to-integer x                 pyi: to-integer y                 idx1: ((320 - pyi) * isx) + pxi + oftx                 idx2: ((320 - pyi + 4) * isx) + 4 + pxi + oftx                 poke img idx1 white                 poke img idx2 gray                 ;end draw                 ] [                 either   (t >= 0.02 )   [                                     px: (x * -0.139) + (y * 0.263) + 72.96                     py: (x * 0.246) + ( y * 0.224) - 9.36                     ;draw                     x: px                     y: py                     pxi:   to-integer   x                                 pyi: to-integer y                     idx1: ((320 - pyi) * isx) + pxi + oftx                     idx2: ((320 - pyi + 4) * isx ) + 4 + pxi + oftx                     poke img idx1 white                     poke img idx2 gray                     ;end draw                     ] [                     px:   64.0                     py:   0.27 * y                     ]                           ]             ]                       ;draw             x: px             y: py             pxi: to-integer x                           pyi: to-integer y             idx1: ((320 - pyi ) * isx ) + pxi + oftx             idx2: ((320 - pyi + 4 ) * isx) + 4 + pxi + oftx             poke img idx1 white             poke img idx2 gray             ;end draw                           ]     ] time [barnsley] show ii wait 10

Wednesday 27 June 2012

Statistics

If you have a lot of data, and you need to know how many occurrences there are (i.e. you need statistic), it's very easy create a function to elaborate it. Look at this:
  tally: func [b [block!] /local c t v n] [
        c: sort copy b
        t: copy []
        while [not tail? c] [
            v: first c
            n: 0
            while [all [not tail? c   v = first c]] [
                n: n + 1
                c: next c
            ]
            append/only t reduce [v n]
        ]
        t
    ]


Now you can analyze all your data, look the example:


>> tally [$4 22 $4 $4 22 12:30 "hello" 12:30 ]
== [[22 2] [$4.00 3] [12:30 2] ["hello" 1]]


tally function returns you a block with the data and how many occurrences of that data in the series.

Tuesday 26 June 2012

How to make a full screen app

Sometime you want to full the screen: with an image, a text, or an application. Creating a full screen application with Rebol is extremely easy:
First of all you need to know the screen size, by using the system/view/screen-face/size variable:

>> system/view/screen-face/size
== 1280x1024


and the center-face function:


>> myapp: layout/size [ button "Close" [unview] ] system/view/screen-face/size
>> center-face myapp
>> view myapp


Here the result:
You can do more, you can center also the button:

>> myapp: layout/size [ a: button "Close" [unview] ] system/view/screen-face/size
>> center-face a
>> center-face myapp
>> view myapp

Here the result:
Ok, this was just a super simple small example. Why don't you add a background image? Some funny color or gradient?
Post your ideas in the comments below! I'll post yor result in the next posts. :-)

Friday 22 June 2012

One liner of the week


view center-face layout [image http://www.rebol.com/graphics/reb-logo.gif [unview]]


This one line
  1. downloads REBOL logo from the web
  2. opens a window centered on screen
  3. displays the logo
  4. closes the window when you click on the logo.

Can you do more with other programming languages?

Wednesday 20 June 2012

EuroJackpot

 EuroJackpot is a transnational lottery in Europe, Participating countries are (in alphabetical order) Denmark, Estonia, Finland, Germany, Italy, the Netherlands and Slovenia. The goal is to choose the five correct numbers out of 50 plus 2 out of another 8 numbers.

I made a small script to extract a random sequence to bet on:

Here the source:

Rebol [
    Title: "EuroJackpot extractor"
    File: %eurojackpot.r
    Author: "Massimiliano Vessi"
    Date: 2012-6-20
    Version: 0.0.2
    email: maxint@tiscali.it
    Purpose: {EuroJackpot is a transnational lottery in Europe,
    Participating countries are (in alphabetical order) Denmark,
    Estonia, Finland, Germany, Italy, the Netherlands and Slovenia.
    The goal is to choose the five correct numbers out of 50 plus 2 out of another 8 numbers.
    If you are lucky, please send me an email.
    }
    ]
random/seed now
;let's create the series with the number required
longserie: copy []
shortserie:   [1 2 3 4 5 6 7 8]
for i 1 50 1 [append longserie i ]
   
;extraction function    
extraction: func [] [
    the5:   copy/part (random longserie) 5
    the2: copy/part (random shortserie) 2
    sort the5
    sort the2
    ]
view/title layout [
    across
    Title "EuroJackpot extractor"
    return
    a1: text "xxx"
    a2: text "xxx"
    a3: text "xxx"
    a4: text "xxx"
    a5: text "xxx"
    return
    b1: text red "xx"
    b2: text red "xx"
    return
    aa: button 100x30 "Extract numbers" [
       
        extraction
        a1/text: the5/1
        a2/text: the5/2
        a3/text: the5/3
        a4/text: the5/4
        a5/text: the5/5
        b1/text: the2/1
        b2/text: the2/2
        show [a1 a2 a3 a4 a5 b1 b2]
        ]
    ] "EuroJackpot extractor"