curve within borders || equalizer
in
Programming Questions
•
1 year ago
Hey guys,
I'm working on a problem and I just wanted to ask here, if maybe somebody got a clever idea solving it.
I'm drawing a curve trough points, that I have placed each one by clicking somewhere on the sketch. If I click on an existing point, I delete it. The ArrayList of points is sorted by the x-positions eveytime a new point appears. So always the curve is drawn from left to right trough the points. We could call that thing a graphic equalizer, right?
So I use that thing as an equalizer, but not to manipulate sound, but a pattern. Anyway. My problem is, that if I have two points close to the border, sometimes the curve reaches beyond the borders:
Do you have any clue how could I solve the problem?
Here's the code, that I use:
in void setup: (obvious)
- for (int i = myPoints.size()-1; i >= 0; i--) {
- GrapPoint p = (GrapPoint) myPoints.get(i);
- p.display();
- }
and in void draw:
- beginShape();
- for (int i = myPoints.size()-1; i >= 0; i--) {
- GrapPoint p = (GrapPoint) myPoints.get(i);
- p.update();
- if (i == 0 || i == myPoints.size()-1) {
- curveVertex(p.posX, p.posY);
- }
- curveVertex(p.posX, p.posY);
- }
- endShape();
- void mouseClicked() {
- if (mouseX < 120+350 && mouseX > 120 && mouseY < 32+80 && mouseY > 32) {
- boolean pointThere = false;
- int b=0;
- for (int i = 0; i < myPoints.size(); i++) {
- GrapPoint p = (GrapPoint) myPoints.get(i);
- if (mouseX < p.posX+15 && mouseX > p.posX-15 && mouseY < p.posY+15 && mouseY > p.posY-15) {
- b = i;
- pointThere = true;
- }
- println("p "+i+" x:"+p.posX+" y:"+p.posY);
- }
- if (pointThere == false) {
- myPoints.add(new GrapPoint(mouseX, mouseY));
- }
- else {
- myPoints.remove(b);
- }
- println(myPoints.size());
- // Bubble Sort
- boolean swapped = true;
- int c;
- int n = 1;
- while (swapped) {
- swapped = false;
- for (int i = 0; i < myPoints.size()-n; i++) {
- GrapPoint p = (GrapPoint) myPoints.get(i);
- GrapPoint q = (GrapPoint) myPoints.get(i+1);
- if (q.posX < p.posX) {
- c = p.posX;
- p.posX = q.posX;
- q.posX = c;
- c = p.posY;
- p.posY = q.posY;
- q.posY = c;
- swapped = true;
- n++;
- }
- }
- }
- }
- }
And to drag it:
- void mouseDragged() {
- if (mouseX < 120+350-2 && mouseX > 122 && mouseY < 32+80-2 && mouseY > 34) {
- for (int i = 0; i < myPoints.size(); i++) {
- GrapPoint p = (GrapPoint) myPoints.get(i);
- if (mouseX < p.posX+4 && mouseX > p.posX-4 && mouseY < p.posY+4 && mouseY > p.posY-4) {
- p.posX = mouseX;
- p.posY = mouseY;
- }
- }
- }
- }
1