java - Drawing a circle in a turtle program -


i working on processing (as in language) sketch, driven turtle logic (see https://en.wikipedia.org/wiki/turtle_graphics). means draw line current coordinate supplied coordinate. supplied coordinate become new current coordinate. want approximate circle , have written simple piece of code using trigonometrics. code looks follow:

void drawcircle(int radius){    // circle center radius amount left of current xpos    int steps = 16;    double step = two_pi /steps;     for(double theta = step; theta <= two_pi; theta += step){       float deltax = cos((float)theta) - cos((float)(theta - step));       float deltay = sin((float)theta) - sin((float)(theta - step));        movexy(deltax*radius, deltay*radius);    }  } 

the program logic simple. use variable theta loop through radians in circle. amount of steps indicate how large each theta chunk is. calculate x,y values specific point in circle governed theta. deduct x,y values of previous cycle (hence theta-step) amount have move position attain desired x,y position. supply delta values movexy function, draws line current point supplied values , makes them new current position.

the program seems work quite when using limited amount of steps. however, when step count increased, circles become more , more fibonacci spiral. guess due imprecision float number , sine , cosine calculations, , adds each iteration.

have interpreted wrong? looking port code javascript eventually, looking solution in design. using bigdecimal might not work, since not contain own cosine , sine functions. have included few images detail problem. appreciated!

step count 16: step count 16

step count 32: step count 32

step count 64: step count 64

step count 128: step count 128

float , sine/cosine should sufficiently precise. question is: how precise position on plane? if position measured in pixels, each of floating point values rounded integer after each step. loss of precision adds up.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -