To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > Math and Physics

Reply
 
Thread Tools Search this Thread Display Modes
Old 09-07-2009, 10:37 AM   #1
olias32
Senior Member
 
olias32's Avatar
 
Join Date: Jan 2003
Location: Romania
Posts: 126
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
Code:
0.69 - 9.33
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
Code:
0.69 - 0.69
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

Quote:
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
__________________
http://www.fishstudio.ro
olias32 is offline   Reply With Quote
Old 09-07-2009, 10:51 AM   #2
olias32
Senior Member
 
olias32's Avatar
 
Join Date: Jan 2003
Location: Romania
Posts: 126
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.
__________________
http://www.fishstudio.ro

Last edited by olias32; 09-07-2009 at 11:03 AM.
olias32 is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > Math and Physics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:46 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.