Lecture - Shape rotation with custom pivots - PApplet


float t = 1;                            // Simulated time variable

void setup()
{
        size(400,300);                        // Applet size
}


void draw()
{
        // Draw bg
        background(255);

        // Draw rotating rectangles
        drawRect(250,50,50,50,t);
        // With calculated motion !
        drawRect(250+(sin(t/8)*30.0),50+(cos(t/8)*30.0),50,50,t*8);

        // Draw nested motion rotating rects
        for(int c = 0; c < 10; c++)
        {
                // Define color fill with alpha blending
                // and draw with different positions
                fill(0, c*10);
                drawRect(40,40,10*c,10*c,t*c);
                drawRect(270-(5*c),200-(5*c),10*c,10*c,t*c);
        }
        t--;
}


void drawRect(float x, float y,float w, float h, float rot) {

        float orginx = w/2;                // calculate pivot
        float orginy = h/2;                // calculate pivot

        pushMatrix();
        translate(x+orginx,y+orginy);      // move coordinate system to pivot
        rotate(radians(rot));              // rotate coordinate system
        translate(-x-orginx,-y-orginy);    // move to old position
        rect(x, y, w, h);                  // draw rectangle
        popMatrix();
}

This website has been archived and is no longer maintained.