int np = 20; //num of points
float x,y;
float xpos[][] = new float [np+2][100];
float ypos[][] = new float [np+2][100];
float angle = TWO_PI/np;
float r;
int num = 100;
void setup() {
size(600, 600);
background(0);
r = width*0.45;
}
void draw() {
translate(width/2, height/2);
background(0);
for (int j = 1; j < num; j++) {
for (int i = 0; i < np+1; i ++) {
if(j == 1) {
x = cos(angle*i) * r;
y = sin(angle*i) * r;
} else {
x = (xpos[i][j-1]+xpos[i+1][j-1])/2;
y = (ypos[i][j-1]+ypos[i+1][j-1])/2;
}
stroke( (j == 1)? color(255,255-j*10) : color(255,255-j*10) );
strokeWeight(5);
point(x, y);
xpos[i][j] = x;
ypos[i][j] = y;
}
for (int i = 0; i < np; i++) {
strokeWeight(1);
stroke( (j == 1)? color(255,255-j*10) : color(255,255-j*10) );
line(xpos[i][j], ypos[i][j], xpos[i+1][j], ypos[i+1][j]);
}
}
}
//void keyPressed() {
// if(key == ' ') {
// saveFrame( "pictures/frame####.png");
// }
//}
i want to make a n-edged right shape (equal side length, equal angles), and making another one, with edges on half points of sides. Hope that makes sense. I dont know why it turns to the middle. Its probably some typo. Thx
OK The problem must be in the calculation of the intermediate points but the use of arrays is confusing. The following sketch shows how it might be done using basic geometry and rotating the graphics context.
I have modified the calculation of the alpha channel of your grey lines and dots to that polygons at all depths are visible.
void setup() {
size(600, 600);
background(0);
}
void draw() {
translate(width/2, height/2);
background(0);
drawShape(20, 280, 30);
}
/**
Parameters
nbrSides = number of sides for each polygon (circle)
radius = radius of the outermost
*/
void drawShape(int nbrSides, float radius, int depth) {
pushMatrix();
float deltaAlpha = 230.0 / depth;
strokeWeight(1.2);
float r = radius;
float sideAngle = TWO_PI / nbrSides;
float sinSideAngle = sin(sideAngle);
float cosSideAngle = cos(sideAngle);
float sinHalfSideAngle = sin(sideAngle / 2);
for (int d = 0; d < depth; d++) {
int alpha = round(255 - deltaAlpha * d);
stroke(255, alpha);
fill(255, alpha);
float nx = r * cosSideAngle;
float ny = r * sinSideAngle;
for (int s = 0; s < nbrSides; s++) {
line(r, 0, nx, ny);
ellipse(r, 0, 3, 3);
rotate(sideAngle);
}
// Calculate the radius for the next polygon using Pythagoras
// Calculate half the chord length
float halfChordLength = r * sinHalfSideAngle;
// Calculate the next radius
r = sqrt(r * r - halfChordLength * halfChordLength);
// rotate by half side angle so inner touches outer polygon
// at chord centres
rotate(sideAngle / 2);
}
popMatrix();
}