User Tools

Site Tools


courses:2009.03.04.vector.loops

march 04 2009: vector shapes and loops

Using vector shape in Processing

  • Create a SVG file in Illustrator. If you use a ready-made symbol from the library, remember to right-click and “Break link to symbol”. Save the file as SVG (see image below for settings).

  • Make sure the SVG file is located in the same folder as your Processing .pde file. Download crow.svg.zip
  • Processing code for loading and displaying the vector shape
    // Loading a shape when using setup() and draw()
     
    PShape s;
     
    void setup() {
      size(800, 600);
      s = loadShape("crow.svg"); // file needs to be located in the sketch folder
      smooth();
      noLoop(); // no looping here, comment if you need dynamic sketch
      shapeMode(CENTER); // draw from center point of the shape
    }
     
    void draw() {
      shape(s, 400, 300); // display the shape on the stage, no size specified
      // shape(s, 400, 300, 100, 100);  // if you need to specify the width and height
    }

For loops to generate graphics/patterns

  • float rotationVal = 0; 
     
    void setup(){
      size(900, 600, P3D); // P3D needed to activate 3D functions and capabilities
      //smooth();
      rectMode(CENTER);  // we draw rect from the center
    }
     
    void draw(){
     
      background(100); // clear the background
     
      for(float j = 0; j < 15; j++){ // loop for rows
     
        for(float i = 0; i < 30; i++){  // loop for columns
     
          // save the current reference transformation matrix
          pushMatrix(); 
     
          // local transformation
          translate(i * 50, j * 50);
          rotateZ(tan(i * j * mouseX/50000));
          rect(0, 0, 70, 70); // draw at local x,y coordinates
     
          // restore to saved matrix, out of local reference
          popMatrix();
     
        } // end of loop for column
     
      } // end of loop for row
     
      //saveFrame(); // if you want to generate tiff for each frame => quicktime sequence
    }

courses/2009.03.04.vector.loops.txt · Last modified: 2009/03/06 00:03 by camille