We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Making arrays of pixels in a circle
Page Index Toggle Pages: 1
Making arrays of pixels in a circle (Read 874 times)
Making arrays of pixels in a circle
Jan 24th, 2007, 6:50pm
 
I am making a game with pixel-sized particles. Is there a way to add to two existing arrays, particlesx and particlesy, a value in each for each point inside the circle?
Here is the code I used for adding one pixel where the mouse is clicked, which is what I want to do, only drop down a circle of particles instead of one:

 if(mousePressed)
 {
   if(mode == "draw")
   {
     
     if(!particle_exists(mouseX, mouseY))
     {
       particlesx = append(particlesx, mouseX);
       particlesy = append(particlesy, mouseY);
     }
   }
 }
Re: Making arrays of pixels in a circle
Reply #1 - Jan 24th, 2007, 7:02pm
 
You could try putting something like this instead of the straightforwards single particle addition:

Code:
float radius=3;
int nummberToAdd=6;
for(int i=0;i<numberToAdd;i++)
{
particlesx=append(particlesx,mouseX+radius*sin(i*(TWO_PI/numberToAdd)));
particlesy=append(particlesy,mouseY+radius*cos(i*(TWO_PI/numberToAdd)));
}
Re: Making arrays of pixels in a circle
Reply #2 - Jan 24th, 2007, 10:05pm
 
That seems to work with a few changes, but is there a way to make it fill the circle, like a paint program?
Thanks!
Re: Making arrays of pixels in a circle
Reply #3 - Jan 24th, 2007, 10:42pm
 
It should be possible, though it could be quite a slow operation if you're adding a lot of things.

Code:
int radius=3; // or whatever
float sqRadius=sq(radius); // calculate once only.
for(int x=-radius;x<=radius;x++)
{
for(int y=-radius;y<=radius;y++)
{
if(sq(x)+sq(y) <= sqRadius)
{
particlesx=append(particlesx,mouseX+x);
particlesy=append(particlesy,mouseY+y);
}
}
}
Re: Making arrays of pixels in a circle
Reply #4 - Jan 25th, 2007, 12:30am
 
That seems to only make a quarter of a circle.
(As you may have guessed, I'm not very good at math)
Re: Making arrays of pixels in a circle
Reply #5 - Jan 25th, 2007, 12:40am
 
I just tested it, and it definitely does a full circle, since it checks every pixel from -radius to radius in the x and y axes to see if the distance from the middle is less than or equal to the radius of the circle.
Re: Making arrays of pixels in a circle
Reply #6 - Jan 25th, 2007, 1:00am
 
Hmm, I re-copied and pasted it and it made a circle. I must have copied something wrong...
Re: Making arrays of pixels in a circle
Reply #7 - Jan 25th, 2007, 2:33am
 
If you're going to run this snippet of code to create the circle of particles often, you might want to rewrite it to use a lookup table rather than sin() and cos().  

Write a snippet of code to populate an array with the values of sin() and cos() you're likely to use, then reference that array instead of using the sin() and cos() functions.
Page Index Toggle Pages: 1