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 & HelpSyntax Questions › get all pixels in a certain area (slice)
Page Index Toggle Pages: 1
get all pixels in a certain area (slice) (Read 2442 times)
get all pixels in a certain area (slice)
Jun 14th, 2010, 10:23am
 
hello,

i want to access the color information of pixels in a slice of a circle, i.e. from 30° to 90°.

here is what i tried:
Code:

startAngle = 30:
endAngle = 90;

for (float a = startAngle; a < endAngle; a+=0.1) {
for (int i = 0; i < radius; i++) {
x = width / 2 + round ( i * cos( (a) * PI / 180) );
y = height/ 2 + round ( i * sin( (a) * PI / 180) );
test = pixels[y * width + x];
}
}


this works, but it is very unelegant, because i calculate multiple pixels (especially those near to the circle center) more than once.

does anyone know of a more effecient way to get the pixels in a slice?

thank you
Re: get all pixels in a certain area (slice)
Reply #1 - Jun 14th, 2010, 1:02pm
 
You also risk missing pixels near the circumference if the radius is large.

Try it the other way round iterate over all the pixels in the image calculate the equivalent x, y position from the pixel number then see if it is in the slice.
Code:

int x, y, l;
int cx = width/2;
int cy = height/2;

float ang;

for(int i = 0; i < pixels.length; i++){
 y = i / width - cy;
 x = i % width - cx;
 l = dist(x, y, cx, cy);
 if(l < radius){
   ang = degrees(atan2(y,x));
 }
 if(ang >= 30 && ang <=90){
   // inside slice
 }
}


This is rough and ready so there maybe the odd syntax/logic error but it might get you started.

Re: get all pixels in a certain area (slice)
Reply #2 - Jun 18th, 2010, 2:11am
 
thank you very much quark, this works very nice!

i used your code and added a part that builds a minimum size rectangle over the circle slice, and i only check the pixels within that rectangle, to save some resources by not checking every pixel on the screen each frame.

Page Index Toggle Pages: 1