5

enter image description here

I have created a loop using processing that draws circles, the overall shape should be a circle. However they are mainly drawn close to X and Y axis. I have randomized the angle for the calculus of its location, I cannot see where the problem is.

Code as follows:

for (int omega = 0; omega<1080; omega++){  //loop for circle creation
    radius = (int)random(80);   //random radius for each circle

    int  color1= (int)random(100);  //little variation of color for each circle
    int  color2= (int)random(100);

        int locationY = (int)(sin(radians(omega))*random(width/2+1)); //location calcualtion
        int locationX = (int)(cos(radians(omega))*random(width/2+1));
        fill(0,color1+200,color2+200,50);
        ellipse(locationX,locationY,radius,radius); //draw circles
    }
2
  • Wich random implementation do you use ? The normal build in is sometimes prone to cluster values. Try using another seed or try another random implementation. There are a couple of real good ones. Try googling for Mersenne Twister, it is a real good one and you can find plenty implementations for java.
    – Ryu Kajiya
    Nov 18, 2014 at 13:12
  • What does "close to X and Y axis" mean? Can you provide a screenshot? Upload it to imgur.
    – Kevin
    Nov 18, 2014 at 13:16

2 Answers 2

5

Good point @Durandal (+1)

However I have one more thought with random circles.

When you generate random distance with such code:

double distance = random( width/2  );

you random from uniform distribution. I mean all values from 0 to width/2 have the same probability. But circle with 3*r radius have 9 times bigger area then circle with r radius. So, the smaller distance from center, the greater density we can see.

This way generated cloud do not have uniform density as you can see on first image. enter image description here

But if you change the probability density function in such way that bigger values are less probable that smaller ones you can get uniformly generated cloud.

Such simple modification:

double distance = Math.sqrt( random( width/2 * width/2 ) );

produces more uniformly distributed circles, as you can see on second image. enter image description here

I hope this can be helpful..

1
  • Good point, and very nicely demonstrated with the pictures +1
    – Durandal
    Nov 18, 2014 at 18:27
5

Its an artefact of the way you calculate location, you're pulling two different random values for the X and Y component:

int locationY = (int)(sin(radians(omega))*random(width/2+1)); //location calcualtion
int locationX = (int)(cos(radians(omega))*random(width/2+1));

You should use one random value "distance" from the center for both X and Y, that will remove the clustering towards the axes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.