Discussion about math, puzzles, games and fun. Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ π -¹ ² ³ °

You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

**safra****Member**- Registered: 2005-12-03
- Posts: 41

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!

Offline

**John E. Franklin****Member**- Registered: 2005-08-29
- Posts: 3,585

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

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

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

**igloo** **myrtilles** **fourmis**

Offline

**safra****Member**- Registered: 2005-12-03
- Posts: 41

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!

Offline

**John E. Franklin****Member**- Registered: 2005-08-29
- Posts: 3,585

No prob.

**igloo** **myrtilles** **fourmis**

Offline

**Ricky****Moderator**- Registered: 2005-12-04
- Posts: 3,791

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:

```
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.

*Last edited by Ricky (2006-02-20 13:28:26)*

"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."

Offline

**John E. Franklin****Member**- Registered: 2005-08-29
- Posts: 3,585

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().

**igloo** **myrtilles** **fourmis**

Offline

**Ricky****Moderator**- Registered: 2005-12-04
- Posts: 3,791

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...

*Last edited by Ricky (2006-02-20 14:23:25)*

"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."

Offline

**John E. Franklin****Member**- Registered: 2005-08-29
- Posts: 3,585

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

**igloo** **myrtilles** **fourmis**

Offline

**safra****Member**- Registered: 2005-12-03
- Posts: 41

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 , 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!

Offline

Pages: **1**