‹ pakt20pakt22 ›

pakt21

See /software/p5js/pakt-februari/pakt21/ for a JavaScript version and /software/supercollider/februari-pakt/pakt21/ for accompanying sound code.

#include "cinder/app/App.h"
#include "cinder/cairo/Cairo.h"

using namespace ci;
using namespace ci::app;
using namespace std;

const int WIDTH= 640;
const int HEIGHT= 480;

class pakt21App : public App {
public:
    void setup();
    void draw();
    cairo::SurfaceImage srfOff;
    cairo::Context ctxOff;
    int index, n;
    float w, h;
    vector<vec2> last;
    vector<Color> cols;
};

void pakt21App::setup() {
    srfOff= cairo::SurfaceImage(WIDTH, HEIGHT, false);
    ctxOff= cairo::Context(srfOff);
    ctxOff.setSourceRgb(0, 0, 0);
    ctxOff.paint();
    n= 150;
    index= 0;
    for(int i= 0; i<n; i++) {
        float t= float(i)/n*M_PI*2;
        last.push_back(vec2(0.0f, 0.0f));
        cols.push_back(Color(sin(t)*0.5f+0.5f, sin(t+M_PI)*0.5f+0.5f, 1.0f));
    }
    w= getWindowWidth();
    h= getWindowHeight();
}

void pakt21App::draw() {
    cairo::Context ctx(cairo::createWindowSurface());
    ctxOff.setSourceRgba(0, 0, 0, 0.05);
    ctxOff.paint();
    for(int i= 0; i<n; i++) {
        float x= sin((i*(sin(index*0.0126f+(sin(i)*sin(i*2)))*sin(index*0.0031f))+index)*0.074f);
        float y= cos((i*(sin(index*0.0152f+(sin(i)*sin(i*2)))*sin(index*0.0021f))+index)*0.064f);
        vec2 pos= vec2(x*w*0.45f+(w*0.5f), y*h*0.45f+(h*0.5f));
        if(index>0) {
            ctxOff.setSourceRgb(cols[i].r, cols[i].g, cols[i].b);
            ctxOff.circle(pos, abs((pos.x-last[i].x)+(pos.y-last[i].y))*0.5f);
            ctxOff.fill();
        }
        last[i]= pos;
    }
    index++;
    ctx.copySurface(srfOff, srfOff.getBounds());
}

CINDER_APP(pakt21App, Renderer2d, [&]( App::Settings *settings ) {
    settings->setWindowSize( WIDTH, HEIGHT );
    settings->setResizable( false );
})