//Stereogram #4 Time becomes ... //(C) 2004 Haruka Kataoka TimeStrokes strokes = new TimeStrokes(new Vector(10000,5000)); long timer = 0;//frames after startup int framesPerSec = 10; Translator trans; void setup() { size(500, 300); framerate(framesPerSec); noSmooth(); trans = new Translator(framesPerSec); } void loop() { trans.setTime(timer++); background(255, 255, 255); strokes.paint(trans); if (trans.isCursorEnable(mouseX, mouseY)) trans.drawCursor(trans.mouse2x(mouseX), trans.mouse2y(mouseY)); trans.drawWarning(mouseX); } // mouse operation void mousePressed() { if (trans.isCursorEnable(mouseX, mouseY)) strokes.start(trans.mouse2x(mouseX), trans.mouse2y(mouseY), timer); } void mouseReleased() { strokes.finish(trans.mouse2x(mouseX), trans.mouse2y(mouseY), timer); } void mouseDragged() { if (trans.isCursorEnable(mouseX, mouseY)) strokes.draw(trans.mouse2x(mouseX), trans.mouse2y(mouseY), timer); else strokes.finish(trans.mouse2x(mouseX), trans.mouse2y(mouseY), timer); } //class strokes with time class TimeStrokes { boolean isDrawing; Vector points; int prex, prey; //constructor TimeStrokes(Vector pointbuffer){ points = pointbuffer; } //start new stroke void start(int x, int y, long time){ if (isDrawing) finish(x, y, time); points.add(new TimeStrokesPoint(x, y, time, true)); prex = x; prey = y; isDrawing = true; } //add next point to the drawing stroke void draw(int x, int y, long time){ if (!isDrawing) return; if (abs(x-prex) + abs(y-prey) > 2) { points.add(new TimeStrokesPoint(x, y, time, false)); prex = x; prey = y; } } //finish the drawing stroke void finish(int x, int y, long time){ if (!isDrawing) return; points.add(new TimeStrokesPoint(x, y, time, false)); isDrawing = false; } //paint strokes void paint(Translator translator) { TimeStrokesPoint pnt, prePnt = null; try { for(Iterator itl = points.iterator(); itl.hasNext(); ){ pnt = (TimeStrokesPoint)itl.next(); if (prePnt!=null && !pnt.isStart) { translator.drawStroke(prePnt.x, prePnt.y, pnt.x, pnt.y, pnt.time); } prePnt = pnt; } }catch(Exception e){} } } //2D point with time and (start point or not) class TimeStrokesPoint { long time; int x, y; boolean isStart; TimeStrokesPoint(int ax, int ay, long atime, boolean aIsStart) { x = ax; y = ay; time = atime; isStart = aIsStart; } } //coordinate translation and painting class Translator { //time setting int loopSec = 300;//seconds of time looping int vanishSec = 120;//strokes vanish after vanishSec //int loopSec = 60; //for radio edit //int vanishSec = 25;//for radio edit //graphic setting float distance = 200; //distance from eye float parallax = 50.0;//default parallax float displayZBound = 100.0; float penWidth = 3.0;//default pen width // int loop; float time2z; int offset; long now; //constructor Translator(int fps) { loop = fps * loopSec; offset = loop/2; time2z = displayZBound / (fps * vanishSec); } //set current time void setTime(long aTime) { now = aTime; } //draw line from (fx,fy) to (tx,ty) with considering time void drawStroke(int fx, int fy, int tx, int ty, long time) { float z = (((now - time + offset) % loop) - offset) * time2z; if (abs(z) < displayZBound) { float r = distance / (distance+z);//display size rate fx = (int)(fx * r); fy = (int)(fy * r); tx = (int)(tx * r); ty = (int)(ty * r); int dp = (int)((parallax * r - parallax)/2.0); int alpha = (int)(255.0*((displayZBound-abs(z))/displayZBound)); if (alpha < 1) alpha = 1; stroke(color(0,0,0,alpha)); strokeWeight(r * penWidth); line(fx+150+dp, fy+150, tx+150+dp, ty+150);//left line(fx+350-dp, fy+150, tx+350-dp, ty+150);//right } } //draw cross cursor void drawCursor(int mx, int my) { stroke(0,0,0); strokeWeight(1); line(mx+140, my+150, mx+160, my+150);//left line(mx+150, my+140, mx+150, my+160);//left line(mx+340, my+150, mx+360, my+150);//right line(mx+350, my+140, mx+350, my+160);//right } //draw left or right bound indicator void drawWarning(int mx) { stroke(0,0,255); strokeWeight(1); if (mx < 50) { line(0 , 0 , 0 , 299);//left line(200, 0 , 200, 299);//left }else if(mx >= 250) { line(299, 0 , 299, 299);//left line(499, 0 , 499, 299);//left } } //translate mouseX to stroke's x int mouse2x(int mx) { return mx - 150; } //translate mouseY to stroke's y int mouse2y(int my) { return my - 150; } //is cursor in left image area ? boolean isCursorEnable(int mx, int my) { return (0 <= mx && mx < 300 && 0 <= my && my < 300); } }