Created
March 3, 2017 02:31
-
-
Save XiaohanYa/c613a3549bf13ae5164ba6ed4e68bb96 to your computer and use it in GitHub Desktop.
InteractionLab
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
import ddf.minim.*; | |
Minim minim; | |
AudioPlayer groove; | |
PImage img; | |
PImage img1; | |
PImage img2; | |
PImage img3; | |
PImage img4; | |
float Rmax = 4; | |
float gap = 10; | |
float dx = (2*Rmax+gap)*cos(PI/6); | |
float dy = (2*Rmax+gap)/2; | |
int imageMode = 0; | |
void setup() { | |
size(600, 600); | |
smooth(8); | |
colorMode(RGB); | |
background(255); | |
minim = new Minim(this); | |
groove = minim.loadFile("groove.mp3", 1024); | |
groove.loop(); | |
textSize(50); | |
fill(0); | |
text("HELLO ^~^", width/2-100, height/2-100); | |
//fill(random(255), random(255), random(255)); | |
text("Welcome to the World of", width/2-300, height/2-20); | |
textSize(100); | |
fill(255, random(50), random(50),random(100,255)); | |
text("Flowers", width/2-150, height/2+100); | |
textAlign(CENTER, BOTTOM); | |
noStroke(); | |
fill(255); | |
textSize(30); | |
fill(0, 100); | |
text("Click ENTER to Move On", width/2+40, height); | |
textAlign(CENTER, BOTTOM); | |
img = loadImage("3547c8ea15ce36d31943a98e3af33a87e850b13f.jpg"); | |
img1 = loadImage("5bd8c75c10385343510ff8849313b07ec8808841.jpg"); | |
img2 = loadImage("3db1faf2b21193131d83801565380cd793238d41.jpg"); | |
img3= loadImage("WechatIMG21.jpeg"); | |
img4= loadImage("WechatIMG22.jpeg"); | |
} | |
void draw() { | |
keyPressed(); | |
if (keyCode==ENTER) { | |
background(255); | |
stroke(random(255),random(255),random(255)); | |
strokeWeight(5); | |
noFill(); | |
rect(0,0,width,height); | |
delay(100); | |
image(img3, 0, 20, 600, 272); | |
textSize(30); | |
fill(random(255),random(255),random(255)); | |
text("To Choose Your Photo:", width/2, height/2+40); | |
text("Press '1', '2', '3' ", width/2, height/2+80); | |
text("To move on:", width/2, height/2+120); | |
text("Press 'Right' ", width/2, height/2+160); | |
textSize(20); | |
fill(0); | |
text("1", 20, 45); | |
textSize(20); | |
fill(0); | |
text("2", 220, 45); | |
textSize(20); | |
fill(0); | |
text("3", 420, 45); | |
} | |
if (keyCode==RIGHT){ | |
background(255); | |
stroke(random(255),random(255),random(255)); | |
strokeWeight(5); | |
noFill(); | |
rect(0,0,width,height); | |
delay(100); | |
image(img4, 0, 20, 600, 272); | |
textSize(30); | |
fill(random(255),random(255),random(255)); | |
text("To Choose Your Flower Brush:", width/2, height/2+40); | |
text("Press 'd', 'r', 'm','w' ", width/2, height/2+80); | |
text("To return:", width/2, height/2+120); | |
text("Press 'Enter' ", width/2, height/2+160); | |
textSize(20); | |
fill(0); | |
text("dandelian", 50, 45); | |
textSize(20); | |
fill(0); | |
text("rose", 180, 45); | |
textSize(20); | |
fill(0); | |
text("mample leaf", 360, 45); | |
textSize(20); | |
fill(255); | |
text("wild flower", 510, 45); | |
} | |
if (key==' '){ | |
background(255); | |
mapleLeaf(random(300),random(300)); | |
dandelian(random(300),random(300),random(300)); | |
Rose(random(300),random(300),random(300)); | |
wildFlower(random(300),random(300),random(300)); | |
textSize(20); | |
fill(0); | |
text("Press 'esc' to Get Out of This Magic World", width/2, height/2); | |
} | |
if(keyCode==ESC){ | |
stop(); | |
} | |
} | |
void keyPressed() { | |
if (key == '3') { | |
imageMode = 3; | |
} else if (key == '1') { | |
imageMode = 1; | |
} else if (key == '2') { | |
imageMode = 2; | |
} | |
if (imageMode == 3 && key == 'd') { | |
image(img, 0, 0, width, height); | |
pixelsToPatterns(img, 0); | |
} else if (imageMode == 3 && key == 'm') { | |
image(img, 0, 0, width, height); | |
pixelsToPatterns(img, 1); | |
} else if (imageMode == 3 && key == 'r') { | |
image(img, 0, 0, width, height); | |
pixelsToPatterns(img, 2); | |
} else if (imageMode == 3 && key == 'w') { | |
image(img, 0, 0, width, height); | |
pixelsToPatterns(img, 3); | |
} else if (imageMode == 1 && key == 'd') { | |
image(img1, 0, 0, width, height); | |
pixelsToPatterns(img1, 0); | |
} else if (imageMode == 1 && key == 'm') { | |
image(img1, 0, 0, width, height); | |
pixelsToPatterns(img1, 1); | |
} else if (imageMode == 1 && key == 'r') { | |
image(img1, 0, 0, width, height); | |
pixelsToPatterns(img1, 2); | |
} else if (imageMode == 1 && key == 'w') { | |
image(img1, 0, 0, width, height); | |
pixelsToPatterns(img1, 3); | |
} else if (imageMode == 2 && key == 'd') { | |
image(img2, 0, 0, width, height); | |
pixelsToPatterns(img2, 0); | |
} else if (imageMode == 2 && key == 'm') { | |
image(img2, 0, 0, width, height); | |
pixelsToPatterns(img2, 1); | |
} else if (imageMode == 2 && key == 'r') { | |
image(img2, 0, 0, width, height); | |
pixelsToPatterns(img2, 2); | |
} else if (imageMode == 2 && key == 'w') { | |
image(img2, 0, 0, width, height); | |
pixelsToPatterns(img2, 3); | |
} | |
stroke(random(255),random(255),random(255)); | |
strokeWeight(5); | |
noFill(); | |
rect(0,0,width,height); | |
delay(100); | |
} | |
// user-defined function | |
void dandelian(float tempX, float tempY, float tempSr) { | |
pushMatrix(); | |
translate(tempX, tempY); | |
rotate(frameCount*0.05); | |
for (float i=0; i<=360; i++) { | |
rotate(tempSr*cos(i*PI) +1); | |
strokeWeight(random(1)); | |
bezier(0, 0, | |
random(10), random(10), | |
random(50), random(50), | |
0, 0); | |
} | |
popMatrix(); | |
} | |
// user-defined function | |
void mapleLeaf (float x, float y) { | |
for (int angle=0; angle<360; angle+=30) { | |
pushMatrix(); | |
translate(x, y); | |
rotate(radians(angle)); | |
triangle(0, 0, 10, random(20), random(40), 20); | |
popMatrix(); | |
} | |
} | |
void pixelsToPatterns(PImage tempImg, int mode) { | |
tempImg.loadPixels(); | |
int x, y; | |
color c; | |
float r; | |
for (int xi=0; xi<=width/dx; xi++) { | |
for (int yi=0; yi<=height/dy; yi++) { | |
x = int(xi*dx); | |
y = int(yi*dy); | |
if (int(xi+yi)%2!=0) { | |
int loc = x+y*tempImg.width; | |
loc = constrain(loc, 0, tempImg.pixels.length-1); | |
c = convolution(x, y, 3, tempImg); | |
if (brightness(c)>200) | |
r = map(brightness(c), 200, 255, Rmax, 0); | |
else | |
r = Rmax; | |
drawPatterns(x, y, r, c, mode); | |
} | |
} | |
} | |
} | |
void drawPatterns(float x, float y, float r, color c, int mode) { | |
float Sr = r/cos(PI/6); | |
pushMatrix(); | |
//scale(0.8); | |
translate(x, y); | |
if (mode==0){ | |
stroke(c, 50); // fill(color, alpha); | |
noFill(); | |
dandelian(x, y, Sr); | |
} | |
if (mode==1){ | |
noStroke(); | |
fill(c); | |
mapleLeaf(x, y); | |
} | |
if (mode==2){ | |
noStroke(); | |
fill(c); | |
Rose(x, y, Sr); | |
} | |
if (mode==3){ | |
noStroke(); | |
fill(c); | |
wildFlower(x, y, c); | |
} | |
popMatrix(); | |
} | |
color convolution(int x, int y, int matrixsize, PImage img) { | |
float rtotal = 0.0; | |
float gtotal = 0.0; | |
float btotal = 0.0; | |
int offset = matrixsize; | |
// Loop through convolution matrix | |
for (int i = 0; i < matrixsize; i++) { | |
for (int j= 0; j < matrixsize; j++) { | |
// What pixel are we testing | |
int xloc = x+i-offset; | |
int yloc = y+j-offset; | |
int loc = xloc + img.width*yloc; | |
// Make sure we have not walked off the edge of the pixel array | |
loc = constrain(loc, 0, img.pixels.length-1); | |
// Calculate the convolution | |
// We sum all the neighboring pixels multiplied by the values in the convolution matrix. | |
rtotal += (red(img.pixels[loc]) / pow(matrixsize, 2)); | |
gtotal += (green(img.pixels[loc]) / pow(matrixsize, 2)); | |
btotal += (blue(img.pixels[loc]) / pow(matrixsize, 2)); | |
} | |
} | |
// Make sure RGB is within range | |
rtotal = constrain(rtotal, 0, 255); | |
gtotal = constrain(gtotal, 0, 255); | |
btotal = constrain(btotal, 0, 255); | |
// Return the resulting color | |
return color(rtotal, gtotal, btotal); | |
} | |
void Rose(float x, float y, float Sr){ | |
for (float angle=0; angle<360; angle+=29) { | |
pushMatrix(); | |
translate(x, y); | |
rotate(radians(angle)); | |
scale(random(5)); | |
beginShape(); | |
vertex(Sr, 0); | |
vertex(Sr*cos(PI/3), Sr*sin(PI/3)); | |
vertex(Sr*cos(PI/3*2), Sr*sin(PI/3*2)); | |
vertex(Sr*cos(PI), Sr*sin(PI)); | |
vertex(Sr*cos(2*PI/7), Sr*sin(2*PI/5)); | |
vertex(Sr*cos(PI/9), Sr*sin(PI/11)); | |
endShape(CLOSE); | |
popMatrix(); | |
} | |
} | |
void wildFlower(float x, float y, float c) { | |
for (float angle=0; angle<360; angle+=27) { | |
pushMatrix(); | |
translate(x, y); | |
rotate(radians(angle*2)); | |
//scale(random(2)); | |
stroke(c,10); | |
line(0, 0, 10, 5); | |
ellipse(10, 5, random(15), 10); | |
popMatrix(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment