How to efficiently code interpolation (using controlP5 to change variables)?
in
Contributed Library Questions
•
1 year ago
Hey everyone,
I have a program with lots of variables and I'm using controlP5 to change them. I'd like to introduce interpolation to move smoothly from the old to the new value. Changes are currently instantaneous. To accompany my question I've included three examples below.
I'm not looking for interpolation methods (linear, cosine, sigmoid, exponential) but rather for best practices of handling interpolation within a program. Does anybody have suggestions, experiences or good practices to share with regard to a code-efficient solution?
Thanks!
Example 1 - DIRECT
Example 2 - BASIC INTERPOLATION
Example 3 - MULTI-VARIABLE INTERPOLATION
I have a program with lots of variables and I'm using controlP5 to change them. I'd like to introduce interpolation to move smoothly from the old to the new value. Changes are currently instantaneous. To accompany my question I've included three examples below.
- In essence my current setup without interpolation.
- Basic interpolation, but not linear. It starts with big leaps and goes towards endlessly smaller increments. You could include code to stop interpolating within a certain distance. However both the interpolation and the starting-stopping aren't great with this solution.
- Multi-variable interpolation. This is 'actual' interpolation, in this example linear but easily substituted by other types of interpolation. The starting-stopping is relatively neat, although the correct handling of values at the edges of the range could possibly be improved. But all in all better than in example 2 on both accounts. However the downside is that you need more variables (at least 3 extra vars for every variable that you want to interpolate) and more code for each individual variable.
I'm not looking for interpolation methods (linear, cosine, sigmoid, exponential) but rather for best practices of handling interpolation within a program. Does anybody have suggestions, experiences or good practices to share with regard to a code-efficient solution?
Thanks!
Example 1 - DIRECT
- import controlP5.*;
- ControlP5 cp5;
- int diameter = 250;
- void setup() {
- size(500, 500);
- cp5 = new ControlP5(this);
- cp5.addSlider("diameter").setPosition(50, 50).setRange(0, width);
- smooth();
- noStroke();
- fill(0);
- }
- void draw() {
- background(125);
- ellipse(width/2, height/2, diameter, diameter);
- }
Example 2 - BASIC INTERPOLATION
- import controlP5.*;
- ControlP5 cp5;
- int diameter = 250;
- float iDiameter = diameter;
- void setup() {
- size(500, 500);
- cp5 = new ControlP5(this);
- cp5.addSlider("diameter").setPosition(50, 50).setRange(0, width);
- smooth();
- noStroke();
- fill(0);
- }
- void draw() {
- background(125);
- iDiameter = 0.95 * iDiameter + 0.05 * diameter;
- ellipse(width/2, height/2, iDiameter, iDiameter);
- }
Example 3 - MULTI-VARIABLE INTERPOLATION
- import controlP5.*;
- ControlP5 cp5;
- float oldDiameter, newDiameter, currentDiameter = 250;
- float diameterInterpolation = 1;
- void setup() {
- size(500, 500);
- cp5 = new ControlP5(this);
- cp5.addSlider("newDiameter")
- .setBroadcast(false)
- .setPosition(50, 50)
- .setRange(0, width)
- .setValue(currentDiameter)
- .setLabel("diameter")
- .setBroadcast(true)
- ;
- smooth();
- noStroke();
- fill(0);
- }
- void draw() {
- background(125);
- if (diameterInterpolation < 1) {
- currentDiameter = lerp(oldDiameter, newDiameter, diameterInterpolation);
- diameterInterpolation += 0.05;
- }
- ellipse(width/2, height/2, currentDiameter, currentDiameter);
- }
- void newDiameter(float theValue) {
- oldDiameter = currentDiameter;
- newDiameter = theValue;
- diameterInterpolation = 0;
- }
1