The rotate() function is not rotating to the correct number of degrees. The image shows the screen with no rotate. If I apply a rotate command by uncommenting line 14, the line is rotated to the 11 o'clock position, not to 9'oclock as I would have expected.
function setup() {
createCanvas(200, 200);
angleMode = DEGREES;
}
function draw() {
noStroke();
fill(200, 102, 0);
circle(100, 100, 180); //draw pivot
strokeWeight(5);
stroke(0, 0, 0);
circle(100, 100, 15); //draw pivot point
translate(100, 100); //move origin
// rotate(180);
//rotate(radians(180));
line(0, 0, 90, 0);
}
However, if I use line 15 (rotate(radians(180)); it works as expected. What is happening?
rotate()accepts radians as its parameter by default, not degrees. By converting degrees to radians, the argument makes sense. Without the conversion, it's some random number of turns, so you wind up at whatever arbitrary rotation 180 radians is.Your attempt to set
angleMode,angleMode = DEGREES;doesn't do what you expect. This overwrites the p5 functionwindow.angleMode. p5 will warn you about the reassignment:Generally, setters in p5 aren't assignments. The correct usage is calling the function,
angleMode(DEGREES). This will allowrotate()to accept degrees by default so you don't need theradians()call.As an aside, canvas transformations like
translate()androtate()will stack, sopush()andpop()and typically used to save and restore the context after these operations:Since you're already translating, you might as well do it first and use 0, 0 as the origin for your drawings.
But it turns out that
rotate()isn't even necessary here:line(0, 0, -90, 0);will achieve the same result, which lets you avoid transformations entirely: