14.08.2010, 20:11
Sometimes you need laser precision detection when a point (player or a turtle) is near a arbitrary line (example line: near a train, or ON a straight long diagonal road)
Following Paul Bourke's excellent advanced math tutorial here, i wrote a pawn version of the example code from his website:
http://local.wasp.uwa.edu.au/~pbourk...try/pointline/
The function returns absurd high value if the point is not close to the line, and the real distance to the line perpendicular to it - as long as the point falls within the line segment. This way you can easily use it to detect if a player is near a line in your script.
Following Paul Bourke's excellent advanced math tutorial here, i wrote a pawn version of the example code from his website:
http://local.wasp.uwa.edu.au/~pbourk...try/pointline/
Code:
// thanks pbourke: http://local.wasp.uwa.edu.au/~pbourke/ge...75945stock Float:Magnitude( Float:Point1[3], Float:Point2[3] ) { new Float:Vector[3]; Vector[0] = Point2[0] - Point1[0]; Vector[1] = Point2[1] - Point1[1]; Vector[2] = Point2[2] - Point1[2]; return floatsqroot( Vector[0] * Vector[0] + Vector[1] * Vector[1] + Vector[2] * Vector[2] ); } stock Float:DistancePointLine( Float:Point[3], Float:LineStart[3], Float:LineEnd[3]) { new Float:LineMag; new Float:U; new Float:Intersection[3]; LineMag = Magnitude( LineEnd, LineStart ); LineMag *= LineMag; if (LineMag < 0.001) // prevent div by zero below, in pawn this equals to "the shit hits the fan" or "all hell breaks loose" return 10000000.0; U = ( ( ( Point[0] - LineStart[0] ) * ( LineEnd[0] - LineStart[0] ) ) + ( ( Point[1] - LineStart[1] ) * ( LineEnd[1] - LineStart[1] ) ) + ( ( Point[2] - LineStart[2] ) * ( LineEnd[2] - LineStart[2] ) ) ) / LineMag; if ( U < 0.0 || U > 1.0 ) return 10000000.0; // closest point does not fall within the line segment Intersection[0] = LineStart[0] + U * ( LineEnd[0] - LineStart[0] ); Intersection[1] = LineStart[1] + U * ( LineEnd[1] - LineStart[1] ); Intersection[2] = LineStart[2] + U * ( LineEnd[2] - LineStart[2] ); return Magnitude( Point, Intersection ); }