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 › help with circle
Page Index Toggle Pages: 1
help with circle (Read 677 times)
help with circle
Nov 11th, 2006, 10:07pm
 
Hi,

I wondering if there was a way to make a circle with a different color for every pixel within the circle. Would I need trig for this? I am not in that high of a math class yet...

elictricocean
Re: help with circle
Reply #1 - Nov 12th, 2006, 2:14pm
 
Suppose the circle has center (cx, cy) and radius r.

1. Call function loadPixels() to load the pixel data for the display window into the system variable pixels[] array.
2. For each value of y that intersects the circle, and for each value of x such that (x, y) is in the circle, set the color of the corresponding element of pixels[].
3. Call function updatePixels to update the display window with the current data from pixels[].

Write nested for statements in step 2. y has values between (cy - r) and (cy + r). For each value of y, x has values between (cx - w) and (cx + w), where w = sqrt(r * r + sq(cy - y)). The element of pixels[] that corresponds to (x, y) has index (x + y * width). You do have to check that the pixels are in the window.

/Rick
Re: help with circle
Reply #2 - Nov 12th, 2006, 4:42pm
 
How about cheating?
Code:

void setup(){
size(200, 200);
background(200);
ellipse(100, 100, 200, 200);
smooth();
loadPixels();
for(int i = 0; i < pixels.length; i++){
if(pixels[i] != color(200)){
pixels[i] = color(random(255), random(255), random(255));
}
}
updatePixels();
}
Re: help with circle
Reply #3 - Nov 12th, 2006, 9:29pm
 
thanks. i think I like the first way better. If you could explain it a little more that would be great thanks.

elictricocean
Re: help with circle
Reply #4 - Nov 12th, 2006, 10:24pm
 
Drawing a picture for step 2 helps. Draw a circle with center (cx, cy) and radius r. Draw a horizontal line for a value of y that intersects the circle at points (x1, y) and (x2, y), where
x1 <= x2.

x1 is (cx - w) and x2 is (cx + w), where w = sqrt(r * r + sq(cy - y)) (Pythagoras' Theorem)

So, for every value of y such that the horizontal line at that value of y intersects the circle, you need to draw the pixels in the span between x1 and x2.

Code:

// determine y1, and y2, the minimum and maximum values of y
// such that the line at y intersects the circle and is
// in the window
for (int y = y1; y < y2; y++) {
// compute w
// determine x1 and x2, the minimum and maximum values
// of x such that (x, y) is inside the circle and
// is in the window
for (int x = x1; x < x2; x++) {
// assign a color to the pixel at (x, y)
}
}


/Rick
Page Index Toggle Pages: 1