I removed the “while” statement to avoid variablescope problems there (I’ll add new statements to avoid drawing of-screen after this problem is solved).
Then I keep getting the “nextpolygon is not used” warning. I really don’t understand!! I use it clearly: " polygon[j] = new Polygon(nextpolygon);" and now it is in the same scope, so that’s not the trouble anymore.
Somehow the sketch never goes beyond j=1 either. For that, I removed the while and the endloop. But it didn’t solve the problem.
Polygon[] polygon = new Polygon[1];
PVector endcorner;
PVector nextpolygon = new PVector(300, 300);
int j=0;
void setup() {
size(600, 600);
background(204, 100, 30);
}
void draw() {
if (j>0) {
PVector nextpolygon = polygon[j].endcorner(); // give nextpolygon the value of the last vector "endcorner"
} // this function is part of the Polygon-class
println("nextpolygon="+nextpolygon);
polygon[j] = new Polygon(nextpolygon);
polygon[j].build();
polygon[j].display();
polygon = (Polygon[])expand(polygon, polygon.length+1);
j++;
println("j="+j);
}
class Polygon {
PVector endcorner = new PVector();
PVector firstcorner = new PVector();
PVector[] points;
Polygon(PVector start) {
firstcorner.set(start);
}
void build() {
println("Building polygon");
int numbercorners= int(random(3, 8)); // select number of corners between 3 and 8
float t = 360/numbercorners; // = 120, 90, 72, 60, 51.x, 45
points = new PVector[numbercorners]; // create an array to store the vectors of each corner
float rotation = random(0, 2 * PI); // = PI/5 = 36°
points[0] = new PVector(firstcorner.x, firstcorner.y);
for (int i=1; i < numbercorners; i++) { // calculate the vectors for each corner of the polygon
// i * t would generate a polygon with the same rotation between each corner,
// therefor adding random amount 'rotation'
// firstcorner is the center of a polygon
float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // x= a + r.cosθ, y= b + rsinθ
// r is the distance from the center to the corner, here: (random(12, 30)
// a and b are coordinates of the center of the polygon
float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30)); //
points[i] = new PVector(xcorner, ycorner);
}
}
void display() { // draw the polygon
println("drawing polygon");
beginShape();
for (int i = 0; i < points.length; i++) {
vertex(points[i].x, points[i].y);
}
endShape(CLOSE);
ellipse(points[points.length-1].x, points[points.length-1].y, 6, 6); // this is temporary, to see the last corner
}
PVector endcorner() { // pass the last vector to the next polygon
println("Passing vector");
int i = int(points.length-1);
println(points[i].x+" "+" "+points[i].y);
PVector endcorner = new PVector(points[i].x, points[i].y); // this passes the x and y values of the last point
println(endcorner.x+" "+" "+endcorner.y);
return endcorner;
}
}
Second version, where I tried to initialize in the if/else statement, gives the nullpointexception error again. I don’t understand at all!
Polygon[] polygon = new Polygon[1];
PVector endcorner, nextpolygon;
int j=0;
void setup() {
size(600, 600);
background(204, 100, 30);
}
void draw() {
if (j==0) {
PVector nextpolygon = new PVector(300, 300);
}
else {
PVector nextpolygon = polygon[j].endcorner(); // give nextpolygon the value of the last vector "endcorner"
} // this function is part of the Polygon-class
println("nextpolygon="+nextpolygon);
polygon[j] = new Polygon(nextpolygon);
polygon[j].build();
polygon[j].display();
polygon = (Polygon[])expand(polygon, polygon.length+1);
j++;
println("j="+j);
}
class Polygon {
PVector endcorner = new PVector();
PVector firstcorner = new PVector();
PVector[] points;
Polygon(PVector start) {
firstcorner.set(start);
}
void build() {
println("Building polygon");
int numbercorners= int(random(3, 8)); // select number of corners between 3 and 8
float t = 360/numbercorners; // = 120, 90, 72, 60, 51.x, 45
points = new PVector[numbercorners]; // create an array to store the vectors of each corner
float rotation = random(0, 2 * PI); // = PI/5 = 36°
points[0] = new PVector(firstcorner.x, firstcorner.y);
for (int i=1; i < numbercorners; i++) { // calculate the vectors for each corner of the polygon
// i * t would generate a polygon with the same rotation between each corner,
// therefor adding random amount 'rotation'
// firstcorner is the center of a polygon
float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // x= a + r.cosθ, y= b + rsinθ
// r is the distance from the center to the corner, here: (random(12, 30)
// a and b are coordinates of the center of the polygon
float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30)); //
points[i] = new PVector(xcorner, ycorner);
}
}
void display() { // draw the polygon
println("drawing polygon");
beginShape();
for (int i = 0; i < points.length; i++) {
vertex(points[i].x, points[i].y);
}
endShape(CLOSE);
ellipse(points[points.length-1].x, points[points.length-1].y, 6, 6); // this is temporary, to see the last corner
}
PVector endcorner() { // pass the last vector to the next polygon
println("Passing vector");
int i = int(points.length-1);
println(points[i].x+" "+" "+points[i].y);
PVector endcorner = new PVector(points[i].x, points[i].y); // this passes the x and y values of the last point
println(endcorner.x+" "+" "+endcorner.y);
return endcorner;
}
}