Name

lerp()

Description

Calculates a number between two numbers at a specific increment. The amt parameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc. The lerp function is convenient for creating motion along a straight path and for drawing dotted lines.

Examples

  • size(400, 400);
    float a = 80;
    float b = 320;
    float c = lerp(a, b, .8);
    float d = lerp(a, b, .20);
    float e = lerp(a, b, .32);
    beginShape(POINTS);
    vertex(a, 200);
    vertex(b, 200);
    vertex(c, 200);
    vertex(d, 200);
    vertex(e, 200);
    endShape();
    Image output for example 1
  • size(400, 400);
    int x1 = 60;
    int y1 = 40;
    int x2 = 320;
    int y2 = 360;
    line(x1, y1, x2, y2);
    for (int i = 0; i <= 40; i++) {
      float x = lerp(x1, x2, i/40.0) + 40;
      float y = lerp(y1, y2, i/40.0);
      point(x, y);
    }
    Image output for example 2

Syntax

  • lerp(start, stop, amt)

Parameters

  • start(float)first value
  • stop(float)second value
  • amt(float)float between 0.0 and 1.0

Return

  • float
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.