rotate around point
in
Programming Questions
•
1 year ago
I want to rotate the PVectors for the quad around the center point.
Without translate and rotate!!!
However i keep failing, my logic is lost :)
I like what i have now but it's not what i want.
- PVector pos1, pos2, pos3, pos4;
- PVector p1, p2, p3, p4;
- PVector center;
- void setup() {
- size(400, 400);
- p1 = new PVector(50, 50);
- p2 = new PVector(width-50, 100);
- p3 = new PVector(width-50, 200);
- p4 = new PVector(50, 300);
- // p1 --- p2
- // | |
- // | |
- // p4 --- p3
- center = new PVector(width/2, height/2);
- }
- void draw() {
- background(150);
- pos1 = p1.get();
- pos2 = p2.get();
- pos3 = p3.get();
- pos4 = p4.get();
- // rotation from center to mouse
- float a = atan2(mouseY - center.y, mouseX - center.x);
- // to rotation should be in this direction:
- pushMatrix();
- translate(center.x, center.y);
- rotate(a);
- line(0, 0, 100, 0);
- popMatrix();
- // attempt #13
- pos1.x = (pos1.x - center.x) * cos(a);
- pos1.y = (pos1.y - center.y) * sin(a);
- pos2.x = (pos2.x - center.x) * cos(a);
- pos2.y = (pos2.y - center.y) * sin(a);
- pos3.x = (pos3.x - center.x) * cos(a);
- pos3.y = (pos3.y - center.y) * sin(a);
- pos4.x = (pos4.x - center.x) * cos(a);
- pos4.y = (pos4.y - center.y) * sin(a);
- pos1.x += center.x;
- pos1.y += center.y;
- pos2.x += center.x;
- pos2.y += center.y;
- pos3.x += center.x;
- pos3.y += center.y;
- pos4.x += center.x;
- pos4.y += center.y;
- quad(pos1.x, pos1.y, pos2.x, pos2.y, pos3.x, pos3.y, pos4.x, pos4.y);
- }
1