Skip to content

Instantly share code, notes, and snippets.

@krclark
Created October 2, 2016 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krclark/a12ec51a920dad93af7c9ffa0eee159d to your computer and use it in GitHub Desktop.
Save krclark/a12ec51a920dad93af7c9ffa0eee159d to your computer and use it in GitHub Desktop.
Mandala Generator
import processing.pdf.*;
int h = 400;
int w = 400;
float a = 0.0;
float r = 90.0;
void setup() {
background(255);
size(400, 400, PDF, "lotus.pdf");
}
int xo = w/2;
int yo = h/2;
void petal(float x, float y, float size, int dir){
if (dir == 1) { //up
bezier(x, y, x-size, y, x-size, y-size, x, y-(3*size));
bezier(x, y, x+size, y, x+size, y-size, x, y-(3*size));
} if (dir == 5) { //down
bezier(x, y, x-size, y, x-size, y+size, x, y+(3*size));
bezier(x, y, x+size, y, x+size, y+size, x, y+(3*size));
} if (dir == 7) { //left
bezier(x, y, x, y-size, x-size, y-size, x-(3*size), y);
bezier(x, y, x, y+size, x-size, y+size, x-(3*size), y);
} if (dir == 3) { //right
bezier(x, y, x, y-size, x+size, y-size, x+(3*size), y);
bezier(x, y, x, y+size, x+size, y+size, x+(3*size), y);
} if (dir == 2) {
bezier(x, y, x-(size*(sqrt(2)/2)), y-(size*(sqrt(2)/2)),x, y-size, x+(3*size*sqrt(2)/2), y-(3*size*sqrt(2)/2));
bezier(x, y, x+(size*(sqrt(2)/2)), y+(size*(sqrt(2)/2)),x+size, y, x+(3*size*sqrt(2)/2), y-(3*size*sqrt(2)/2));
} if (dir == 4) {
bezier(x, y, x+(size*(sqrt(2)/2)), y-(size*(sqrt(2)/2)),x+size, y, x+(3*size*sqrt(2)/2), y+(3*size*sqrt(2)/2));
bezier(x, y, x-(size*(sqrt(2)/2)), y+(size*(sqrt(2)/2)),x, y+size, x+(3*size*sqrt(2)/2), y+(3*size*sqrt(2)/2));
} if (dir == 6) {
bezier(x, y, x+(size*(sqrt(2)/2)), y+(size*(sqrt(2)/2)),x, y+size, x-(3*size*sqrt(2)/2), y+(3*size*sqrt(2)/2));
bezier(x, y, x-(size*(sqrt(2)/2)), y-(size*(sqrt(2)/2)),x-size, y, x-(3*size*sqrt(2)/2), y+(3*size*sqrt(2)/2));
} if (dir == 8) {
bezier(x, y, x-(size*(sqrt(2)/2)), y+(size*(sqrt(2)/2)),x-size, y, x-(3*size*sqrt(2)/2), y-(3*size*sqrt(2)/2));
bezier(x, y, x+(size*(sqrt(2)/2)), y-(size*(sqrt(2)/2)),x, y-size, x-(3*size*sqrt(2)/2), y-(3*size*sqrt(2)/2));
}
//NE
//SE
//SW
//NW
}
void octagon(int xo, int yo, int r) {
line(xo, yo-r, xo+sqrt(2)*(r/2), yo-sqrt(2)*(r/2));
line(xo+sqrt(2)*(r/2), yo-sqrt(2)*(r/2), xo+r, yo);
line(xo+r, yo, xo+sqrt(2)*(r/2), yo+sqrt(2)*(r/2));
line(xo+sqrt(2)*(r/2), yo+sqrt(2)*(r/2), xo, yo+r);
line(xo, yo+r, xo-sqrt(2)*(r/2), yo+sqrt(2)*(r/2));
line(xo-sqrt(2)*(r/2), yo+sqrt(2)*(r/2), xo-r, yo);
line(xo-r, yo, xo-sqrt(2)*(r/2), yo-sqrt(2)*(r/2));
line(xo-sqrt(2)*(r/2), yo-sqrt(2)*(r/2), xo, yo-r);
}
void petal_oct(int xo, int yo, float r, int size, float fact) {
petal(xo, yo-r, size*fact, 1);
petal(xo+sqrt(2)*(r/2), yo-sqrt(2)*(r/2), size, 2);
petal(xo+r, yo, size*fact, 3);
petal(xo+sqrt(2)*(r/2), yo+sqrt(2)*(r/2), size, 4);
petal(xo, yo+r, size*fact, 5);
petal(xo-sqrt(2)*(r/2), yo+sqrt(2)*(r/2), size, 6);
petal(xo-r, yo, size*fact, 7);
petal(xo-sqrt(2)*(r/2), yo-sqrt(2)*(r/2), size, 8);
}
void hexagon(int xo, int yo, int r) {
line(xo, yo-r, xo+sqrt(3)*(r/2), yo-r/2);
line(xo+sqrt(3)*(r/2), yo-r/2, xo+sqrt(3)*(r/2), yo+(r/2));
line(xo+sqrt(3)*(r/2), yo+(r/2), xo, yo+r);
line(xo, yo+r, xo-sqrt(3)*(r/2), yo+(r/2));
line(xo-sqrt(3)*(r/2), yo+(r/2), xo-sqrt(3)*(r/2), yo-(r/2));
line(xo-sqrt(3)*(r/2), yo-(r/2), xo, yo-r);
}
void mooncake(int xo, int yo, int r, float th) {
th = th*(r/40);
float le = th/2;
float x1 = xo;
float y1 = yo-r;
float x2 = xo+sqrt(3)*(r/2);
float y2 = yo-r/2;
float x3 = xo+sqrt(3)*(r/2);
float y3 = yo+(r/2);
float x4 = xo;
float y4 = yo+r;
float x5 = xo-sqrt(3)*(r/2);
float y5 = yo+(r/2);
float x6 = xo-sqrt(3)*(r/2);
float y6 = yo-(r/2);
bezier(x1, y1, x1+le, y1-le, (x1+x2)/2+le, (y1+y2)/2-le, (x1+x2)/2, (y1+y2)/2);
bezier((x1+x2)/2, (y1+y2)/2, (x1+x2)/2+le, (y1+y2)/2-le, x2+le, y2-le, x2, y2);
bezier(x2, y2, x2+le, y2, (x2+x3)/2+le, (y3+y2)/2, (x3+x2)/2, (y3+y2)/2);
bezier((x3+x2)/2, (y3+y2)/2, (x3+x2)/2+le, (y3+y2)/2, x3+le, y3, x3, y3);
bezier(x3, y3, x3+le, y3+le, (x3+x4)/2+le, (y3+y4)/2+le, (x3+x4)/2, (y3+y4)/2);
bezier((x3+x4)/2, (y3+y4)/2, (x3+x4)/2+le, (y3+y4)/2+le, x4+le, y4+le, x4, y4);
bezier(x4, y4, x4-le, y4+le, (x5+x4)/2-le, (y5+y4)/2+le, (x5+x4)/2, (y5+y4)/2);
bezier((x5+x4)/2, (y5+y4)/2, (x5+x4)/2-le, (y5+y4)/2+le, x5-le, y5+le, x5, y5);
bezier(x5, y5, x5-le, y5, (x5+x6)/2-le, (y6+y5)/2, (x5+x6)/2, (y5+y6)/2);
bezier((x5+x6)/2, (y5+y6)/2, (x5+x6)/2-le, (y5+y6)/2, x6-le, y6, x6, y6);
bezier(x6, y6, x6-le, y6-le, (x6+x1)/2-le, (y6+y1)/2-le, (x6+x1)/2, (y6+y1)/2);
bezier((x6+x1)/2, (y6+y1)/2, (x6+x1)/2-le, (y6+y1)/2-le, x1-le, y1-le, x1, y1);
//bezier();
}
void lotus(int xo, int yo, int r, float th) {
th = (th +r/8);
float le = th/2;
float x1 = xo;
float y1 = yo-r;
float x2 = xo+sqrt(3)*(r/2);
float y2 = yo-r/2;
float x3 = xo+sqrt(3)*(r/2);
float y3 = yo+(r/2);
float x4 = xo;
float y4 = yo+r;
float x5 = xo-sqrt(3)*(r/2);
float y5 = yo+(r/2);
float x6 = xo-sqrt(3)*(r/2);
float y6 = yo-(r/2);
bezier(x1, y1, x1+le, y1-le, (x1+x2)/2, (y1+y2)/2, (x1+x2)/2+th, (y1+y2)/2-th);
bezier((x1+x2)/2+th, (y1+y2)/2-th, (x1+x2)/2, (y1+y2)/2,x2+le, y2-le, x2, y2);
bezier(x2, y2, x2+le, y2, x2, (y2+y3)/2, x2+th, (y2+y3)/2);
bezier(x2+th, (y2+y3)/2, x2, (y2+y3)/2, x3+le, y3, x3, y3);
bezier(x3, y3, x3+le, y3+le, (x3+x4)/2, (y3+y4)/2, (x3+x4)/2+th, (y3+y4)/2+th);
bezier((x3+x4)/2+th, (y3+y4)/2+th, (x3+x4)/2, (y3+y4)/2,x4+le, y4+le, x4, y4);
bezier(x4, y4, x4-le, y4+le, (x4+x5)/2, (y4+y5)/2, (x4+x5)/2-th, (y4+y5)/2+th);
bezier((x4+x5)/2-th, (y4+y5)/2+th, (x4+x5)/2, (y4+y5)/2, x5-le, y5+le, x5, y5);
bezier(x5, y5, x5-le, y5, (x5+x6)/2, (y6+y5)/2, (x6+x5)/2-th, (y6+y5)/2);
bezier((x6+x5)/2-th, (y6+y5)/2, (x5+x6)/2, (y6+y5)/2, x6-le, y6, x6, y6);
bezier(x6, y6, x6-le, y6-le, (x1+x6)/2, (y1+y6)/2, (x6+x1)/2-th, (y6+y1)/2-th);
bezier((x6+x1)/2-th, (y6+y1)/2-th, (x1+x6)/2, (y1+y6)/2, x1-le, y1-le, x1, y1);
}
void pentagon(int xo, int yo, int r) {
stroke(0);
line(xo, yo-r, xo-(sin(36)*r), yo+(cos(36)*r));
line(xo-(sin(36)*r), yo+(cos(36)*r), xo+(cos(18)*r), yo-(sin(18)*r));
line(xo+(cos(18)*r), yo-(sin(18)*r), xo - (cos(18)*r), yo-(sin(18)*r));
line(xo - (cos(18)*r), yo-(sin(18)*r), xo+(sin(36)*r), yo+(cos(36)*r));
line(xo+(sin(36)*r), yo+(cos(36)*r), xo, yo-r);
}
void circle_hex(int xo, int yo, int r) {
ellipse(xo, yo-r, 10, 10);
ellipse(xo+sqrt(3)*(r/2), yo-r/2, 10, 10);
ellipse(xo, yo+r, 10, 10);
ellipse(xo+sqrt(3)*(r/2), yo+(r/2), 10, 10);
ellipse(xo-sqrt(3)*(r/2), yo+(r/2), 10, 10);
ellipse(xo-sqrt(3)*(r/2), yo-(r/2), 10, 10);
}
void trip_circle_hex(int xo, int yo, int r) {
int f = 5;
ellipse(xo-f, yo-r, 4, 4);
ellipse(xo+f, yo-r, 4, 4);
ellipse(xo, yo-r-f, 4, 4);
ellipse(xo+sqrt(3)*(r/2)-f, yo-r/2, 4, 4);
ellipse(xo+sqrt(3)*(r/2)+f, yo-r/2, 4, 4);
ellipse(xo+sqrt(3)*(r/2), yo-r/2-f, 4, 4);
ellipse(xo-f, yo+r, 4, 4);
ellipse(xo+f, yo+r, 4, 4);
ellipse(xo, yo+r-f, 4, 4);
ellipse(xo+sqrt(3)*(r/2)-f, yo+(r/2), 4, 4);
ellipse(xo+sqrt(3)*(r/2)+f, yo+(r/2), 4, 4);
ellipse(xo+sqrt(3)*(r/2), yo+(r/2)-f, 4, 4);
ellipse(xo-sqrt(3)*(r/2)-f, yo+(r/2), 4, 4);
ellipse(xo-sqrt(3)*(r/2)+f, yo+(r/2), 4, 4);
ellipse(xo-sqrt(3)*(r/2), yo+(r/2)-f, 4, 4);
ellipse(xo-sqrt(3)*(r/2)-f, yo-(r/2), 4, 4);
ellipse(xo-sqrt(3)*(r/2)+f, yo-(r/2), 4, 4);
ellipse(xo-sqrt(3)*(r/2), yo-(r/2)-f, 4, 4);
}
void drawdisjoint(int edges, int r, int choice) {
//3 choices
//edges is going to be between 20 and 1 inclusively
//draw *edges* number of triple petals, swirls, or dots
if (choice%3 == 0) petal_oct(xo, yo, r - float(r/3), 7, 1);
if (choice%3 == 1) circle_hex(xo, yo, r+edges);
if (choice%3 == 2) trip_circle_hex(xo, yo, r+edges);
//?
}
void drawsolid(int edges, int r, int choice){
//9 choices
//draw a polygon, circle, flower or spike with *edges* number of vertices
edges = 0;
r = r/2;
if (choice%4 == 1){
lotus(xo,yo, r,10);
}
if (choice%4 == 2){
ellipse(xo, yo, r, r);
}
if (choice%4 == 3) {
quad(xo-r, yo-r, xo+r, yo-r, xo+r, yo+r, xo-r, yo+r); //square
}
if (choice%4 == 0){
mooncake(xo, yo, r, 10);
}
//choice = int(random(1, 7));
if (choice == 1) pentagon(xo, yo, r);
if (choice == 2) octagon(xo, yo, r);
if (choice == 3)quad(xo, yo-r, xo+r, yo, xo, yo+r, xo-r, yo); //diamond
if (choice == 4) hexagon(xo, yo, r);
else drawdisjoint(edges, r, choice);
}
void draw() {
// Draw something good here
for (int i = 20; i > 0; i-=2) {
int r = 20*i;
//20*i = radius. Choose between random options
int c = int(random(0, 9));
drawsolid(i, r, c);
if (i%10 == 0) drawdisjoint(i, r, c);//odd
//even
}
// Exit the program
println("Finished.");
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment