Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: x: array [(amount)]
Pages: [1] Print
Author Topic: x: array [(amount)]  (Read 358 times)
keckman
Newbie
*
Offline Offline

Posts: 8


View Profile
x: array [(amount)]
« on: October 06, 2005, 03:30:39 AM »

I have two problem 1) and 2)

1) Can't i really use variable to tell to REBOL which amount of memory i want to reserve  for table like this:
amount: 10
table: array [(amount)]

There allways have to be an exact integer like table: array [10] (i dont wanna (or perhaps i cant) use dynamical data structures or something like that, i just wanna have global variable to tell how big thet tables are).

2) How can i use 'feel' in animations so that i can use variables when drawing many circles for example? Like when when drawing two circle i can do them in loop like this:
***********************
block1: copy []
for i 1 2 1 [
      append block1 compose [
         circle (x/(i)) 30
         ]
   ]
nakyma: layout [
    box 100.100.100 scr effect reduce ['draw block1]
**********************************
But, code under, where i use 'feel' doing animation i have to write eight time the same code and if i want to increase the amount of circles i have to do it by hand...:

           pen  col/1
            circle pos/1 r/1
            pen  col/2
            circle pos/2 r/2
            pen  col/3
            circle pos/3 r/3
            pen  col/4
            circle pos/4 r/4
            pen  col/5
            circle pos/5 r/5
            pen  col/6
            circle pos/6 r/6
            pen  col/7
            circle pos/7 r/7
     pen  col/8
            circle pos/8 r/8

**********************************
; i want to draw 1000 bouncing balls but not want to draw them by hand
; thats the problem in code under:
rebol []
maxx: 600
maxy: 400
b: as-pair maxx maxy

lkm: 8
xp1: array [8]
yp1: arraY [8]
xp2: array[8]
yp2: array[8]
pos: array[8]
r: array [8]
dy: array [8]
dx: array [8]
col: [255.0.0 0.255.0 0.0.255 200.200.0 0.255.255 128.128.128 255.0.255 100.255.20]
random/seed now

for i 1 lkm 1 [
   xp1/(i): 50 + random (maxx - 50)
   yp1/(i): 50 + random (maxy - 50)
   pos/(i):  as-pair xp1/(i) yp1/(i)
   r/(i): 10 + random 50
   dy/(i): 2 + ((random 1000 ) / 200)
   dx/(i): 2 + ((random 1000 ) / 200)
   if random 10 < 5 [
      dx/(i): - dx/(i)
   ]
   if random 10 < 5 [
      dy/(i): - dy/(i)
   ]
   
]

view layout [
    box as-pair maxx maxy black effect [
        draw [
           pen  col/1
            circle pos/1 r/1
            pen  col/2
            circle pos/2 r/2
            pen  col/3
            circle pos/3 r/3
            pen  col/4
            circle pos/4 r/4
            pen  col/5
            circle pos/5 r/5
            pen  col/6
            circle pos/6 r/6
            pen  col/7
            circle pos/7 r/7
     pen  col/8
            circle pos/8 r/8
           ]
    ]
    rate 30 feel [
            engage: func [face action event][
                switch action [
                    time [
                       for i 1 lkm 1 [
                           yp2/(i): yp1/(i) + dy/(i)
                           xp2/(i): xp1/(i) + dx/(i)
                           
                           if yp2/(i) <= r/(i) [
                              kk:(yp2/(i) - yp1/(i)) / (xp2/(i) - xp1/(i))
                              yp2/(i): r/(i)
                        xp2/(i): xp1/(i) + ((r/(i) - yp1/(i)) / kk)                        
                              dy/(i): - dy/(i)
                           ]
                           if yp2/(i) >= (maxy - r/(i)) [
                              kk:(yp2/(i) - yp1/(i)) / (xp2/(i) - xp1/(i))
                             yp2/(i): maxy - r/(i)
                              xp2/(i): xp1/(i) + ((yp2/(i) - yp1/(i)) / kk)
                              dy/(i): - dy/(i)
                           ]
                           
                           ;uusi xp2: xp1 + dx
                           if xp2/(i) <= r/(i) [
                              kk:(yp2/(i) - yp1/(i)) / (xp2/(i) - xp1/(i))
                              xp2/(i): r/(i)
                              yp2/(i): kk * (r/(i) - xp1/(i)) + yp1/(i)  
                              dx/(i): - dx/(i)
                           ]
                           if xp2/(i) >= (maxx - r/(i)) [
                              kk:(yp2/(i) - yp1/(i)) / (xp2/(i) - xp1/(i))
                              xp2/(i): maxx - r/(i)
                              yp2/(i): kk * (xp2/(i) - xp1/(i)) + yp1/(i)
                              dx/(i): - dx/(i)
                           ]
                           xp1/(i): xp2/(i)
                           yp1/(i): yp2/(i)
                           pos/(i)/x: xp2/(i)
                           pos/(i)/y: yp2/(i)  
                       ]              
                        show face
               ]
         ]
      ]
   ]
]
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
x: array [(amount)]
« Reply #1 on: October 06, 2005, 05:04:17 AM »

It's late here, so only time for your first question...
Code:
>> amount: 10
== 10
>> table: array/initial amount 0
== [0 0 0 0 0 0 0 0 0 0]
>> table: array/initial amount "x"
== ["x" "x" "x" "x" "x" "x" "x" "x" "x" "x"]
Hope that helps.
Logged

- Carl Read
keckman
Newbie
*
Offline Offline

Posts: 8


View Profile
x: array [(amount)]
« Reply #2 on: October 10, 2005, 01:55:32 AM »

Thankx, that helped. The other question is about this kind of situation, where i can't made the circles move

REBOL [
]
block: copy []
x: [100 200 300]
y: [50 200 100]
r: [50 70 90]
place: [0x0 0x0 0x0 ]
for i 1 3 1 [
   place/(i): as-pair (x/(i)) (y/(i))
       append block compose [
       circle (place/(i)) (r/(i))
    ]
]

view layout [
    box black 400x400 effect reduce ['draw block]
   
   rate 30 feel [
            engage: func [face action event][
                switch action [
                    time [
                       for i 1 3 1 [
                             place/(i)/x: place/(i)/x + 1
                       ]              
                        show face
               ]
         ]
      ]
   ]
   
]

Why i can't make the circles move to the right?
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
x: array [(amount)]
« Reply #3 on: October 12, 2005, 01:04:25 AM »

I'm not actually sure why your code doesn't work, but it seems the effects block is not your original block.

Anyway, by using code that works directly on the effects block, I got your circles to move.  See the code in the TIME block...

Code:
REBOL []

block: copy []
x: [100 200 300]
y: [50 200 100]
r: [50 70 90]
place: [0x0 0x0 0x0 ]
for i 1 3 1 [
   place/(i): as-pair (x/(i)) (y/(i))
   append block compose [
       circle (place/(i)) (r/(i))
   ]
]

view layout [
   box black 400x400 effect reduce ['draw block] rate 30 feel [
       engage: func [face action event][
           switch action [
               time [
                  ; for i 1 3 1 [
                  ;     place/(i)/x: place/(i)/x + 1
                  ; ]
                   effect-blk: face/effect/2
                   forskip effect-blk 3 [
                       effect-blk/2/x: effect-blk/2/x + 1
                   ]
                   show face
               ]
           ]
       ]
   ]
]

Incidentally, REBOL's FOR is very slow (or at least was before View1.3.)  If you can, it's better to use REPEAT.  I think tests showed it was about ten times faster!

Umm, yeah - not been changed...

>> t: now/time/precise repeat i 1000000 [] print now/time/precise - t
0:00:00.11

>> t: now/time/precise for i 1 1000000 1 [] print now/time/precise - t
0:00:04.78

And I was rather kind on the "ten times"... :-)  Your milage might differ...
 
Logged

- Carl Read
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: x: array [(amount)]
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 03, 2008, 11:07:24 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2315 Posts in 597 Topics by 4205 Members
Latest Member: TimTearton

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc