We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys:
I'm finding a problem with a very simple piece of code.
It seems as if processing is not accurate when interpreting my 'rotate' command.
The result of the code is a square on which 45 degrees lines are drawn from the centre. But every fourth 'frameCount' the line is drawn on top of the previous one with the same degree rotation.
I'd expect every fourth line to be indistinguishable from the previous one, but the result suggests otherwise, as you can appreciate a slight widening of the superimposed lines.
Any ideas on how to avoid the 'widening' or inaccuracy?
Many Thanks.
The code:
int speed=1; int diameter=400; float lineLength=sqrt(sq(diameter/2)+sq(diameter/2)); float angle=QUARTER_PI;
void setup() { size (500,500); frameRate(speed); rectMode(CENTER); rect(width/2, height/2,diameter,diameter); }
void draw() { translate(width/2, height/2); rotate(angle); line(0,0,lineLength,0); println(PI+QUARTER_PI); angle+=HALF_PI; }
Comments
Hello ! What happen if you use 1.57 instead of HALF_PI ?
It gets much worse... the widening becomes very apparent. I guess it has to do with the lack of non-repetitive decimals.
It's weird, HALF_PI should be fine... Don't know, sorry
Thanks anyway! :)
The inaccuracy is not Processing itself, it is floating point numbers. HALF_PI cannot be represented correctly using floats and so each time you add that amount you are building up rounding errors.
Also there are visual artifacts sometimes in Processing when drawing lines on top of each other many many times without clearing the background. The solution to that would be to put background() inside draw() and then draw as many lines as you have to per draw() frame to match the original's behavior
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
I think I have a solution :)
Instead of incrementing your angle, you could store the 4 angles between the center and each corner, and then alternate the angle you use. If you do that, you will be sure to target exactly the same angle each time and it should work as expected
Thanks everyone for the feedback (including the 'how to format code' rules I was not aware of). I think 'asimes' is right. For now, I have solved the issue by re-initiating the 'angle' variable for each loop of 4 lines, avoiding the rounding errors.