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.
Page Index Toggle Pages: 1
Slight improvement to example (Read 863 times)
Slight improvement to example
Sep 30th, 2009, 9:42am
 
Hi, I'm new here, downloaded processing yesterday and registered today. To cut to the chase, had a quick look at some of the examples (presumably aimed at beginners) and some have a bit of a hackish appearance. Having already done a bit of straight java (swing etc) and recently been getting into the nuts and bolts of C++ I appreciate the straight forward accessibility of what I see here. Any how, not done much yet, but I think I might have imperoved the radial colour example a tad - It seems to prefer jumping from cartesian to polar rather than just stay in polar avoiding an unnecesary radians() conversion. Like thus:
Code:

/**
* Simple Radial Gradient
* by Ira Greenberg.
*
* Using the convenient red(), green()
* and blue() component functions,
* generate an array of radial gradients.
*/

void setup(){
size(600, 600);
background(0);
smooth();

// create a simple table of gradients
int columns = 4;
int radius = (width/columns)/2;
// create some gradients
for (int i=radius; i< width; i+=radius*2){
for (int j =radius; j< height; j+=radius*2){
createGradient(i, j, radius,
color(int(random(255)), int(random(255)), int(random(255))),
color(int(random(255)), int(random(255)), int(random(255))));
}
}
}

void createGradient (float x, float y, float radius, color c1, color c2){
float px = 0, py = 0, angle = 0;

// calculate differences between color components
float deltaR = red(c2)-red(c1);
float deltaG = green(c2)-green(c1);
float deltaB = blue(c2)-blue(c1);
// hack to ensure there are no holes in gradient
// needs to be increased, as radius increases
float gapFiller = 12.0;
float increment=(TWO_PI)/(360*gapFiller);// adds clarity being next to gapfiller def
for (int i=0; i< radius; i++){
for (float j=0; j<TWO_PI; j+=increment){
px = x+cos(angle)*i;
py = y+sin(angle)*i;
angle+=increment;
color c = color(
(red(c1)+(i)*(deltaR/radius)),
(green(c1)+(i)*(deltaG/radius)),
(blue(c1)+(i)*(deltaB/radius))
);
set(int(px), int(py), c);
}
}
// adds smooth edge
// hack anti-aliasing
noFill();
strokeWeight(3);
ellipse(x, y, radius*2, radius*2);
}

Only a suggestion
Many thanks for a brilliant API and IDE

Man on a mission
Page Index Toggle Pages: 1