Find new Destination Point (rotate PVector)

Hi everyone.
I ve this silly problem that i cant figure out.

Im creating an object that has an origin point and destination point. When i create a new object, i swap the coordinate and the destination vector is now the origin vector of the new object.

But now i want to find the new destination point according a certain angle that i give in the constructor.
This is the code. is very similar to a “random Walker” , but I want to specify the angle and size of the vector.

ArrayList <Point>p;

void setup() {
  p = new ArrayList<Point>();
  p.add (new Point(new PVector(0,0), new PVector(100, 100)));
  size(1000, 1000);
}

void draw() {
  background(255);
  translate(500, 500);
  for(Point pp : p){
     pp.display();
  }
}

void mousePressed(){
  
  Point pp = p.get(p.size()-1);
  p.add (new Point(new PVector(pp.dest.x, pp.dest.y), 45));

}

class Point {

  PVector origin;
  PVector dest;
  float angle;
  
  Point(PVector _origin, PVector _dest) {
    origin = _origin;
    dest = _dest;
  }
  
  Point(PVector _origin, int a) {
    origin = _origin;
    angle = a;
    dest = new PVector(1,1);
    dest = origin.rotate(45);
    dest.setMag(100);
  }
  
  void display(){
    line(origin.x,origin.y, dest.x, dest.y);
  }
}

As an alternative to the rotate function im using this:

 void rotate2D(PVector v, int choice) {
    float theta =0;
    
    if(choice == 0){
    theta = 0;
    }else if(choice == 1){
    theta = radians(45);
    }else if(choice == 2){
    theta = radians(90);
    }else if(choice ==3){
     theta = radians(135);
    }else if(choice ==4){
     theta = radians(180);
    }else if(choice ==5){
     theta = radians(225);
    }else if(choice ==6){
     theta = radians(270);
    }else if(choice ==7){
     theta = radians(335);
    }else if(choice ==8){
     theta = radians(360);
    }   
  // What's the magnitude?
  float m = v.mag();
  // What's the angle?
  float a = v.heading2D();
  // Change the angle
  a += theta;
  // Polar to cartesian for the new xy components
  v.x += m * cos(a);
  v.y += m * sin(a);
  
  println(v);
}

and i want is something similar to this:

You need to call copy() before you modify the object. Working example below.

Kf


ArrayList <Point>p;

void setup() {
  size(1000, 1000);
  p = new ArrayList<Point>();
  p.add (new Point(new PVector(0, 0), new PVector(100, 100)));
  strokeWeight(3);
  noLoop();
}

void draw() {
  background(125);
  translate(width/2,height/2);
  
  for (Point pp : p) {
    pp.display();
  }
}

void mousePressed() {

  Point pp = p.get(p.size()-1);
  println(frameCount,pp);
  p.add (new Point(new PVector(pp.dest.x, pp.dest.y), 45));
  
  redraw();
}

class Point {

  PVector origin;
  PVector dest;
  float angle;

  Point(PVector _origin, PVector _dest) {
    origin = _origin;
    dest = _dest;
  }

  Point(PVector _origin, int a) {
    origin = _origin;
    angle = a;
    dest = new PVector(1, 1);
    dest = origin.copy().rotate(45);
    dest.setMag(100);
  }

  void display() {
    line(origin.x, origin.y, dest.x, dest.y);
  }
  
  @Override
  String toString(){
    return origin+":  "+dest;
  }
}
2 Likes

So, still having problems to draw the angles correctly: I don’t know why strage angles appear, I can’t understand it

PVector pos;
PVector prev;
float [] angles = {0, -45, -90};

void setup() {
  size(1000, 1000);
  background(255);
  pos = new PVector(200, 200);
  prev = pos.copy();
}

void draw() {

  //point(pos.x, pos.y);
}

void mousePressed() {
  stroke(0);
  strokeWeight(2);

  prev.set(pos);
  prev.rotate(0);
  PVector step  = pos.copy().rotate(radians(angles[int(random(angles.length))]));

  step.setMag(100);

  pos.add(step);
  line(pos.x, pos.y, prev.x, prev.y);
}

any idea?

Maybe you should say rotate 0 or clear the rotation previous to set it

my version

PVector pos;
PVector prev;

// in degrees: 
float [] angles = {
  0, -45, -90
};

void setup() {
  size(1000, 1000);
  background(255);

  pos = new PVector(20, height-50);
  prev = pos.copy();
}

void draw() {
  //point(pos.x, pos.y);
}

//----------------------------------------------------------------

void mousePressed() {

  stroke(0);
  strokeWeight(2);

  prev.set(pos.x, pos.y);

  float angle = radians(angles[int(random(angles.length))]); 
  PVector step = new PVector(20, 0); // x length = 20, y length = 0
  step.rotate(angle);

  pos.add(step);
  line(prev.x, prev.y, 
    pos.x, pos.y);
}
//

1 Like