Class object rotation

edited February 2016 in Questions about Code

I woulkd like the triangle fan to rotate. Of course I tried rotate(), but i couldn't get it working properly.

The Code:

Kranz stKranz;

int radian = 300;
int numbeams = 36;

void setup() {//setup
  smooth();
  size(600, 600);
  colorMode(RGB,360);
  background(360);
  stroke(360);
  stKranz = new Kranz ();
}
void draw() {
  //background(360);
  stKranz.display();
  //resetMatrix();
}

The Class Tab Code:

class Kranz{
  float beams = 360/numbeams;
  float r = 0;
  Kranz(){
  beginShape(TRIANGLE_FAN);
  vertex(width/2, height/2); //zentriert

  for (float angle = 0; angle <= 360; angle+=beams) {
    float vx = width/2 + cos(radians(angle))*radian;
    float vy = width/2 + sin(radians(angle))*radian;
    fill(angle, 0, 0);
    vertex(vx, vy);
  }
  endShape();
 }
void display(){
 }
}
Tagged:

Answers

  • Answer ✓
    Kranz stKranz;
    float a1;
    //
    // 
    
    
    void setup() {//setup
      size(600, 600);
      colorMode(RGB, 360);
      background(360);
      stroke(360);
      stKranz = new Kranz ();
    }
    
    void draw() {
      background(360);
    
      translate(width/2, height/2);
      rotate(a1);
    
      stKranz.display();
      a1+=.012;
    }
    
    
    class Kranz {
      int radian = 300;
      int numbeams = 36;
      float beams = 360/numbeams;
      float r = 0;
    
      // constr 
      Kranz() {
        //
      } // constr 
    
      void display() {
        beginShape(TRIANGLE_FAN);
        vertex(0, 0); //zentriert
    
        for (float angle = 0; angle <= 360; angle+=beams) {
          float vx =  cos(radians(angle))*radian;
          float vy =  sin(radians(angle))*radian;
          fill(angle, 0, 0);
          vertex(vx, vy);
        }
        endShape();
      }
    }// class
    //
    
  • edited January 2016

    actually you could make the shape in the constructor Kranz() and

    store it in a PShape variable

    and use it in display()

    explanation

    the point is that the rotation takes place around the origin (0,0)

    Hence, you need to draw the fan over 0,0

    it extends from -radius (minus radius) to plus radius so its center is exactly on the origin

  • // fan circus:
    // a rotating triangle holds three rotating fans 
    // (each fan at one corner of the triangle)  
    
    // the fans
    Kranz stKranz1, stKranz2, stKranz3;
    
    // the triangle angle: rotation 
    float angleTriangle = 0.0;
    float angleTriangleSpeed = .012;
    
    // -----------------------------------------------------
    // core functions 
    
    void setup() {
    
      size(1400, 1000);
      colorMode(RGB, 360);
      background(360);
      stroke(360);
    
      createKranzArray();
    }
    
    void draw() {
      background(360);
      translate(width/2, height/2);
      rotate(angleTriangle);
    
      stKranz1.display();
      stKranz2.display();
      stKranz3.display();
    
      angleTriangle+=angleTriangleSpeed;
    }
    
    // ---------------------------------------------------------
    // other functions 
    
    void createKranzArray() {
    
      // create triangle
      PVector[] centers = new PVector[3]; // center for each Kranz 
      int k=0;
      for (int i=0; i<360; i+=120) {
        // size of the triangle
        float r = 352; 
        // store 3 corners / centers 
        centers[k] = new PVector (0 + r*sin(radians(i)), 
          0 + r*cos(radians(i)));
        k++;
      }
    
      // use triangle for Kranz 
      stKranz1 = new Kranz (centers[0].x, centers[0].y, 0.0, 0.027); // slightly faster than triangle 
      stKranz2 = new Kranz (centers[1].x, centers[1].y, 0.302, -0.02); // backward 
      stKranz3 = new Kranz (centers[2].x, centers[2].y, 0.04, 0.00618); // slower than triangle
    
      // kill it 
      centers=null;
    }
    
    // =========================================================
    
    class Kranz {
      float x;  // position 
      float y; 
    
      int radian = 120; // radius 
    
      int numbeams = 36;
      float beams = 360/numbeams;
      // float r = 0;
    
      float ownRotation;
      float ownRotationSpeed; 
    
      // constr 
      Kranz( float x_, float y_, 
        float ownRotation_, float ownRotationSpeed_) {
        //
        x=x_;
        y=y_;
    
        ownRotation = ownRotation_;
        ownRotationSpeed = ownRotationSpeed_;
      } // constr 
    
      void display() {
        pushMatrix(); 
        translate(x, y);
        rotate (ownRotation); 
    
        beginShape(TRIANGLE_FAN);
        vertex(0, 0); //zentriert
    
        for (float angle = 0; angle <= 360; angle+=beams) {
          float vx =  cos(radians(angle))*radian;
          float vy =  sin(radians(angle))*radian;
          fill(angle, 0, 0);
          vertex(vx, vy);
        }
        endShape();
        popMatrix();
        ownRotation+=ownRotationSpeed;
      }
    }// class
    //
    
Sign In or Register to comment.