Reputation: 5121
The code given below is used for rotating a line about the center of the window. The thing is the output of the program is weird. Where am I going wrong? I am including code and the different outputs to illustrate the problem.
void setup()
{
size(500,500);
translate(width/2, height/2);
line(0,0,200,0);
line(0,0,0,200);
line(0,0,-200,0);
line(0,0,0,-200);
pushMatrix();
float x=200*cos(degrees(30));
float y=200*sin(degrees(30));
line(0,0,x,y);
popMatrix();
}
When changing the angle to 45 I get:
And when I change the angle to 60 I get:
How are the angles working here?
Upvotes: 1
Views: 1844
Reputation: 974
You used the wrong function to get radians.
radians(angle) converts degrees --> radians;
degrees(angle) converts radians --> degrees.
The following code works well:
float x, y, radius;
void setup () {
size(500, 500);
radius = 200;
}
void draw () {
background(255);
pushMatrix();
translate(width/2, height/2);
line(-200, 0, 200, 0);
line(0, -200, 0, 200);
x = radius*cos(radians(30));
y = radius*sin(radians(30));
line(0, 0, x, y);
popMatrix();
}
Let me clarify another thing:
Transformations are cumulative and apply to everything that happens after and subsequent calls to the function accumulates the effect. [...] If translate() or rotate() is called within draw(), the transformation is reset when the loop begins again. – P5 Reference
pushMatrix() and popMatrix() are useful to encapsulate matrix transformations you don't want to apply to the whole code. They are useless in your example.
Upvotes: 3