Daniel Pitts wrote:
Specifically, I have an Angle class and a Vector class, and I'd like to
be able to convert between them quickly...
----Context----
The Angle class stores its value as a 8 bit integer value representing
"bygrees" (byte degrees). each "bygree" is 1/256th of a circle, with 0
starting at 12:00 going clockwise.
The Vector class stores its value as a pair of floating (double
precision) values.
After running it through a profiler, I've optimized the angle class to
precompute all 256 possible values, including sine, cosine, radian
conversion, etc...Good speed increase :-)
But, now my application is bottlenecking on an Math.atan2 call,
converting a vector's direction to an Angle object.
----One solution----
I'm trying to think of ways to speed up the conversion of Vectors to
Angles, but the only way I can think to precompute this is to make a 2d
array of Angle objects. and return
angleFor[normalizeForLookup(x)][normalizeForLookup(y)]. Where
normalizeForLookup would handle rounding and bound's checking.
OR, I could use an atan lookup for int((y/x) * k) (k to be determined),
but I would have to find a suitable k, and maybe some other tricks.
On the plus side, I can determine that the largest magnetude of any
vector would be sqrt(2) * 1000, and accuracy isn't extremely important.
The downside to this would be creating an array of approximitly
4,000,000 elements. Although most of those elements point to a subset
of 256 objects, 4 megs of references is non negligible. Not to mention
the init time required.
----Another possible solution----
So, the other alternative I see is to find a faster implementation of
atan2 (or atan), and use it in place of Math.atan2, but I wouldn't know
where to start. a quick search on google returned nothing, but I'm not
very good at finding keywords to search.
Thanks in advance,
Daniel.
In addition to Eric's suggestions, are you sure that you really need to
convert the vectors into angles? A lot of people do this unnecessarily.
Mark Thornton
my reply to Erics post.