int NUM_BEANS = 12; Bean[] beansArr; void setup(){ size(600, 600); background(255); strokeWeight(random(0.8,1.5)); stroke(0,50); frameRate(20); restart(); } void restart() { beansArr = new Bean[NUM_BEANS]; for (int i = 0; i < NUM_BEANS; i++) { beansArr[i] = new Bean(i); } for (int i = 0; i < NUM_BEANS; i++) { Bean thisBean = beansArr[i]; int otherBeanIndex = int(random(NUM_BEANS)); while(otherBeanIndex == i) { otherBeanIndex = int(random(NUM_BEANS)); } Bean otherBean = beansArr[otherBeanIndex]; thisBean.connect(otherBean); } } void draw() { background(255); for (int i = 0; i < NUM_BEANS; i++) { Bean thisBean = beansArr[i]; thisBean.display(); } } void mouseMoved() { for (int i = 0; i < NUM_BEANS; i++) { Bean thisBean = beansArr[i]; thisBean.amIUnder(mouseX, mouseY); } } void mousePressed() { restart(); } class Bean { float xpos; float ypos; int beanrad; color glow; boolean targetBeanSet = false; Bean targetBean; PVector[] linePoints; int id; boolean imUnder = false; Bean(int i) { id = i; beanrad = 20; glow = color(random(100),random(200)); xpos = random(beanrad*2, width-beanrad*2); ypos = random(beanrad*2, height-beanrad*2); } void amIUnder(float mX, float mY) { imUnder = false; if (dist(mX, mY, xpos, ypos) < (beanrad)) { imUnder = true; println("bean " + id); } } void display() { smooth(12); if (imUnder) { fill(0); } else { fill (glow); } ellipseMode(RADIUS); ellipse(xpos, ypos,beanrad,beanrad); if (targetBeanSet) { float lastX = 0; float lastY = 0; for (int i=0; i < linePoints.length; i++) { ellipse(linePoints[i].x,linePoints[i].y, 3,3); if (i > 0) { line(linePoints[i].x,linePoints[i].y, lastX, lastY); } lastX = linePoints[i].x; lastY = linePoints[i].y; } } //line(xpos, ypos, targetBean.xpos, targetBean.ypos); } void connect(Bean otherBean) { targetBeanSet = true; targetBean = otherBean; int i = 0; float p = dist(xpos,ypos,targetBean.xpos,targetBean.ypos)/10.0; println(p); linePoints = new PVector[ceil(p)]; for (i=0; i < p; i++) { float x = lerp(xpos, targetBean.xpos, i/p); float y = lerp(ypos, targetBean.ypos, i/p); linePoints[i] = new PVector(x, y); // int lastx = i-1 // ell.ipse(linePoints[i].x,linePoints[i].y, 4,4); } } }