Placing items at "random" on a page

If you're having trouble using Context Free or don't understand the language, ask for help here.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
dzeni
Posts: 18
Joined: Wed Sep 28, 2005 2:18 pm
Location: Auckland, New Zealand
Contact:

Placing items at "random" on a page

Post by dzeni »

Hi,

I was wondering if there is a way to place objects at random on a page. For example, I have created a nice "ripple" that I want to appear randomly on the screen.

"Random" is probably not the quite the right word for it though. I would like to be able to set parameters such as size and HSB but would like to leave the actual placement (in terms of x # y #) to chance.

Anyway to do this??

Thanks

Jenni :)

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Post by MtnViewJohn »

The way this is often done is to do a random walk:

Code: Select all

rule walk {walk { x 4 y 5 }}
rule walk {walk { x -1 y -2 }}
rule walk {walk { x 2 y -3 }}
rule walk {walk { x -5 y 1 }}
rule walk {walk { x 0 y 3 }}
rule walk {walk { x 1 y -4 }}
rule walk {walk { x 3 y -5 }}
rule walk {walk { x -3 y 2 }}
rule walk {walk { x -1 y 3 }}
rule walk 0.01 {donewalking{}} 
This will execute walk rules for some random number of times, jogging around in a random manner, until it hits the bottom walk rule and actually draws the object. Notice that the sum of all the x translations is zero and the same is true for the sum of all y translations. This prevents there being a bias in a particular direction.

dzeni
Posts: 18
Joined: Wed Sep 28, 2005 2:18 pm
Location: Auckland, New Zealand
Contact:

Ripple Walking

Post by dzeni »

Hi,

Thanks for your speedy response. Am feeling somewhat foolish here as I can't seem to figure out how to make the rule work for my ripples. As in I don't know how to incorporate your lovely code into my code to make it draw the object in a random place several times over.

Below is the code to make the ripple in question:

Code: Select all

startshape ripple

rule ripple
{
	dotcircle{}
	dotcircle{size .8}
	dotcircle{size .6}
	dotcircle{size .4}
	dotcircle{size .2}
}

rule dotcircle
{
	quarter{}
	quarter{r 90}
	quarter{r 180}
	quarter{r 270}
}

rule quarter
{
	twocircles{}
	twocircles{r 5}
	twocircles{r 10}
	twocircles{r 15}
	twocircles{r 20}
	twocircles{r 25}
	twocircles{r 30}
	twocircles{r 35}	
	twocircles{r 40}
	twocircles{r 45}
	twocircles{r 50}
	twocircles{r 55}
	twocircles{r 60}
	twocircles{r 65}
	twocircles{r 70}
	twocircles{r 75}
	twocircles{r 80}
	twocircles{r 85}
	twocircles{r 90}
	twocircles{r 95}

}

rule twocircles
{
	CIRCLE{size .2 b 0}
	CIRCLE{x 10 size .8}
}
So how would I make the above ripple "walk" ??

Thanks in advance.

Dzeni :)

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Post by MtnViewJohn »

You would do this

Code: Select all

startshape walk

rule walk {walk { x 4 y 5 }} 
rule walk {walk { x -1 y -2 }} 
rule walk {walk { x 2 y -3 }} 
rule walk {walk { x -5 y 1 }} 
rule walk {walk { x 0 y 3 }} 
rule walk {walk { x 1 y -4 }} 
rule walk {walk { x 3 y -5 }} 
rule walk {walk { x -3 y 2 }} 
rule walk {walk { x -1 y 3 }} 
rule walk 0.01 {donewalking{}} 

rule donewalking{
	ripple{}
	walk{}
}
rule donewalking 0.1 {
	ripple {}
}
...
Have you thouh of implementing ripple using concentric black and white circles?

Code: Select all

rule ripple
{
   ring{} 
   ring{size .8} 
   ring{size .6} 
   ring{size .4} 
   ring{size .2} 
}

rule ring {
	CIRCLE {size 20}
	CIRCLE {size 19 b 1 }
	CIRCLE {size 2}
}

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Post by MtnViewJohn »

Oh. I just realized that the concentric approach doesn't work if they overlap. Your way is better.

dzeni
Posts: 18
Joined: Wed Sep 28, 2005 2:18 pm
Location: Auckland, New Zealand
Contact:

Post by dzeni »

Thanks for your help :)

Of course it works well.

Post Reply