# Rotating vectors around the Y-axis

Dear all,

I would need your help to compute the vectors of a 3D curl field.

In 2D I used to:

• create a vector field in 2D
• place a bunch of points randomly in that field
• have the vectors pointing to their closest point
• rotate the vectors by 90 degrees

This is probably not the correct way to do this and if you have a better approach I would be very interested to see it.

``````int W = 40, H = 40, step = 20;
int N = W*H;

void setup() {
size(800, 800, P2D);
background(255);
randomSeed(1002);
smooth(8);

ArrayList<PVector> points = new ArrayList();

for (int i=0; i<10; i++) {
}

pushStyle();
strokeWeight(12);
stroke(255, 40, 40);
for (PVector p : points) {
point(p.x, p.y);
}
popStyle();

for (int i=0; i<N; i++) {
float x = i%W;
float y = i/W;
PVector pt = new PVector(x*step, y*step);

float minDist = 10000;
float d = 0;
PVector closest = null;

for (PVector p : points) {
d = pt.dist(p);
if (d < minDist) {
minDist = d;
closest = p;
}
}

PVector vec = closest.copy().sub(pt).setMag(step);
pushMatrix();
translate(pt.x, pt.y);
rotate(HALF_PI); // for curl field
line(0, 0, vec.x, vec.y);
popMatrix();

PVector rotatedVector = vec.copy().rotate(HALF_PI);

}

noLoop();

}
``````

The problem arises when I want to rotate the vectors in 3D.

I can rotate the lines with `rotateY(HALF_PI)` but I don’t know how to get the vector corresponding to these rotated lines.

In 2D I could write `rotatedVector = vec.copy().rotate(HALF_PI)` but in 3D i cannot apply `rotateY` to a vector (only `rotate`).

Hou would you compute this rotated vector in 3D ?

``````import peasy.*;
PeasyCam cam;

int W = 10, H = 10, D = 10, step = 50;
int N = W*H*D;
ArrayList<PVector> points = new ArrayList();

void setup() {
size(1000, 600, P3D);
randomSeed(1002);
smooth(8);

cam = new PeasyCam(this, 800);

for (int i=0; i<10; i++) {
points.add(new PVector(random(W*step*.25, W*step*.75), random(100, (H-1)*step), random(D*step*.25, D*step*.75)));
}
}

void draw() {
background(255);
translate((-W*step)>>1, (-H*step)>>1, (-D*step)>>1);

pushStyle();
strokeWeight(12);
stroke(255, 40, 40);
for (PVector p : points) {
point(p.x, p.y, p.z);
}
popStyle();

for (int i=0; i<N; i++) {
float x = i%W;
float y = (i/W)%H;
float z = i/(W*H);

PVector pt = new PVector(x*step, y*step, z*step);

float minDist = 10000;
float d = 0;
PVector closest = null;

for (PVector p : points) {
d = pt.dist(p);
if (d < minDist) {
minDist = d;
closest = p;
}
}

PVector vec = new PVector(closest.x, pt.y, closest.z).sub(pt).setMag(step*.8);
pushMatrix();
translate(pt.x, pt.y, pt.z);
rotateY(HALF_PI); // for curl field
line(0, 0, 0, vec.x, vec.y, vec.z);
popMatrix();

//PVector rotatedVector = ????;
}

}
``````

2 Likes

Apologies, I think I just found what I hadn’t managed to find earlier.

``````// Rotating a vector around the Y axis by 90 degrees

PVector vector = PVector(random(width), random(height), random(depth))

float x = vector.x * cos(HALF_PI) + vector.z * sin(HALF_PI)
float y = vector.y
float z = -vector.x * sin(HALF_PI) + vector.z * cos(HALF_PI)

PVector rotatedVector = PVector(x, y, z)
``````