We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
I would like to connect the points in this circle to form a complete ring, having trouble though, this is what i have:
int numPoints = 50;
float angle, x, y;
float[] x2;
float[] y2;
void setup() {
size(600, 300);
smooth();
noFill();
}
void draw() {
background(#ffffff);
pushMatrix();
translate(width/2, height/2);
angle = TWO_PI/numPoints;
for (int i = 0; i < numPoints; i++) {
x = cos(angle*i)*numPoints;
y = sin(angle*i)*numPoints;
x2 = new float[numPoints];
y2 = new float[numPoints];
x2[i] =+ x;
y2[i] =+ y;
point(x,y);
// not sure what to do here
line(x,y, x2[i], y2[i]);
}
popMatrix();
}
Answers
There's probably a way better solution but I enjoyed playing with it:
no need for arrays
(takes advantage of the circular nature of sin / cos - cos(360) = cos(0), does mean you're calculating each point twice though)
if you do have an array of points (precalculated in setup(), perhaps) you use the % operator to ensure the index wraps at the end
because numPoints % numPoints = 0
alternatively, if you use beginShape(); and vertex(x[i], y[i]) then you can use endShape(CLOSE); which will close the shape automatically. see endShape in reference.
Modification on no-array version provided by @koogs that avoids duplicate calculations.
An even compacter full version: :P
the point wasn't to be compact, it was to be clear.