Random Rotation

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

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
User avatar
jodawznev
Posts: 4
Joined: Tue Jul 10, 2007 7:21 pm
Contact:

Random Rotation

Post by jodawznev »

The code on the design ideas page for random rotation seems a little complicated. I was talking with TroulCortex and he suggested this:

Code: Select all

startshape Begin
 
background { h 200 sat 1 b 1 }
 
rule DrawArrow {
  SQUARE { x 0.5 size 1 0.075 }
  TRIANGLE { x 1 r 30 size 0.2 }
}
 
rule RandomRotate 360 { RandomRotate {r 1}}
rule RandomRotate {DrawArrow{}}
 
rule DrawRandomArrows { 30*{ b -0.05 z 1 } RandomRotate { } }
 
rule Begin { DrawRandomArrows { h 240 sat 1 b 1 } }
Is there a difference in results or performance?

User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Post by pakin »

Is there a difference in results or performance?
Performance. The code that you posted evaluates between 1 and 360 rules (with an average of 180 rules) to select a random angle. The code in the Design Ideas page evaluates only seven rules to select a random angle.

As a quick experiment I ran your code with 100,000 random arrows instead of 30 and rendered a 500x500 image. It took 1:06.96 from start to finish. I then ran the code from the Design Ideas page, again with 100,000 random arrows and a 500x500 image. This time it took only 0:11.06 from start to finish -- one-sixth of the time of the "simpler" approach.

mycelium
Posts: 29
Joined: Tue Aug 28, 2007 4:21 am
Location: Italy
Contact:

Post by mycelium »

The statistic distribution of random angles isn't omogeneus.

appling two rules that compensate may give a more omogeneus distribution.

Code: Select all


 startshape Begin
 
background { h 200 sat .1 b 1 }
 
rule DrawArrow {
  SQUARE { x 0.5 size 1 0.075 a -.998}
//  TRIANGLE { x 1 r 30 size 0.2 a -.9}
}
 

// ORIGINAL CODE
 
rule RandomRotate 360 { RandomRotate {r 1}}
rule RandomRotate {DrawArrow{}}
rule DrawRandomArrows { 6000*{ b -0.05 z 1 } RandomRotate { } }
  

// CODE USED TWO TIMES TO COMPENSATE STATISTIC DISTRIBUTION

rule RandomRotateC 360 { RandomRotateC {r 1}}
rule RandomRotateA 360 { RandomRotateA {r -1}}
rule RandomRotateC {RandomRotateA{}}

rule RandomRotateA {DrawArrow{}}
rule DrawRandomArrows2 { 5000*{ b -0.05 z 1 } RandomRotateC { } }
 

rule Begin { 
DrawRandomArrows { h 240 sat 1 b 1 } 
DrawRandomArrows2 { h 240 sat 1 b 1 x 2 } 
}
the code in the page Design ideas is more complicated but
- seems to have a omogeneous distribution
- have much better performances

i think in conclusion that best performances and best omogeneous distribution can be got with this code from Design ideas (that is the longest)

Code: Select all

rule RandomRotate { MyShape { } }
rule RandomRotate { MyShape { r 1 } }
rule RandomRotate { MyShape { r 2 } }
rule RandomRotate { MyShape { r 3 } }
rule RandomRotate { MyShape { r 4 } }
              /*
               *
               */
rule RandomRotate { MyShape { r 359 } }

User avatar
jodawznev
Posts: 4
Joined: Tue Jul 10, 2007 7:21 pm
Contact:

Post by jodawznev »

Thanks for the feedback!
I guess I could have tested the performance myself...
But I still don't understand what makes the angle distribution non-homogenous.
Care to enlighten me?

mycelium
Posts: 29
Joined: Tue Aug 28, 2007 4:21 am
Location: Italy
Contact:

Post by mycelium »

I'm sorry... I have just made a graphic test to understand it.
I discovered it when i drow my first CFDGs and i used something like this to simulate shadows on a sphere (softercore).
I can't give a mathematical explanation (i don't know much about). Maybe it is a consequence of recursive rules. Maybe the guys who wrote CFDG can give you a correct explanation.

troulcortex
Posts: 1
Joined: Sun Sep 23, 2007 3:29 pm

Statistics and Distribution

Post by troulcortex »

Hi guys,

First off: a compensation rule will cause a normal distribution rather than a homogenous distribution of angles. See http://www.contextfreeart.org/gallery/view.php?id=818 .

Now, as to exactly why the angles produced are non-homogenous using the first proposed solution by JodawZnev:

rule RandomRotate 360 { RandomRotate {r 1}}

The chance that this thing stops on the 1st iteration is 1/360.
2nd: (359/360) * (1/360) ........ it has to 'miss' the first one and 'hit' the second.
3rd: (359/360)*(359/360)*(1/360)

nth: ((359/360)^(n-1))*1/360

hence 359th: 0.37*1/360.... only 37% as likely to come out as the the first iteration!


You will agree that the probability that it stops at iteration i is greater than the probability of it stopping at i+1, although marginally.
The fact that the thing goes back around the circle if it goes past 360 does not compensate (i think you can work that one out).

This effect can be reduced to the point of being negligeable by increasing iteration rate:

rule RandomRotate 50000 { RandomRotate {r 1}}

Now, the chance of it stopping at 1 and at 360 are virtually identical:

(49,999/50,000)^360 = 0.993 is close enough to 1 to give the desired effect.... But it will take longer to render............

Does this clear things up enough?

mycelium
Posts: 29
Joined: Tue Aug 28, 2007 4:21 am
Location: Italy
Contact:

Post by mycelium »

thank you for the explanation. I am sorry i can't contribute to discussion... anyway i created a CFDG inspired by this topic:
http://www.contextfreeart.org/gallery/view.php?id=881

Post Reply