the use of translate inside a for statement?

inside the for statement I seem don't understand the mechanism of the 2nd translate( 0 ,8)

here is the orig code from the book : (see below)

what does this mean ?

for each "for" statements run an addition + 8 in the original translate(xpos, 0) is added and additional -0.12 for the angle :  since its 14 units meaning 14 lines

1st run: translate(xpos, 0); rotate 0      
2nd run: translate(xpos, 8); rotate -12
3rd run: translate(xpos, 16);  rotate - 24
4th run: translate(xpos, 32); rotate - 36
so on;...

hence "line" is really :

1st run line (20,0,20,0);rotate 0 
2nd run line (20,0,20,8);rotate -12
3rd run line (20,0,20,16); rotate - 24
4th run line (20,0,20,32); rotate - 36  

in the real coordinates of processing ?

Answers

  • edited July 2015
    int x = 20; // X-coordinate
    int u = 14; // Units
    float a = -0.12; // Angle
    
    void setup() {
      size(100, 100);
      stroke(0, 153);
      smooth();
      noLoop();
    }
    
    void draw() {
      background(204);
      tail(20, 14, 0.22);
    }
    
    void tail(int xpos, int units, float angle) {
      pushMatrix();
      translate(xpos, 0);
      for (int i = units; i > 0; i--) { // Count in reverse
        strokeWeight(i);
        line(0, 0, 0, 8);
        translate(0, 8);
        rotate(angle);
      }
      popMatrix();
    }
    
  • Answer ✓

    It helps to be able to see what you are drawing...

    int x = 20; // X-coordinate
    int u = 14; // Units
    float a = -0.12; // Angle
    
    void setup() {
      size(400, 400);
      smooth();
    }
    
    void origin_axies() {
      pushStyle();
      stroke(255, 0, 0);
      line(0, 0, 20, 0);
      fill(0);
      text("x", 20, 0);
      stroke(0, 255, 0);
      line(0, 0, 0, 20);  
      text("y", 0, 20);
      popStyle();
    }
    
    void draw() {
      background(255);
      translate(width/2, height/2);
      origin_axies();
      stroke(0, 153);
      tail(20, 14, 0.22);
      noLoop();
    }
    
    void tail(int xpos, int units, float angle) {
      pushMatrix();
      translate(xpos, 0);
      for (int i = units; i > 0; i--) { // Count in reverse
        strokeWeight(i);
        line(0, 0, 0, 8);
        translate(0, 8);
        rotate(angle);
      }
      popMatrix();
    }
    
  • but wat bout trans inside the for ? what number do it originate ? to the first line ? or to the last?

Sign In or Register to comment.