-
Senior Member
Math.cos not accurate! Help!
Hi everyone
I have the following issue which is driving me nuts.
I've written these 2 functions for returning the end coordinates of a line, when you know the starting coordinates, angle and length.
So, x1 and y1 are the starting point's coordinates, u = angle and l = length.
Code:
function degToRad(u) {
return((u*Math.PI)/180);
}
function angleLineX(x1, y1, l, u) {
u = degToRad(u);
x2 = l*Math.cos(u);
x2 = x1 + x2;
return(x2);
}
function angleLineY(x1, y1, l, u) {
u = degToRad(u);
y2 = l*Math.sin(u);
y2 = y1 - y2;
return(y2);
}
The degToRad() function just converts the angle from degrees to radians.
These functions usually work pretty well, but i came across a few values which don't seem to work.
Code:
a = angleLineX(0, 0, 864, 89.67);
b = angleLineX(0, 0, 864, 89.66);
trace(a + " - " + b);
the output to the above is
If i change it to
Code:
a = angleLineX(0, 0, 864, 89.68);
b = angleLineX(0, 0, 864, 89.67);
trace(a + " - " + b);
the output becomes
So, if i change the angle by 0.01 degrees i get the huge difference in the x coordinate, but only from 89.66 to 89.67
Otherwise, from 89.67 to 89.68 there's virtually no change
Can someone please help me?
Am i doing something wrong? I'm guessing the cos function is not very accurate, as you can see from the help file
Note: The cosine of a 90 degree angle is zero, but because of the inherent inaccuracy of decimal calculations using binary numbers, Flash Player will report a number extremely close to, but not exactly equal to, zero.
Not very accurate, ok, but to that extent?
Also, can anyone sugesst a fix or alternative to doing that?
Thanks
-
Senior Member
fixed. thanks
It wasn't a problem with the cos function but with a Math.round i was using when converting from degrees to radians,
which i forgot about.
Last edited by olias32; 09-07-2009 at 10:03 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|