# Find new Destination Point (rotate PVector)

**URL:** <https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585>\
**Category:** Coding Questions\
**Created:** [July 13, 2020, 3:06am UTC](https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585 "2020-07-13T03:06:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [July 13, 2020, 3:06am UTC](https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585/1 "2020-07-13T03:06:43Z")

</div>

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.

```auto
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:

```auto
 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:

 ![Captura de Pantalla 2020-07-13 a la(s) 00.04.55](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2a4a13e0a7b7db95efafd6f5784a94112badee6c.png)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 13, 2020, 5:49am UTC](https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585/2 "2020-07-13T05:49:02Z")

</div>

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

Kf

```java

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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [April 11, 2022, 11:22pm UTC](https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585/3 "2022-04-11T23:22:09Z")

</div>

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

```auto
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);
}

```

 ![Captura de Pantalla 2022-04-11 a la(s) 20.19.25](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/a/6abc71c5c13f849bd77dd58c203aa39f4cd410e0.png)

any idea?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 12, 2022, 6:26am UTC](https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585/4 "2022-04-12T06:26:15Z")

</div>

> [@vjjv](#):
>
> `pos`

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 12, 2022, 8:37am UTC](https://discourse.processing.org/t/find-new-destination-point-rotate-pvector/22585/5 "2022-04-12T08:37:10Z")

</div>

**my version**

```auto
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);
}
//

```
