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
00029 #define CS_POLY_IN 1
00030 #define CS_POLY_ON 0
00031 #define CS_POLY_OUT -1
00032
00033 #include "csgeom/vector2.h"
00034 #include "csgeom/plane2.h"
00035 #include "csgeom/segment.h"
00036
00037 class csBox2;
00038 class csPoly2D;
00039
00044 class csMath2
00045 {
00046 public:
00053 static int WhichSide2D (const csVector2& v,
00054 const csVector2& s1, const csVector2& s2)
00055 {
00056 float k = (s1.y - v.y)*(s2.x - s1.x);
00057 float k1 = (s1.x - v.x)*(s2.y - s1.y);
00058 if (k < k1) return -1;
00059 else if (k > k1) return 1;
00060 else return 0;
00061 }
00062
00069 static int WhichSide2D (const csVector2& v,
00070 const csSegment2& s)
00071 {
00072 return WhichSide2D (v, s.Start (), s.End ());
00073 }
00074
00082 static int InPoly2D (const csVector2& v,
00083 csVector2* P, int n, csBox2* bounding_box);
00084
00090 static float Area2 (const csVector2& a,
00091 const csVector2& b,
00092 const csVector2& c)
00093 {
00094 return
00095 a.x * b.y - a.y * b.x +
00096 a.y * c.x - a.x * c.y +
00097 b.x * c.y - c.x * b.y;
00098 }
00099
00105 static float Right (const csVector2& a,
00106 const csVector2& b,
00107 const csVector2& c)
00108 {
00109 return Area2 (a, b, c) <= -SMALL_EPSILON;
00110 }
00111
00117 static float Left (const csVector2& a,
00118 const csVector2& b,
00119 const csVector2& c)
00120 {
00121 return Area2 (a, b, c) >= SMALL_EPSILON;
00122 }
00123
00129 static bool Visible (const csVector2& p, const csPlane2& pl)
00130 { return pl.Classify (p) <= 0; }
00131
00138 static bool PlanesEqual (const csPlane2& p1, const csPlane2& p2)
00139 {
00140 return ( ( p1.norm - p2.norm) < (float).001 ) &&
00141 ( ABS (p1.CC-p2.CC) < (float).001 );
00142 }
00143
00149 static bool PlanesClose (const csPlane2& p1, const csPlane2& p2);
00150 };
00151
00157 class csIntersect2
00158 {
00159 public:
00166 static bool IntersectPolygon (const csPlane2& plane, csPoly2D* poly,
00167 csSegment2& segment);
00168
00174 static bool Segments (
00175 const csSegment2& a, const csSegment2& b,
00176 csVector2& isect, float& dist);
00177
00183 static bool SegmentLine (
00184 const csSegment2& a,
00185 const csSegment2& b,
00186 csVector2& isect, float& dist);
00187
00192 static bool Lines (
00193
00194 const csSegment2& a, const csSegment2& b,
00195 csVector2& isect);
00196
00205 static bool Plane (
00206 const csVector2& u, const csVector2& v,
00207 const csPlane2& p,
00208 csVector2& isect,
00209 float& dist);
00210
00219 static bool Plane (
00220 const csSegment2& uv,
00221 const csPlane2& p,
00222 csVector2& isect,
00223 float& dist)
00224 {
00225 return Plane (uv.Start (), uv.End (), p, isect, dist);
00226 }
00227
00232 static void PlaneNoTest (const csVector2& u, const csVector2& v,
00233 const csPlane2& p, csVector2& isect, float& dist)
00234 {
00235 float x,y, denom;
00236 x = v.x-u.x; y = v.y-u.y;
00237 denom = p.norm.x*x + p.norm.y*y;
00238 dist = -(p.norm*u + p.CC) / denom;
00239 isect.x = u.x + dist*x; isect.y = u.y + dist*y;
00240 }
00241
00246 static void PlaneNoTest (const csSegment2& uv,
00247 const csPlane2& p, csVector2& isect, float& dist)
00248 {
00249 PlaneNoTest (uv.Start (), uv.End (), p, isect, dist);
00250 }
00251
00257 static bool Planes (const csPlane2& p1, const csPlane2& p2,
00258 csVector2& isect);
00259
00260 };
00261
00264 #endif // __CS_MATH2D_H__