Interpolation problem - number of points on path falls short

edited January 2018 in Questions about Code

Dear all

I'm trying to improve the code of the XYscope library (to send oscilloscope beam to points on path between vectors, rather than vectors themselves) – this requires lerping from say 5 PVector points to 1024 positions of a wavetable array.. Getting sooo close, but the lerping stops just shy of the last point.

Abstracted example below, 4 separated paths (the z of a PVector is used to separate and move the oscilloscope beam fast to next point). Only the last one will fall shy of reaching the end. Any idea what's wrong?

/*
Attempt to draw better wavetable's for XYscope.
Issue: not reaching last few points of lerp to close final line
*/

ArrayList<PVector> pts = new ArrayList<PVector>();
float waveShapeLength = 1024;

void setup() {
  size(800, 500, P3D);
  background(255);
  noFill();

  // draw initial lines separated by 0 Z
  pts.add(new PVector(50, 50, 0));
  pts.add(new PVector(50, 50, 1));
  pts.add(new PVector(width-50, 50, 1));

  pts.add(new PVector(50, 150, 0));
  pts.add(new PVector(50, 150, 1));
  pts.add(new PVector(width-50, 150, 1));

  pts.add(new PVector(50, 250, 0));
  pts.add(new PVector(50, 250, 1));
  pts.add(new PVector(width-50, 250, 1));

  pts.add(new PVector(150, 350, 0));
  pts.add(new PVector(150, 350, 1));
  pts.add(new PVector(width-150, 350, 1));
}

void draw() {
  background(255);

  // DRAW POINTS
  for (int i=0; i<pts.size(); i++) {
    ellipse(pts.get(i).x, pts.get(i).y, 5, 5);
  }

  // DRAW PATH COMPLETED
  strokeWeight(.25);
  beginShape();
  for (int i=0; i<pts.size(); i++) {
    vertex(pts.get(i).x, pts.get(i).y);
  }
  endShape();

  strokeWeight(1);

  //CALCULATE TOTAL DISTANCE
  int totalDist = 0;
  for (int i=1; i<pts.size(); i++) {
    PVector pv = pts.get(i-1);
    if (pts.get(i).z == 0)
      pv = pts.get(i);
    totalDist+= PVector.dist(pv, pts.get(i));
  }
  //println("totalDist: "+ totalDist); // = 2604

  // CALCULATE LINE DISTANCE PERCENTAGE (= 1.0)
  float tot = 0;
  for (int i=1; i<pts.size(); i++) {
    PVector pv = pts.get(i-1);
    if (pts.get(i).z == 0)
      pv = pts.get(i);
    tot += PVector.dist(pv, pts.get(i))/totalDist;
  }
  //println("total: "+ tot); // = 1.0

  // LERP BETWEEN POINTS
  int curP = 1;
  float curL = 0;
  int c = 0;
  for (int i=0; i < waveShapeLength; i++) {
    PVector cv = pts.get(curP);
    PVector pv = pts.get(curP-1);
    float distPercentage = PVector.dist(pts.get(curP-1), pts.get(curP))/totalDist;
    PVector v = PVector.lerp(pv, cv, curL);

    // PROBLEM REVEALED
    //println(curP +" / "+ curL); // reveals missng steps

    // DRAW LERPED POINTS
    float s = map(curL, 0, 1, 1, 10);
    ellipse(v.x, v.y, s, s);
    c++;

    // REWIND IF z == 0
    if (pts.get(curP).z == 0)
      i--;

    // CALCULATE LERP AMOUNT
    if (curP < pts.size()) {
      curL += 1.0/(distPercentage*(waveShapeLength));
    }

    // NEXT POINT
    if ((curL > 1 || pts.get(curP).z == 0) && curP < pts.size()-1) {
      curP++;
      curL = 0;
    }
  }
}

Answers

Sign In or Register to comment.