00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_MATH2D_H__
00021 #define __CS_MATH2D_H__
00022
00023 #define CS_POLY_IN 1
00024 #define CS_POLY_ON 0
00025 #define CS_POLY_OUT -1
00026
00027 #include "csgeom/vector2.h"
00028 #include "csgeom/plane2.h"
00029 #include "csgeom/segment.h"
00030
00031 class csBox2;
00032 class csPoly2D;
00033
00038 class csMath2
00039 {
00040 public:
00047 static int WhichSide2D (const csVector2& v,
00048 const csVector2& s1, const csVector2& s2)
00049 {
00050 float k = (s1.y - v.y)*(s2.x - s1.x);
00051 float k1 = (s1.x - v.x)*(s2.y - s1.y);
00052 if (k < k1) return -1;
00053 else if (k > k1) return 1;
00054 else return 0;
00055 }
00056
00063 static int WhichSide2D (const csVector2& v,
00064 const csSegment2& s)
00065 {
00066 return WhichSide2D (v, s.Start (), s.End ());
00067 }
00068
00076 static int InPoly2D (const csVector2& v,
00077 csVector2* P, int n, csBox2* bounding_box);
00078
00084 static float Area2 (const csVector2& a,
00085 const csVector2& b,
00086 const csVector2& c)
00087 {
00088 return
00089 a.x * b.y - a.y * b.x +
00090 a.y * c.x - a.x * c.y +
00091 b.x * c.y - c.x * b.y;
00092 }
00093
00099 static float Right (const csVector2& a,
00100 const csVector2& b,
00101 const csVector2& c)
00102 {
00103 return Area2 (a, b, c) <= -SMALL_EPSILON;
00104 }
00105
00111 static float Left (const csVector2& a,
00112 const csVector2& b,
00113 const csVector2& c)
00114 {
00115 return Area2 (a, b, c) >= SMALL_EPSILON;
00116 }
00117
00123 static bool Visible (const csVector2& p, const csPlane2& pl)
00124 { return pl.Classify (p) <= 0; }
00125
00132 static bool PlanesEqual (const csPlane2& p1, const csPlane2& p2)
00133 {
00134 return ( ( p1.norm - p2.norm) < (float).001 ) &&
00135 ( ABS (p1.CC-p2.CC) < (float).001 );
00136 }
00137
00143 static bool PlanesClose (const csPlane2& p1, const csPlane2& p2);
00144 };
00145
00151 class csIntersect2
00152 {
00153 public:
00160 static bool IntersectPolygon (const csPlane2& plane, csPoly2D* poly,
00161 csSegment2& segment);
00162
00168 static bool Segments (
00169 const csSegment2& a, const csSegment2& b,
00170 csVector2& isect, float& dist);
00171
00177 static bool SegmentLine (
00178 const csSegment2& a,
00179 const csSegment2& b,
00180 csVector2& isect, float& dist);
00181
00186 static bool Lines (
00187
00188 const csSegment2& a, const csSegment2& b,
00189 csVector2& isect);
00190
00199 static bool Plane (
00200 const csVector2& u, const csVector2& v,
00201 const csPlane2& p,
00202 csVector2& isect,
00203 float& dist);
00204
00213 static bool Plane (
00214 const csSegment2& uv,
00215 const csPlane2& p,
00216 csVector2& isect,
00217 float& dist)
00218 {
00219 return Plane (uv.Start (), uv.End (), p, isect, dist);
00220 }
00221
00226 static void PlaneNoTest (const csVector2& u, const csVector2& v,
00227 const csPlane2& p, csVector2& isect, float& dist)
00228 {
00229 float x,y, denom;
00230 x = v.x-u.x; y = v.y-u.y;
00231 denom = p.norm.x*x + p.norm.y*y;
00232 dist = -(p.norm*u + p.CC) / denom;
00233 isect.x = u.x + dist*x; isect.y = u.y + dist*y;
00234 }
00235
00240 static void PlaneNoTest (const csSegment2& uv,
00241 const csPlane2& p, csVector2& isect, float& dist)
00242 {
00243 PlaneNoTest (uv.Start (), uv.End (), p, isect, dist);
00244 }
00245
00251 static bool Planes (const csPlane2& p1, const csPlane2& p2,
00252 csVector2& isect);
00253
00254 };
00255
00256 #endif // __CS_MATH_H__