Im trying to make a visualizaztion I saw in one of 3blue1brown videos
The code below is supposed to rotate two vectors (x,y and mpos.x, mpos.y) around another vector (dx,dy). I wrote my own function, because the built in rotate fuction didnt work. This doesnt either.
Thanks for your help!
PVector iHatHalfPi = new PVector(0, 1);
PVector jHatHalfPi = new PVector(-1, 0);
void setup() {
size(800, 800);
}
void draw() {
translate(width / 2, height / 2);
background(0);
PVector mpos = new PVector(mouseX - width / 2, mouseY - height / 2);
noFill();
stroke(255);
strokeWeight(4);
point(mpos.x, mpos.y);
beginShape();
for (int a = 0; a < 360; a += 10) {
float x = cos(radians(a)) * 350;
float y = sin(radians(a)) * 350;
vertex(x, y);
}
endShape(CLOSE);
for (int a = 0; a < 360; a += 10) {
float x = cos(radians(a)) * 350;
float y = sin(radians(a)) * 350;
float dx = lerp(mpos.x, x, 0.5);
float dy = lerp(mpos.y, y, 0.5);
stroke(255);
strokeWeight(2);
line(mpos.x, mpos.y, x, y);
stroke(0, 255, 0, 80);
point(dx, dy);
PVector rotated1 = Rotate(new PVector(x, y), new PVector(dx, dy), iHatHalfPi, jHatHalfPi);
PVector rotated2 = Rotate(new PVector(mpos.x, mpos.y), new PVector(dx, dy), iHatHalfPi, jHatHalfPi);
line(rotated1.x, rotated1.y, rotated2.x, rotated2.y);
}
}
PVector Rotate(PVector p, PVector c, PVector t, PVector t2) {
PVector pt = new PVector(p.x - c.x, p.y - c.y);
PVector point = new PVector(pt.x * t.x + pt.y * t2.x, pt.x * t.y + pt.y * t2.y);
return point;
}