Page 1 of 1

Trigonometry in PHP, a bit of angle calculation

Posted: Fri Feb 14, 2003 9:16 am
by GodDamnit
Hi, I'm stuck and need some help. I've spent the last week working on a trig class that will allow the user to construct a virtual polygon and determine if a given point is within that polygon or not.

Having browsed the net extensively I have found a few decent pieces of code, but nothing in PHP, so I've been trying to convert these. The basic theory is that if you add up all of the angles between the points on the polygon from the target point, you should get 360 degrees. However, calculating which angles should be positive and which should be negative, etc is getting to me. I'm not bad at maths, hell, I spent a couple of years working in OpenGL, lots of maths there, but the difference is that the languages I used then supported a getangle function.

I was just wondering if someone has a guaranteed working getangle function for PHP? A function that takes 3 points and calculates the angle between 2 of them from the third, and which takes into account the direction of the vector between the two points so that negatives are calculated correctly. Does anyone know where I can find such a function?

This has really been annoying me, I've trashed the code and started over again 3 times already and I dont want to have to do that again! :)

Thanks for any help.

Posted: Fri Feb 14, 2003 4:12 pm
by Rob the R
Although I don't have this in PHP, I wonder if you ran across this site:

http://mcraefamily.com/MathHelp/Geometr ... angle2.htm

This has a very detailed explanation of a different technique and an implementation in Javascript (which is easily ported to PHP). I was surprised at how small the code turns out to be!

Posted: Sat Feb 15, 2003 5:49 am
by GodDamnit
Thanks, but that method really only works with triangles. I have now successfully got the angle calculation working correctly, but I'm just having trouble getting it to determine if the angle should be negative or not. But I've been able to come up with some theories that should work. Thanks for the help anyway.

Posted: Sat Feb 15, 2003 11:44 am
by Rob the R
Hm. You're right, although the method in the site I mentioned will work if the polygon has no angles > 180 degrees (there's a name for that type of polygon, but I can't remember it).

Sorry I wan't much help, but I hope you get your problem solved!