// DEFINE canvas size
int xsize=400;
int ysize=400;
// COMPUTE center of canvas
int xhalf=xsize/2;
int yhalf=ysize/2;
// DEFINE inner and outer radii
float rinner=50;
float router=200;
// DEFINE number of random dots to draw
int NDOTS=100;
int dots=0;
void setup() {
// DEFINE a xsize pixels x ysize pixels canvas
size(xsize,ysize);
// DRAW outer circle with diameter 2*router pixels
ellipse (xhalf,yhalf,2*router,2*router);
// DRAW inner circle with diameter 2*rinner pixels and fill in as light GRAY
fill(200,200,200);
ellipse(xhalf,yhalf,2*rinner,2*rinner);
}
void draw() {
// DRAW no more than NDOTS
if(dots<NDOTS) dots++; else return;
// COMPUTE polar position of random dot
float r = (float)(randomWithinRange(rinner,router));
float theta=(float)(randomWithinRange(0.0,2*Math.PI));
//println("r="+r+" theta="+theta);
// COMPUTE x,y position of random dot
float xdot=r*cos(theta)+xhalf;
float ydot=r*sin(theta)+yhalf;
// DRAW a random dot 2 pixel diameter
ellipse(xdot,ydot,2,2);
}
// ===============================================================
// The following method: randomWithinRange(min,max) was copied from this webpage:
//
// http://stackoverflow.com/questions/7961788/math-random-explained
// ===============================================================
// RETURN a random value between two values
double randomWithinRange(double min, double max)
{
double range = Math.abs(max - min);
return (Math.random() * range) + (min <= max ? min : max);
}