Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

Post a reply

Go back

Write your message and submit
:) :| :( :D :o ;) :/ :P :lol: :mad: :rolleyes: :cool: | :dizzy :eek :kiss :roflol :rolleyes :shame :down :up :touched :sleep :wave :swear :tongue :what :faint :dunno
Options

Go back

Topic review (newest first)

safra
2006-02-21 21:15:14

Thanks Ricky, the cos sin and arctan functions were/are a bit of a concern as yes they are supposed to be slow. I put it on the list of optimization issues and will test what the fastest solution is (including my silly pythagoras solution wink, but who knows, 10 lines of code can be faster then one line!). Would be nice to know what your findings are, if you wish to share?

Thanks a lot both of you!

John E. Franklin
2006-02-21 13:55:53

I think the pentium processor might have a floating point processor in it to do trig??  Just a guess.

Ricky
2006-02-21 13:22:30

Maybe you can just ignore that post all together...

So I wrote my own sin function, like I described, and a hash table, but it seems to be about 10-15 times as slow as the function included in the C library (which are the same ones that you use in C++).

I see no way to optimize my solution unless you take out the function call (which might give it large overhead).  This makes me really curious about how the trig functions actually work...

John E. Franklin
2006-02-21 12:40:39

Making a hash table for arctan will be funny to use, because the slope changes by leaps and bounds between 88 degrees
and 89.99 degrees.   But with a little thought, it would probably still be a good idea.   Just need to create a
binary tree instead of a hash table, so you can test less or greater or equal, and get down to the right element to
use quickly.   That's what I would do for the arctan().

Ricky
2006-02-21 12:27:06

Cx = Ax + 50 cos(arctan(slope))

Cy = Ay + 50 sin(arctan(slope))

Haven't really check to see if these are the right answers, I'm just going to assume they are.  Oh, and if speed of your program doesn't matter, none of the below will...

sin and cos are normally very slow functions (depending on how your compiler implements them).  Always try to avoid using them if possible.  But, if you have to use them, heres how:

Using sin and arctan functions in whatever language your using, generate a file that just lists these values of certain numbers.  If you were doing it in C++, it would be:

Code:

for (x = 0; x < 2*PI; x+=PI/720.0)
{
   o << sin(x) << endl;
}

Now generate that file for whatever trig functions you are using.  Then, in the beginning of your program, you load up these data values into a hash table.  From this, you can then approximate each trig function in O(1) time, which means constant time, the fastest you can get.  I picked the value PI/720.  This will generate a fairly small file (1440 lines).  You can use higher values such as PI/1000 or even PI/2000, and you will get a very close approximation.  With 2000, that will probably be as close an approximation as just sin(x) is.

The downside of using larger variable is that it uses more hard drive space (the file) and it uses more RAM (the hash table).

Edit:  If you wish to do this, but got lost in my mumbo jumbo, just say so.  I'm going to be doing this very soon for a project of my own (Pool, aka billards), so I might as well do it now.

John E. Franklin
2006-02-21 09:46:34

No prob.

safra
2006-02-21 09:39:30

Oh yes, that works great! I now have a ball cam in a direct x game. What I do is use the new and old ball position to get the direction of the ball flight and place the camera either in front or behind the ball at a fixed distance at exactly the height of the ball. Yes, Math is Fun!

Got one weird behavior though, as soon as the ball hits the ground the cam is reversed and actually point A (the new point) gets in between the old point (B) and C (the cam). Quite strange, let's see if I can figure out why that happens.

Thanks a lot John!

John E. Franklin
2006-02-21 08:53:13

slope = (By - Ay) / (Bx - Ax)

Cx = Ax + 50 cos(arctan(slope))

Cy = Ay + 50 sin(arctan(slope))

safra
2006-02-21 08:40:10

I have to 2 points A and B with known x and y values. Point C is on the same vector as A and B (the line that crosses A and B). The distance between A and C is constant (for example 50). C can be between A and B or B can be between A and C. A can never be between B and C! What is the fastest way to get the coordinates of C? Hope this makes sense to someone!?

I started with this using pythagoras from the old days back at school and I think I will get there at the end but as this is part of a program I think I will need at least 10 lines with if statements and defining imaginary coordinates when using pythagoras while it should be as fast as possible.

Hope someone can help me with this, thanks!

Board footer

Powered by FluxBB