/* mandalas 1.3 copyright 2008 Elizabeth D Zwicky zwicky@otoh.org http://www.otoh.org */ float maxDistance; void setup () { colorMode(HSB, 100); size (600,600); background(0); frameRate(10); if (width < height) { maxDistance = (width/2) * .95; } else { maxDistance = (height/2) * .95; } loop(); rectMode(CENTER); } boolean stopped = false; boolean reInit = false; boolean continuous = true; //void draw () {drawIt();} void draw () { float thisLayer, otherLayer, lastLayer, strokeWeightSet; float hueLayer, satLayer, brightLayer, setAlpha; float longHLayer, shortHLayer; lastLayer = 0; translate(width/2, height/2); if ((layerNum < 0) ) { initVars(); noStroke(); fill (random(0,100), random(0,100), random(0,100)); rect(0,0,width, height); // fill(30, 100, 100); for (int layer=0; layer<= numLayers; layer++){ noFill(); layerNum = 0; } if (layerNum <= numLayers && ! stopped) { thisLayer = layerNum * (1/numLayers); // rotate (((90/nodes)/numLayers) * layerNum); strokeWeightSet = strokeWeightStart + ((strokeWeightGoal - strokeWeightStart) * sin(thisLayer * PI)); strokeWeight(strokeWeightSet); if (strokeWeightSet == 0){ setAlpha = 100; } else { setAlpha = (90/strokeWeightSet) + 10; } if (twoColor) { hueLayer = round(thisLayer/(1/numLayers)) % 2; satLayer = hueLayer; brightLayer = hueLayer; } else { hueLayer = sin(thisLayer*hueCycle); satLayer = sin(thisLayer * satCycle); brightLayer = sin(thisLayer* brightCycle); } stroke((hueStart + ((hueGoal - hueStart) * hueLayer))%100, satStart + ((satGoal - satStart) * satLayer), brightStart + ((brightGoal - brightStart) * brightLayer), setAlpha); otherLayer = ((layerNum * (1/numLayers)) + fillChange)%1; if (twoColor) { hueLayer = (hueLayer + 1) %2; satLayer = hueLayer; brightLayer = hueLayer; } else { hueLayer = sin(otherLayer*hueCycle); satLayer = sin(otherLayer*satCycle); brightLayer = sin(otherLayer*brightCycle); } fill((hueStart + ((hueGoal - hueStart) * hueLayer))%100, satStart + ((satGoal - satStart) * satLayer), brightStart + ((brightGoal - brightStart) * brightLayer), fillAlpha); if (nonLinear) { thisLayer = thisLayer * thisLayer; } if (alterShape){ if ( layerNum%2 == 1) { longHLayer = thisLayer; shortHLayer = lastLayer; } else{ longHLayer = lastLayer; shortHLayer = thisLayer; } } else { longHLayer = shortHLayer = thisLayer; } flowerPolygon( 0, 0, longH +((longHGoal - longH) * longHLayer), shortH + ((shortHGoal - shortH) * shortHLayer), curveH1 + ((curveH1Goal - curveH1) * thisLayer), curveH2 + ((curveH2Goal -curveH2) * thisLayer), curveA1 + ((curveA1Goal - curveA1) * thisLayer), curveA2 + ((curveA2Goal - curveA2) * thisLayer), nodes, flowerMode); lastLayer = thisLayer; layerNum++; } if (layerNum > numLayers && continuous && !stopped) { nextLayer(); } } void nextLayer () { if (reInit) { initGoals(); } layerNum = 0; } float longH, longHGoal, shortH, shortHGoal; float curveH1, curveH1Goal, curveH2, curveH2Goal; float curveA1, curveA1Goal, curveA2, curveA2Goal; float numLayers; float nodes; float [] angleGoals = new float[13]; int saveNum = 1; float hueStart, hueGoal, satStart, satGoal, brightStart, brightGoal; float fillChange, fillAlpha; float hueCycle, brightCycle, satCycle; float longHRot, shortHRot; float frameMod; float layerNum = -1; boolean doTest, simpleCurve, asym, nonLinear, twoColor, colorRGB, alterShape; int flowerMode; float strokeWeightGoal = 0; float strokeWeightStart = 0; void nextDrawMode() { flowerMode = (flowerMode +1 ) %3; } void initStrokeWeight() { if (random(0,2) < 1) { strokeWeightStart = random(0,10); if (strokeWeightStart < 5 && random(0,3) < 2) { strokeWeightGoal = 10; } else if (strokeWeightStart > 5 && random(0,3) < 2) { strokeWeightGoal = 1; } else { strokeWeightGoal = strokeWeightStart; } } else { strokeWeightGoal = 0; strokeWeightStart = 0; } } void initCurveA() { curveH1 = random (50, maxDistance); curveA1 = random (-(360/nodes), 360/nodes); } void initCurveB() { curveH2 = random (0, maxDistance/2); curveA2 = random (-(360/nodes), 360/nodes); } void initNumLayers () { numLayers = round(random(maxDistance/20,maxDistance/nodes)); if (alterShape) { numLayers = round(numLayers/3); } } void initLongH () { longH = random (-maxDistance, maxDistance); } void initShortH () { shortH = random (-maxDistance/2, maxDistance/2); } void initVars () { if (random(0,2) > 1){ nodes = round(random(2,30)); } else { nodes = round(random(2,7)); } simpleCurve = (random(0,1) > .5); asym = (random(0,1) > .5); nonLinear = (random(0,1) > .5); alterShape = (random(0,1) > .5); //alterShape = true; //simpleCurve = true; //asym = false; //nonLinear = true; //nodes = 32; flowerMode = floor(random(0,3)); //flowerMode = 2; initFill(); initLongH(); initShortH(); initCurveA(); initCurveB(); initNumLayers(); angleGoals[0] = 180; angleGoals[1] = -180; angleGoals[2] = 360/nodes; angleGoals[3] = -(360/nodes); angleGoals[4] = 0; angleGoals[5] = random(0, 720/nodes); angleGoals[6] = random(360/nodes, 720/nodes); angleGoals[7] = -(random(360/nodes, 720/nodes)); angleGoals[8] = random(720/nodes, 720/nodes + 360/nodes); angleGoals[9] = -(random(720/nodes, 720/nodes + 360/nodes)); angleGoals[10] = 180/nodes; angleGoals[11] = -180/nodes; initGoals(); initHue(); } void initFill() { fillChange = (random(0,1)); fillAlpha = random(0,25); if (alterShape) { fillAlpha = random(5,30); } } void initHue () { hueStart = random(0,100); hueGoal = random (0,100); if (random(0,2) < 1) { hueCycle = PI; } else { hueCycle = HALF_PI; } } void initGoals () { initLongHGoal(); initShortHGoal(); initSatBright(); initGoalA(); initGoalB(); } void initSatBright() { if (random(0,2) < 1) { brightCycle = PI; } else { brightCycle = HALF_PI; } if (random(0,2) < 1) { satCycle = PI; } else{ satCycle = HALF_PI; } if (random(0,1) < .5) { satStart = 100; } else { satStart = 0; } if (satStart == 100 && random(0,1) < .5) { satGoal = 0; } else { satGoal = 100; } if (random(0,1) < .5) { brightStart = 100; } else { brightStart = 0; } if (brightStart == 100 && random(0,1) < .5) { brightGoal = 0; } else { brightGoal = 100; } } void initGoalA () { if (longHGoal < maxDistance && longHGoal > 0 && random(0,4) < 2) { curveH1Goal = maxDistance; } else if (longHGoal < 0 && longHGoal > -maxDistance && random(0,4) > 2) { curveH1Goal = -maxDistance; } else { curveH1Goal = random(-maxDistance, maxDistance); } curveA1Goal = angleGoals[round(random(0,11))]; } void initGoalB() { if (shortHGoal < maxDistance && shortHGoal > 0 && random(0,4) < 2) { curveH2Goal = maxDistance; } else if (shortHGoal < 0 && shortHGoal > -maxDistance && random(0,4) > 2) { curveH2Goal = -maxDistance; } else { curveH2Goal = random(-maxDistance, maxDistance); } curveA2Goal = angleGoals[round(random(0,11))]; } void initShortHGoal () { if (abs(shortH) > maxDistance/2 && random(0,3) < 2) { shortHGoal = 0; } else if (shortH < maxDistance/2 && shortH > -(maxDistance * .25) && random(0,2) <1) { shortHGoal = -maxDistance; } else if (longH < maxDistance * .25 && longH > -(maxDistance * .5) && random(0,2) < 1) { shortHGoal = maxDistance; } else if (abs(longHGoal) == abs(longH)) { shortHGoal = maxDistance; } else if (random(0,2) < 1) { shortHGoal = shortH; } else { shortHGoal = -shortH; } } void initLongHGoal () { if (abs(longH) > maxDistance/2 && random(0,3) < 2) { longHGoal = 0; } else if (longH < maxDistance/2 && longH > -(maxDistance * .25) && random(0,2) <1) { longHGoal = -maxDistance; } else if (longH < maxDistance * .25 && longH > -(maxDistance * .5) && random(0,2) <1) { longHGoal = maxDistance; } else if (random(0,2) < 1) { longHGoal = longH; } else { longHGoal = -longH; } } void flowerPolygon (float xLoc, float yLoc, float longHyp, float shortHyp, float curveHyp1, float curveHyp2, float curveAng1, float curveAng2, float nSides, int mode) { //90 degree in radians in half pi float baseAng = radians (360/nSides); float halfAng = radians (180/nSides); float thisAng = 0; float thisHalfAng = 0; float nextAng = 0; float thisCurveAng1; float thisCurveAng1r = 0; float thisCurveAng2; float thisCurveAng2r; beginShape(); curveAng1 = radians (curveAng1); curveAng2 = radians (curveAng2); for (float i = 0; i < nSides; i++){ nextAng = (baseAng * (i+1)) + (longHRot * layerNum ) ; thisAng = (baseAng * i) + (longHRot * layerNum); thisHalfAng = (baseAng * i) + halfAng + (shortHRot * layerNum) ; thisCurveAng1 = (baseAng * i) + halfAng - curveAng1 + (longHRot * layerNum); thisCurveAng1r = (baseAng * i) + halfAng + curveAng1 + (longHRot * layerNum) ; thisCurveAng2 = (baseAng * i) + halfAng - curveAng2 + (longHRot * layerNum); thisCurveAng2r = (baseAng * i) + halfAng + curveAng2 + (longHRot * layerNum); if (mode == 2 && ! asym) { curveVertex (xLoc + (longHyp * cos (thisAng )), yLoc + (longHyp * sin(thisAng ))); } else { vertex (xLoc + (longHyp * cos ((baseAng * i) )), yLoc + (longHyp * sin((baseAng * i) ))); } if (mode == 0) { if (simpleCurve) { if (asym) { bezierVertex( xLoc + (curveHyp2 * cos(thisCurveAng2)), yLoc + (curveHyp2 * sin(thisCurveAng2)), xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r)), xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } else { bezierVertex(xLoc + (curveHyp1 * cos(thisCurveAng1)), yLoc + (curveHyp1 * sin(thisCurveAng1)), xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r)), xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } } else { bezierVertex (xLoc + (curveHyp1 * cos(thisCurveAng1)), yLoc + (curveHyp1 * sin(thisCurveAng1)), xLoc + (curveHyp2 * cos(thisCurveAng2)), yLoc + (curveHyp2 * sin(thisCurveAng2)), xLoc + (shortHyp * cos(thisHalfAng)), yLoc + (shortHyp * sin(thisHalfAng))); if (asym) { bezierVertex ( xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r)), xLoc + (curveHyp2 * cos (thisCurveAng2r)), yLoc + (curveHyp2 * sin(thisCurveAng2r)), xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } else { bezierVertex (xLoc + (curveHyp2 * cos (thisCurveAng2r)), yLoc + (curveHyp2 * sin(thisCurveAng2r)), xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r)), xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } } } else if (mode == 1) { vertex( xLoc + (curveHyp1 * cos(thisCurveAng1)), yLoc + (curveHyp1 * sin(thisCurveAng1))); if (!simpleCurve && !asym) { vertex(xLoc + (curveHyp2 * cos(thisCurveAng2)), yLoc + (curveHyp2 * sin(thisCurveAng2))); vertex (xLoc + (shortHyp * cos(thisHalfAng)), yLoc + (shortHyp * sin(thisHalfAng))); } if (!simpleCurve) { vertex (xLoc + (curveHyp2 * cos (thisCurveAng2r)), yLoc + (curveHyp2 * sin(thisCurveAng2r))); } if (! asym) { vertex (xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r))); } if (asym & !simpleCurve) { vertex (xLoc + (curveHyp2 * cos (thisCurveAng2r)), yLoc + (curveHyp2 * sin(thisCurveAng2r))); } vertex( xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } else { curveVertex( xLoc + (curveHyp1 * cos(thisCurveAng1)), yLoc + (curveHyp1 * sin(thisCurveAng1))); if (!simpleCurve) { curveVertex(xLoc + (curveHyp2 * cos(thisCurveAng2)), yLoc + (curveHyp2 * sin(thisCurveAng2))); } if (!simpleCurve || asym) { curveVertex (xLoc + (shortHyp * cos(thisHalfAng)), yLoc + (shortHyp * sin(thisHalfAng))); curveVertex (xLoc + (curveHyp2 * cos (thisCurveAng2r)), yLoc + (curveHyp2 * sin(thisCurveAng2r))); } curveVertex (xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r))); if (!asym) { curveVertex( xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } else { vertex(xLoc + (longHyp * cos(nextAng)), yLoc + (longHyp * sin(nextAng))); } } } if (!asym) { curveVertex (xLoc + (longHyp * cos (nextAng)), yLoc + (longHyp * sin(nextAng))); curveVertex( xLoc + (curveHyp1 * cos( halfAng - curveAng1 )), yLoc + (curveHyp1 * sin(halfAng - curveAng1 ))); if (simpleCurve) { curveVertex (xLoc + (curveHyp1 * cos(thisCurveAng1r)), yLoc + (curveHyp1 * sin(thisCurveAng1r))); } else { curveVertex(xLoc + (curveHyp2 * cos(halfAng - curveAng2 )), yLoc + (curveHyp2 * sin(halfAng - curveAng2 ))); } } endShape(); } void clear () { fill(random(0,100), random(0,100), random(0,100)); noStroke(); rect(0,0,width, height); noFill(); } void mousePressed () { layerNum = -1; } void keyPressed () { if (key == 'z') { initLongHGoal(); } else if (key == 'x') { initShortHGoal(); } else if (key == 'c') { initLongH(); } else if (key == 'v') { initShortH(); } else if (key == 'b') { initGoalA(); initGoalB(); } if (key == 'n'){ initCurveA(); initCurveB(); } else if (key == 'm') { nextDrawMode(); } else if (key == 'a') { continuous = ! continuous; } else if (key == 's') { stopped = !stopped; } else if (key == 'S'){ saveNum++; String fileName = "easymandala"; fileName = fileName.concat(str(saveNum)); fileName = fileName.concat(".jpg"); save (fileName); } else if (key == 'd') { reInit = !reInit; } else if (key == 'f') { nextLayer(); } else if (key == 'g') { clear(); } else if (key == 'h') { layerNum = -1; } else if (key == 'j') { asym = !asym; } else if (key == 'k') { simpleCurve = !simpleCurve; } else if (key == 'l') { if (nodes > 2) { nodes--; } else { nodes = 100; } } else if(key == 'L') { nodes++; } else if (key == 'q') { initHue(); } else if (key == 'w') { initSatBright(); } else if (key == 'e') { initStrokeWeight(); } else if (key == 'r') { twoColor = !twoColor; } else if (key == 't') { if (colorRGB){ colorMode(HSB,100); } else { colorMode(RGB, 100); } colorRGB = ! colorRGB; } else if (key == 'y') { alterShape = !alterShape; } else if (key == 'u') { if (frameRate < .5) { frameMod = .5; } if (frameRate > 10) { frameMod = -.5; } frameRate(frameRate + frameMod); } else if (key == 'i'){ initNumLayers(); } else if (key == 'o') { nonLinear = ! nonLinear; } else if (key == 'p') { initFill(); } }