If statement not working

edited February 2018 in Questions about Code

Hi, I think returning my PVector finally works, but now there's something wrong with my if-statement. Processing doesn't seem to see it.

I test this by using println-commands. "j" is 0, but somehow the if is passed by, because the "test J=0" is not printed in the console. Neither is the j>0 condition.

So I get "nextpolygon is undefined"...

Polygon[] polygon = new Polygon[1];
PVector endcorner, nextpolygon;
int j=0;

void setup() {
  size(600, 600);
  background(204, 100, 30);
}

void draw() {
    println(j);
    if (j=0) {
    PVector nextpolygon[j] = new PVector(300, 300);
    println("test j = 0" + nextpolygon[j].x);
    }
    else if(j>0){
    PVector nextpolygon[j] = polygon[j].endCorner();
    println("test j > 0" + nextpolygon[j].x);
    }
    println("test buiten if" + nextpolygon[j].x);
  while (nextpolygon.x<width) {
    polygon[j] = new Polygon(nextpolygon);
    polygon[j].build();
    polygon[j].display();
    polygon = (Polygon[])expand(polygon, polygon.length+1);
    j++;
    println(endcorner.x +" "+ nextpolygon.x + " " + " " +polygon.length);
  }

  noLoop();
}

class Polygon {
  PVector endCorner = new PVector();
  PVector firstcorner = new PVector();
  PVector[] points;

  Polygon(PVector start) {
    firstcorner.set(start);
  }

  void build() {
    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 regular polygon, therefor adding random amount 'rotation'
      float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // the random numer is the length of a side
      float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30));
      points[i] = new PVector(xcorner, ycorner);
    }
  }

  void display() {         // draw the 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);
  }

  PVector endCorner() {             // pass the last vector to the next polygon
    int i = int(points.length-1);

        println(points[i].x+" "+" "+points[i].y);

    PVector endcorner = new PVector(points[i].x, points[i].y);

        println(endcorner.x+" "+" "+endcorner.y);

    return endcorner;

  }
}

Answers

  • edited February 2018

    On line 2, you define a global PVector called nextpolygon:

    PVector endcorner, nextpolygon;
    

    On lines 13 and 17, you are defining another PVector (a local one), with the same name!

     PVector nextpolygon[j] = new PVector(300, 300);
    

    Any and all other problems aside, this is going to CONFUSE THE BEJAZZY out of you. I highly suggest you work out - in plain English words - what your variables are storing and the process you are following. Which variables are arrays? ONLY after you have it NAILED DOWN in regular words & understood completely should you then attempt to start writing some code.

  • I think "thank you" TfGuy44, although you sound quite hostile telling me "ONLY after you have it NAILED DOWN in regular words & understood completely should you then attempt to start writing some code."

    I see I was mistaken with the array-thing, but that's not the problem. That was a last minute change, because the error was already before I accidentally made nextpolygon an array...

    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 if(j>0){
        PVector nextpolygon = polygon[j].endCorner();
        }
    
      while (nextpolygon.x<width) {
        polygon[j] = new Polygon(nextpolygon);
        polygon[j].build();
        polygon[j].display();
        polygon = (Polygon[])expand(polygon, polygon.length+1);
        j++;
        println(endcorner.x +" "+ nextpolygon.x + " " + " " +polygon.length);
      }
    
      noLoop();
    }
    
    class Polygon {
      PVector endCorner = new PVector();
      PVector firstcorner = new PVector();
      PVector[] points;
    
      Polygon(PVector start) {
        firstcorner.set(start);
      }
    
      void build() {
        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 regular polygon, therefor adding random amount 'rotation'
          float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // the random numer is the length of a side
          float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30));
          points[i] = new PVector(xcorner, ycorner);
        }
      }
    
      void display() {         // draw the 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);
      }
    
      PVector endCorner() {             // pass the last vector to the next polygon
        int i = int(points.length-1);
    
            println(points[i].x+" "+" "+points[i].y);
    
        PVector endcorner = new PVector(points[i].x, points[i].y);
    
            println(endcorner.x+" "+" "+endcorner.y);
    
        return endcorner;
    
      }
    }
    

    I also tried

    if (j=0) {
    nextpolygon = new PVector(300, 300);
    }
    else if(j>0){
    nextpolygon = polygon[j].endCorner();
    }
    

    I think "this is going to CONFUSE THE BEJAZZY out of you" is funny and appears to be true :-).

  • Answer ✓

    if (j=0) { is not correct as it should be if (j==0) {

    https://processing.org/reference/equality.html

    I didn't test if there are more problems with your code. Using println functions to debug is a good idea. You should consider also using the debugging tool in the PDE. It is not difficult to use and you can step over your code and see what each condition evaluates to and the value of each of your variables in running time.

    Kf

Sign In or Register to comment.