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: