Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nossidge
Created March 6, 2013 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nossidge/5100925 to your computer and use it in GitHub Desktop.
Save nossidge/5100925 to your computer and use it in GitHub Desktop.
SpinningJaggedTargets
// Save as animated GIF
// Requires gifAnimation Processing Library
// http://www.extrapixel.ch/processing/gifAnimation/
import gifAnimation.*;
GifMaker gifExport;
boolean saveToGif = false;
// The time between GIF frames
int frameDelay = 60;
// The pixel size for the canvas
float canvasDimRatio = 1;
int xCanvasDimension = int(300*canvasDimRatio);
int yCanvasDimension = xCanvasDimension;
// Declare the Shape array
ArrayList<Shape> rectArray = new ArrayList<Shape>();
int i = 0;
//////////////////////////////////////////////////////////////////////
void setup(){
frameRate(11);
size(xCanvasDimension,yCanvasDimension);
createCircles();
}
//////////////////////////////////////////////////////////////////////
void createCircles() {
rectArray = new ArrayList<Shape>();
Shape newRect;
float rectCount = 11;
float rectDistanceBetween = 20;
float rectDim = rectDistanceBetween * rectCount;
for (int i = 0; i < rectCount; i++) {
newRect = new Shape(xCanvasDimension/2 - rectDim/2, yCanvasDimension/2 - rectDim/2,
//rectDim, rectDim, 0, 0, color(255,100), color(0));
//rectDim, rectDim, 0, 0, color(random(0,255),100), color(0));
//rectDim, rectDim, 0, 0, color(random(0,255),random(0,255),random(0,255),100), color(0));
rectDim, rectDim, 0, 0, color(random(0,255),random(0,255),random(0,255),100),
color(random(0,255),random(0,255),random(0,255),100));
rectArray.add(newRect);
rectDim = rectDim - rectDistanceBetween;
}
}
//////////////////////////////////////////////////////////////////////
void draw() {
background(0);
for(Shape iRect:rectArray) {
translate(width/2, height/2);
rotate(radians(i));
translate(-width/2, -height/2);
iRect.drawShape();
i++;
}
//i = i + 1;
gifAddFrame();
}
//////////////////////////////////////////////////////////////////////
void keyPressed(){
println("pressed " + int(key) );
switch( int(key) ) {
case 114: saveToGif = true; gifCreateNew(); break; // _R_ecord new gif
case 115: gifSave(); break; // _S_ave
case 110: createCircles(); break; // _N_ew pattern
}
}
void stop() {
gifSave();
}
//////////////////////////////////////////////////////////////////////
void gifCreateNew() {
if (saveToGif) {
gifExport = new GifMaker(this, System.currentTimeMillis() + ".gif");
gifExport.setRepeat(0);
}
}
void gifAddFrame() {
if (saveToGif) {
gifExport.setDelay(frameDelay);
gifExport.addFrame();
}
}
void gifSave() {
if (saveToGif) {
gifExport.setDelay(frameDelay);
gifExport.finish();
saveToGif = false;
}
}
//////////////////////////////////////////////////////////////////////
// NOT the same Shape class as before
// This one draws from x & y pixel pos, not cell
class Shape extends PVector {
String defaultShape = "recct";
PVector shapeDim = new PVector();
PVector shapeVel = new PVector();
PVector originalPos = new PVector();
PVector originalDim = new PVector();
Boolean inOriginalPos = true;
Boolean offScreen = false;
Boolean killed = false;
color fill;
color stroke;
int zLayer; // White should be on top, so draw them last
int drawOrigin = CORNER; // Supports CORNER and CENTER only
////////////////////////////////////////////
Shape(float xPos, float yPos,
float xCellW, float yCellH,
float xVel, float yVel,
color inputFill, color inputStroke) {
this.set(xPos, yPos, 0.0);
originalPos.set(xPos, yPos, 0.0);
shapeDim.set(xCellW, yCellH, 0.0);
originalDim.set(xCellW, yCellH, 0.0);
shapeVel.set(xVel, yVel, 0.0);
this.fill = inputFill;
this.stroke = inputStroke;
}
////////////////////////////////////////////
// CENTER is more accurate, but CORNER is more weird
void setColors() {
fill(this.fill);
stroke(this.stroke);
ellipseMode(this.drawOrigin);
rectMode(this.drawOrigin);
strokeWeight(1);
}
void setDrawOrigin(int originType){
this.drawOrigin = originType;
}
////////////////////////////////////////////
void drawShape() {
drawShape(this.shapeDim.x, this.shapeDim.y);
}
void drawShape(float dimensions) {
drawShape(dimensions, dimensions);
}
void drawShape(float xWidth, float yHeight) {
if (defaultShape == "rect") {
drawRect();
} else if (defaultShape == "ellipse") {
drawEllipse(xWidth,yHeight);
} else if (defaultShape == "quad") {
drawRandomQuad(xWidth,yHeight);
} else {
drawRandomQuad(xWidth,yHeight);
}
}
////////////////////////////////////////////
void drawEllipse() {
drawEllipse(this.shapeDim.x, this.shapeDim.y);
}
void drawEllipse(float dimensions) {
drawEllipse(dimensions, dimensions);
}
void drawEllipse(float xWidth, float yHeight) {
setColors();
// If CORNER
float xPos = this.x;
float yPos = this.y;
if (drawOrigin==CENTER) {
xPos = this.x + (this.originalDim.x / 2);
yPos = this.y + (this.originalDim.y / 2);
}
ellipse(xPos, yPos, xWidth, yHeight);
}
////////////////////////////////////////////
void drawRect() {
drawRect(this.shapeDim.x, this.shapeDim.y);
}
void drawRect(float dimensions) {
drawRect(dimensions, dimensions);
}
void drawRect(float xWidth, float yHeight) {
setColors();
// If CORNER
float xPos = this.x;
float yPos = this.y;
if (drawOrigin==CENTER) {
xPos = this.x + (this.originalDim.x / 2);
yPos = this.y + (this.originalDim.y / 2);
}
rect(xPos, yPos, xWidth, yHeight);
}
////////////////////////////////////////////
void drawRandomQuad() {
drawRandomQuad(this.shapeDim.x, this.shapeDim.y);
}
void drawRandomQuad(float dimensions) {
drawRandomQuad(dimensions, dimensions);
}
void drawRandomQuad(float xWidth, float yHeight) {
PVector TopLeft = new PVector(this.x, this.y, this.z);
PVector TopRight = new PVector(TopLeft.x + xWidth, TopLeft.y, this.z);
PVector BottomLeft = new PVector(TopLeft.x, TopLeft.y + yHeight, this.z);
PVector BottomRight = new PVector(BottomLeft.x + xWidth, BottomLeft.y, this.z);
float TopPointX = random(TopLeft.x, TopRight.x);
float BottomPointX = random(BottomLeft.x, BottomRight.x);
float LeftPointY = random(TopLeft.y, BottomLeft.y);
float RightPointY = random(TopRight.y, BottomRight.y);
setColors();
strokeWeight(2);
stroke(0);
ellipse(TopLeft.x, TopLeft.y, xWidth, yHeight);
strokeWeight(2);
//fill(110,110,110,123);
beginShape();
vertex(TopPointX, TopLeft.y); //Top
vertex(TopRight.x, RightPointY); //Right
vertex(BottomPointX, BottomLeft.y); //Bottom
vertex(TopLeft.x, LeftPointY); //Left
vertex(TopPointX, TopLeft.y); //Top
endShape();
}
////////////////////////////////////////////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment