////////////////////////////////////////// // // The Diurnal class supports day and night // class Diurnal { boolean dayTime; Position myPos; float msPerPixel; float lastPixelTime; color sunColor = color(192,192,0); color nightColor = color(32,16,96); color dayColor = color(64,64,255); Diurnal() { lastPixelTime = millis(); msPerPixel = 3.6e4 / width; dayTime = true; myPos = new Position(width/3,height/4); myPos.setEdgeOnOff(false); } Position getSunPos(){return myPos;} boolean IsDay(){return dayTime;} color currentSky(){return IsDay()? calcDayColor() :nightColor; } color calcDayColor() { float peak = width/2; float dw = myPos.getX() < peak ? myPos.getX(): width - myPos.getX(); float scaler = dw /peak; float gr = scaler * (green(dayColor) - green(nightColor)) +green(nightColor) ; float bl = scaler * (blue(dayColor) - blue(nightColor)) +blue(nightColor) ; float rd = scaler * (red(dayColor) - red(nightColor)) +red(nightColor) ; return color(rd,gr,bl); } void update() { if(millis() - lastPixelTime > msPerPixel) { //eraseSun(); float posX = myPos.getX(); if(posX >= width) { myPos.setX(0); dayTime = !dayTime; } else { myPos.setX(posX+1); } lastPixelTime = millis(); background(currentSky()); drawSun(); } } void drawSun() { if(IsDay()) { noStroke(); noSmooth(); fill(sunColor); ellipse(myPos.getX(),myPos.getY(),35,35); } } void eraseSun() { noStroke(); fill(currentSky()); ellipse(myPos.getX(),myPos.getY(),35,35); } } ////////////////////////////////////////// // // The position class supports wrapping at edges // class Position { boolean edgeDetection; float xPos; float yPos; Position(float x, float y) { edgeDetection = false; xPos = x; yPos = y; } Position(Position pos) { edgeDetection = false; xPos = pos.getX(); yPos = pos.getY(); } void setEdgeOnOff(boolean edge){edgeDetection = edge;} float getX() { return xPos;} float getY() { return yPos;} float checkEdge(float pos, float edge) { if(edgeDetection) { if (pos >= edge) { pos = 3; } else if( pos <= 0) { pos = edge - 3; } } return pos; } void setX(float x) { xPos = checkEdge(x,width); } void setY(float y) { yPos = checkEdge(y,height); } }