Line not drawing (PVector)

I’m about done scratching my head over this.
What I’m trying to do is this:

  1. Make a point A.
  2. Make a new point B in refference to point A with an added x and y using PVector, aswell as rotate by an amount.
  3. Make a line between point A and point B
  4. Continue this with an increasing rotation variable.

The imidiate problem I’m having is the line function not drawing lines, and me not understanding why.

Here’s the code I’ve written so far.

float fAngle = 0;
PVector pvLength = new PVector(0, -5);
ArrayList<PVector> alPoints = new ArrayList<PVector>();

void setup() {
  size(600, 600);
  surface.setLocation(200, 200);
  translate(width / 2, height / 2);
  alPoints.add(new PVector(0, 0));
  //alPoints.add(new PVector(width / 2, height / 2));
  //frameRate(2);
  strokeWeight(3);
  //background(155);
  stroke(255);
}

void draw() {
  // find median point of points and translate to that point
  // but for now
  //background(0);
  //PVector pvLatestPoint = alPoints.get(alPoints.size() -1);
  //translate(pvLatestPoint.x, pvLatestPoint.y);
  translate(width / 2, height / 2);

  for (int i = 0; i < alPoints.size() - 1; i++) {
    PVector pv1 = alPoints.get(i);
    PVector pv2 = alPoints.get(i + 1);
    line(pv1.x, pv1.y, pv2.x, pv2.y);
    point(pv1.x, pv1.y);
  }
  makePoint();
}

void makePoint() {
  PVector pvNewpoint = alPoints.get(alPoints.size()-1);
  pvNewpoint.add(pvLength);
  pvNewpoint.rotate(fAngle);
  //pvNewpoint.setMag(20);
  alPoints.add(pvNewpoint);
  fAngle += 20;
}

White line on white background…?
:wink:

It’s not that, I’ve just been trying a lot of things and forgot to set the background back when posting.

Hi,

I modified your code a little so you can see what’s happening:

float fAngle = 0;
PVector pvLength = new PVector(0, -5);
ArrayList<PVector> alPoints = new ArrayList<PVector>();

void setup() {
  size(600, 600);
  translate(width / 2, height / 2);
  alPoints.add(new PVector(0, 0));
}

void draw() {}

void mousePressed() {
  translate(width / 2, height / 2);

  for (int i = 0; i < alPoints.size(); i++) {
    PVector pv1 = alPoints.get(i);
    println(pv1.x, pv1.y);
  }
  println(""); //DEBUG
  
  makePoint();
}

void makePoint() {
  PVector pvNewpoint = alPoints.get(alPoints.size()-1);
  pvNewpoint.add(pvLength);
  pvNewpoint.rotate(fAngle);
  alPoints.add(pvNewpoint);
  fAngle += 20;
}

As you can see, the idea is to write in the console the content of your arrayList.
At each mouse click, we add a new point and the new content is written.

And you should see something strange happening:

  1. All your values are the same
  2. Each time you add a point the values are changing

image

In fact the issue is in the following line:

PVector pvNewpoint = alPoints.get(alPoints.size()-1);

pvNewpoint is a pointer to the last element of your array. So everything you will do to it, will also apply the last element of your array. It is not a new object, simply a new nickname for it if you will.

To solve it, simply create a copy of it:

PVector pvNewpoint = alPoints.get(alPoints.size()-1).copy();
1 Like

ah, you beat me to it!!!

  • also remember that angle is measured in radians, so you don’t want
    to add 20



float fAngle = 0;
PVector pvLength = new PVector(0, 15);
ArrayList<PVector> alPoints = new ArrayList<PVector>();

void setup() {
  size(600, 600);
  surface.setLocation(200, 200);

  // translate(width / 2, height / 2);
  alPoints.add(new PVector(0, 0));
  // alPoints.add(new PVector(0, 1));
  //alPoints.add(new PVector(width / 2, height / 2));
  //frameRate(2);
  strokeWeight(3);
  //background(155);
  stroke(255);
}

void draw() {
  // find median point of points and translate to that point
  // but for now

  //background(0);

  //PVector pvLatestPoint = alPoints.get(alPoints.size() -1);
  //translate(pvLatestPoint.x, pvLatestPoint.y);
  translate(width / 2, height / 2);
  print(alPoints.size(), " " );
  for (int i = 0; i < alPoints.size() -1; i++) {
    PVector pv1 = alPoints.get(i);
    PVector pv2 = alPoints.get(i + 1);
    stroke(255);
    fill(255); 

    line(pv1.x, pv1.y, 
      pv2.x, pv2.y);

    //point(pv1.x, pv1.y);
    // point(pv2.x, pv2.y);
  }
  makePoint();
}

void makePoint() {
  PVector pvNewpoint = alPoints.get(alPoints.size()-1).copy();
  pvNewpoint.add(pvLength);
  pvNewpoint.rotate(fAngle);
  alPoints.add(pvNewpoint);
  fAngle += 0.0220;
}
//

Thank’s alot :smiley: such a small error by my part.

1 Like