Scrolling shape not closing

Hi @eltrem,

Sorry, I’m quite busy at the moment, but I’ve managed to find a few minutes to review your code. I’m doubtful that it will work the way you intended, as it leads to inconsistencies with regards to the shape. Additionally, the unwanted linking of a shape to another shape depends on your buffer and how it is handled, which could cause further issues.

Unfortunately, I won’t be able to fix your code to make it work the way you want it to. However, I’ve created a quick example that demonstrates how it can be done. It may not be the most efficient method, but due to lack of time it is done quickly.

Cheers
— mnse


ArrayList<Float> ampHistory;
ArrayList<Float> yHistory;
ArrayList<ArrayList<PVector>> allShapes;

void setup() {
  size(600, 400);
  allShapes = new ArrayList<>();
  ampHistory=new ArrayList<>();
  yHistory=new ArrayList<>();
  for (int i = 0; i < width; i++) {
    ampHistory.add(0.);
    yHistory.add(0.);
  }

  strokeWeight(2);
  fill(255, 0, 0);
  stroke(0);
}

void draw() {
  background(255);
  // update the latest amplitude and y values here
  float latestAmp = map(mouseX, 0, width, 0, 255); // replace this with your actual amplitude data
  float latestY = map(mouseY, 0, height, 0, height); // replace this with your actual y data

  // add the latest amplitude and y values to the history
  ampHistory.add(latestAmp);
  yHistory.add(latestY);
  ampHistory.remove(0);
  yHistory.remove(0);

  allShapes.clear();
  ArrayList<PVector> shape = null;
  for (int ampIdx=ampHistory.size()-1; ampIdx >= 0; ampIdx--) {
    float amp = ampHistory.get(ampIdx);
    float y = yHistory.get(ampIdx);
    if (amp > 1 && ampIdx > 0) {
      if (shape == null) {
        shape=new ArrayList<>();
      }
      shape.add(new PVector(ampIdx, y, amp));
    } else {
      if (shape != null) {
        allShapes.add(shape);
        shape=null;
      }
    }
  }

  for (ArrayList<PVector> s : allShapes) {
    beginShape();
    for (int i = 0; i < s.size(); i++) {
      PVector p = s.get(i);
      curveVertex(p.x, p.y-p.z/2);
    }
    for (int i = s.size()-1; i >= 0; i--) {
      PVector p = s.get(i);
      curveVertex(p.x, p.y+p.z/2);
    }
    endShape(CLOSE);
  }
}

example:
ezgif.com-resize

1 Like