Trigonometry operation and precision
in
Programming Questions
•
2 years ago
Hi,
I'm trying to perform simple trigonometric operation and I notice a drift in the result.
The code below is expected to make a point to rotate around a center of rotation. Instead of roration on a circle path, it is doing a spiral path. I think it is due to a precision problem.
If you change the "theta" value to 360, the rotation is stable, but alway at the same place. Everything is fine.
Same thing if you put "180".
The program react weirdly if you put 90.
If you set "theta" to 10 degres for example, the point turn on a spiral instead of a cricle.
We see that the radius, between the center of rotation and the point itself, is not constant.
What can I do to solve this??
Thanks
Nick
***********************************
float theta1=0;
float cr_x=100;
float cr_y=100;
float box1_x=200;
float box1_y=100;
void setup() {
size(200,200);
}
void draw() {
background(200);
theta1=radians(10);
box1_x=box1_x-cr_x;
box1_y=box1_y-cr_y;
box1_x=box1_x*cos(theta1) - box1_y*sin(theta1);
box1_y=box1_x*sin(theta1) + box1_y*cos(theta1);
box1_x=box1_x+cr_x;
box1_y=box1_y+cr_y;
line(cr_x,cr_y,box1_x,box1_y);
rectMode(CENTER);
rect(box1_x, box1_y, 10,10);
println("radius : "+sqrt(pow(box1_x-cr_x,2)+pow(box1_y-cr_y,2)));
}
1