We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › efficiency / streamlining
Page Index Toggle Pages: 1
efficiency / streamlining (Read 450 times)
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);

}
Re: efficiency / streamlining
Reply #1 - Jan 6th, 2008, 12:11pm
 
Learn about "arrays". The following code can be turned into two arrays...

float x0 = 0;
float y0 = 0;
float x1 = 0;
float y1 = 0;
float x2 = 0;
float y2 = 0;
float x3 = 0;
float y3 = 0;

That look like this...


float[] x = new float[4];
float[] y = new float[4];


And then you use them like this...


for (int counter = 0; counter < 4; counter++) {
  x[counter] += (targetX - x0) * easing;
  y[counter] += (targetY - y0) * easing;
}


start here

Once you understand this, learn about a 2D array and then about objects and arrays of them.
Re: efficiency / streamlining
Reply #2 - Jan 6th, 2008, 2:21pm
 
many thanks.
Page Index Toggle Pages: 1