Skip to content

Instantly share code, notes, and snippets.

@ale2x72
Last active September 18, 2016 09:15
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 ale2x72/a1b14462922147d9338830e4b553d259 to your computer and use it in GitHub Desktop.
Save ale2x72/a1b14462922147d9338830e4b553d259 to your computer and use it in GitHub Desktop.
weighted division - divides an interval d in n parts l, l1, l2...ln so that l1 = l * f, l2 = l1 *f, etc
/*
weighted division
divides an interval d in n parts l, l1, l2...ln
so that l1 = l * f, l2 = l1 *f, etc
the weighing factor f is mapped to mouseX
code for Processing 3
code © Alessio Erioli - Co-de-iT
*/
int n = 5;
float d = 400;
float f= 1.5;
float r, rp;
float s[];
void setup() {
size(900, 900);
// for single factor method
// r = weightedDiv(d, n, f);
s = weightedDivs(d, n, f);
noFill();
stroke(120);
}
void draw() {
background(255);
f = map(mouseX, 0, width, 0.5,2);
surface.setTitle("proportion factor "+ nf(f, 0,2));
s = weightedDivs(d, n, f);
point(width * .5, height *.5);
for(int i=0; i<s.length; i++){
ellipse(width * .5, height *.5, s[i]*2, s[i]*2);
}
// for single factor method
/*
rp=0;
for (int i=0; i<n+1; i++) {
ellipse(width * .5, height *.5, rp*2, rp*2);
rp+=r*pow(f, i);
}
*/
}
float[] weightedDivs(float d, int n, float f) {
float[] spans = new float[n+1];
float span= 0;
for (int i=0; i<n; i++) {
span += pow(f, i);
}
float fact = d/span;
span = 0;
for (int i=0; i<n+1; i++) {
spans[i]=span;
span+=fact*pow(f, i);
}
return spans;
}
// single factor method
float weightedDiv(float d, int n, float f) {
float span= 0;
for (int i=0; i<n; i++) {
span += pow(f, i);
}
float fact = d/span;
return fact;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment