Last active
December 13, 2015 20:49
-
-
Save nossidge/4973081 to your computer and use it in GitHub Desktop.
NoisyCrosses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a symmetrical diamond shaped (sideways square) snowflake. | |
// Create 1/8th of the shape and reflect that for the remaining 7/8ths. | |
// Make it a repeatable function with a parameter for the center XY. | |
// New pattern generated on key press. | |
// Animated GIF generated and saved on form unload. | |
// ToDo: Store as class? Then it would be copyable. | |
// Save as animated GIF | |
// Requires gifAnimation Processing Library | |
// http://www.extrapixel.ch/processing/gifAnimation/ | |
import gifAnimation.*; | |
GifMaker gifExport; | |
boolean saveToGif = true; | |
// The time between GIF frames | |
int frameDelay = 1000; | |
// No of cells between the centre and edge DIAGONALLY | |
// It's kind of confusing, but it makes sense in the code | |
// Works nicely between 4 to 6 | |
int triangleRadius = 8; | |
int triangleDiameter = (triangleRadius*4) - 1; | |
// This specifies how much the shape should resemble a "star" | |
// == 1 makes a cross | |
// == 2 makes a square diamond | |
// >= 2 makes a star | |
float starness = 2.7; | |
// Cell will be drawn if the RNG is below 1 | |
// So a value of 3 will draw about 1/3 of the cells | |
float randomUpperLimit = 1.8; | |
// The dimensions of each cell | |
int cellW = 7; | |
int cellH = cellW; | |
// The ideal pixel size for the canvas | |
int yPixelIdeal = 256; | |
int xPixelIdeal = int(yPixelIdeal * 1); | |
// Has to be an odd number to make sure we have a central pixel | |
int xCells = (xPixelIdeal / cellW) + 1; | |
int yCells = (yPixelIdeal / cellH) + 1; | |
// Find central cell | |
int centreCellX = (xCells - 1) / 2; | |
int centreCellY = (yCells - 1) / 2; | |
////////////////////////////////////////////////////////////////////// | |
void setup(){ | |
size(xCells*cellW,yCells*cellH); | |
background(0); | |
if (saveToGif) { | |
gifExport = new GifMaker(this, System.currentTimeMillis() + ".gif"); | |
gifExport.setRepeat(0); | |
} | |
keyPressed(); | |
} | |
////////////////////////////////////////////////////////////////////// | |
void draw() {} | |
void keyPressed(){ | |
background(0); | |
// Draw one in each corner of the canvas | |
newPattern(triangleRadius*2 , triangleRadius*2); | |
newPattern(triangleRadius*2 , yCells-1-triangleRadius*2); | |
newPattern(xCells-1-triangleRadius*2 , triangleRadius*2); | |
newPattern(xCells-1-triangleRadius*2 , yCells-1-triangleRadius*2); | |
// Draw one in the centre | |
newPattern(centreCellX , centreCellY); | |
addToGif(); | |
} | |
void stop() { | |
if (saveToGif) { | |
gifExport.setDelay(frameDelay); | |
gifExport.finish(); | |
} | |
} | |
////////////////////////////////////////////////////////////////////// | |
void addToGif() { | |
if (saveToGif) { | |
gifExport.setDelay(frameDelay); | |
gifExport.addFrame(); | |
} | |
} | |
////////////////////////////////////////////////////////////////////// | |
void newPattern(int centreCellX, int centreCellY) { | |
for(int i=0;i<triangleRadius;i++){ | |
for(int j=0;j<( (triangleRadius*2) - (starness*i) );j++){ | |
//// Colours ///////////////////////////////// | |
// White | |
// fill(255); | |
// Grey | |
fill(255-i*60, 255-i*60, 255-i*60); | |
// Dark red | |
// fill(210-(150/triangleRadius)*i, 20, 0); | |
// Cross red (makes a more pronounced "cross" effect) | |
// fill(220-(220/triangleRadius)*i, 0, 0); | |
// Red cross | |
// fill(255-i*60, 0, 0); | |
// Dark blue | |
// fill(0, 0, 255-(255/triangleRadius)*i ); | |
// Lighter blue | |
// fill(0, 0, random(100,255)); | |
// Blue cross | |
// fill(0, 0, 255-i*60); | |
// Green cross | |
// fill(0, 255-i*60, 0); | |
// Yellow cross | |
// fill(255-i*60, 255-i*60, 0); | |
// Completely random (stained glass window / rag rug effect) | |
// (Warning: kinda lame) | |
// fill(random(255), random(255), random(255)); | |
// fill(random(255), random(255), random(255), 110+(100/triangleRadius)*i); | |
// fill(random(255)-i*60, random(255)-i*60, random(255)-i*60); | |
////////////////////////////////////////////// | |
// Split the square into eighths, and have each square in each eighth behave the same | |
if (random(randomUpperLimit)<1) { | |
rect( (centreCellX+i)*cellW, (centreCellY+j+i)*cellH, cellW, cellH); | |
rect( (centreCellX-i)*cellW, (centreCellY+j+i)*cellH, cellW, cellH); | |
rect( (centreCellX+i)*cellW, (centreCellY-j-i)*cellH, cellW, cellH); | |
rect( (centreCellX-i)*cellW, (centreCellY-j-i)*cellH, cellW, cellH); | |
rect( (centreCellX+j+i)*cellW, (centreCellY+i)*cellH, cellW, cellH); | |
rect( (centreCellX-j-i)*cellW, (centreCellY+i)*cellH, cellW, cellH); | |
rect( (centreCellX+j+i)*cellW, (centreCellY-i)*cellH, cellW, cellH); | |
rect( (centreCellX-j-i)*cellW, (centreCellY-i)*cellH, cellW, cellH); | |
} | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment