poportis
YaBB Newbies
Offline
Posts: 2
efficiency / streamlining
Jan 6th , 2008, 11:51am
Hello. I am wondering if there is a more efficient / elegant way of doing this (see code at the bottom of this post). there are four dots following the mouse coordinates, but I'd like to have n dots approaching the mouse and be able to specify the number of dots in a quicker + simpler way than having to add this to the code whenever I want to add or remove dots: float xn = 0; float yn = 0; xn += (targetX - x0) * easing; yn += (targetY - y0) * easing; ellipse(xn += sin(random(-1.0, 1.0))*random(15), yn += sin(random(-1.0, 1.0))*random(15), 10, 10); Is it possible to encapsulate this in a for loop so that I can have to loop create n float variables, ellipses and so on... something a little like this (I know this doesn't do the job but hopefully shows what I'm after): for (int i=0; i<20; i++) { float x+i = 0; float y+i = 0; x+i += (targetX - x+i) * easing; y+i += (targetY - y+i) * easing; ellipse(x+i += sin(random(-1.0, 1.0))*random(15), y+i += sin(random(-1.0, 1.0))*random(15), 10, 10); } That way there'd be much less code for me to play around with. I've had a go at this but have gotten my self into grotesque mind loops trying to get the code to create variables for me... cheers. //adapted from the handbook: float easing = 0.03; float x0 = 0; float y0 = 0; float x1 = 0; float y1 = 0; float x2 = 0; float y2 = 0; float x3 = 0; float y3 = 0; void setup() { size(500, 500); smooth(); noStroke(); } void draw() { background(255); float targetX = mouseX; float targetY = mouseY; x0 += (targetX - x0) * easing; y0 += (targetY - y0) * easing; x1 += (targetX - x1) * easing; y1 += (targetY - y1) * easing; x2 += (targetX - x2) * easing; y2 += (targetY - y2) * easing; x3 += (targetX - x3) * easing; y3 += (targetY - y3) * easing; fill(0); ellipse(targetX, targetY, 20, 20); fill(215, 15, 56); ellipse(x0 += sin(random(-1.0, 1.0))*random(15), y0 += sin(random(-1.0, 1.0))*random(15), 10, 10); ellipse(x1 += sin(random(-1.0, 1.0))*random(15), y1 += sin(random(-1.0, 1.0))*random(15), 10, 10); ellipse(x2 += sin(random(-1.0, 1.0))*random(15), y2 += sin(random(-1.0, 1.0))*random(15), 10, 10); ellipse(x3 += sin(random(-1.0, 1.0))*random(15), y3 += sin(random(-1.0, 1.0))*random(15), 10, 10); }