Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circles drawn mainly in X an Y axises, WHY?

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
    }
like image 518
eneko Avatar asked Nov 18 '14 13:11

eneko


2 Answers

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..

like image 75
przemek hertel Avatar answered Sep 19 '22 04:09

przemek hertel


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.

like image 33
Durandal Avatar answered Sep 21 '22 04:09

Durandal