'Rotating Line around Point in p5.js
Goal: I'm trying to create a triangle given two angles (a0,b0)
. To do so, I'm trying to rotate a vector r0
by some angle a0
around one of the vertices of r0
. Here's a diagram illustrating my idea.
Problem: However, when I rotate the line, it seems to rotate around the origin. I've read many, many articles on how to fix this, but none of the suggested solutions (i.e., translate then rotate,
push()
, pop()
) don't seem to work, perhaps because I'm dealing with a line segment here. Below is my code.
MWE Code:
let angle = 0;
function setup() {
createCanvas(600, 400);
angleMode(DEGREES);
}
function draw() {
let v1 = createVector(width / 2 - 50, height / 2);
let v2 = createVector(width / 2 + 50, height / 2);
background(255);
stroke(0);
strokeWeight(4);
let r0 = line(v1.x, v1.y, v2.x, v2.y);
rotate(20);
let r1 = line(v1.x, v1.y, v2.x, v2.y);
strokeWeight(10);
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/p5.min.js"></script>
Any help is appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|