csgeom/math2d.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998-2000 by Jorrit Tyberghein 00003 Largely rewritten by Ivan Avramovic <ivan@avramovic.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_MATH2D_H__ 00021 #define __CS_MATH2D_H__ 00022 00029 // These are also defined in plane2.h 00030 #ifndef __CS_POLY_MACROS__ 00031 #define __CS_POLY_MACROS__ 00032 #define CS_POLY_IN 1 00033 #define CS_POLY_ON 0 00034 #define CS_POLY_OUT -1 00035 #endif 00036 00037 #include "csextern.h" 00038 00039 #include "csgeom/vector2.h" 00040 #include "csgeom/plane2.h" 00041 #include "csgeom/segment.h" 00042 00043 class csBox2; 00044 class csPoly2D; 00045 00050 class CS_CSGEOM_EXPORT csMath2 00051 { 00052 public: 00059 static int WhichSide2D (const csVector2& v, 00060 const csVector2& s1, const csVector2& s2) 00061 { 00062 float k = (s1.y - v.y)*(s2.x - s1.x); 00063 float k1 = (s1.x - v.x)*(s2.y - s1.y); 00064 if (k < k1) return -1; 00065 else if (k > k1) return 1; 00066 else return 0; 00067 } 00068 00075 static int WhichSide2D (const csVector2& v, 00076 const csSegment2& s) 00077 { 00078 return WhichSide2D (v, s.Start (), s.End ()); 00079 } 00080 00088 static int InPoly2D (const csVector2& v, 00089 csVector2* P, int n, csBox2* bounding_box); 00090 00096 static float Area2 (const csVector2& a, 00097 const csVector2& b, 00098 const csVector2& c) 00099 { 00100 return 00101 a.x * b.y - a.y * b.x + 00102 a.y * c.x - a.x * c.y + 00103 b.x * c.y - c.x * b.y; 00104 } 00105 00111 static float Right (const csVector2& a, 00112 const csVector2& b, 00113 const csVector2& c) 00114 { 00115 return Area2 (a, b, c) <= -SMALL_EPSILON; 00116 } 00117 00123 static float Left (const csVector2& a, 00124 const csVector2& b, 00125 const csVector2& c) 00126 { 00127 return Area2 (a, b, c) >= SMALL_EPSILON; 00128 } 00129 00135 static bool Visible (const csVector2& p, const csPlane2& pl) 00136 { return pl.Classify (p) <= 0; } 00137 00144 static bool PlanesEqual (const csPlane2& p1, const csPlane2& p2) 00145 { 00146 return ( ( p1.norm - p2.norm) < (float).001 ) && 00147 ( ABS (p1.CC-p2.CC) < (float).001 ); 00148 } 00149 00155 static bool PlanesClose (const csPlane2& p1, const csPlane2& p2); 00156 }; 00157 00163 class CS_CSGEOM_EXPORT csIntersect2 00164 { 00165 public: 00172 static bool IntersectPolygon (const csPlane2& plane, csPoly2D* poly, 00173 csSegment2& segment); 00174 00180 static bool Segments ( 00181 const csSegment2& a, const csSegment2& b, // Two segments. 00182 csVector2& isect, float& dist); // intersection point and distance 00183 00189 static bool SegmentLine ( 00190 const csSegment2& a, // First segment. 00191 const csSegment2& b, // A line (end is only direction) 00192 csVector2& isect, float& dist); // intersection point and distance 00193 00198 static bool Lines ( 00199 // Two lines (end is only direction). 00200 const csSegment2& a, const csSegment2& b, 00201 csVector2& isect); // intersection point 00202 00211 static bool Plane ( 00212 const csVector2& u, const csVector2& v, 00213 const csPlane2& p, // plane Ax+By+Cz+D=0 00214 csVector2& isect, // intersection point 00215 float& dist); // distance from u to isect 00216 00225 static bool Plane ( 00226 const csSegment2& uv, // Segment. 00227 const csPlane2& p, // plane Ax+By+Cz+D=0 00228 csVector2& isect, // intersection point 00229 float& dist) // distance from u to isect 00230 { 00231 return Plane (uv.Start (), uv.End (), p, isect, dist); 00232 } 00233 00238 static void PlaneNoTest (const csVector2& u, const csVector2& v, 00239 const csPlane2& p, csVector2& isect, float& dist) 00240 { 00241 float x,y, denom; 00242 x = v.x-u.x; y = v.y-u.y; 00243 denom = p.norm.x*x + p.norm.y*y; 00244 dist = -(p.norm*u + p.CC) / denom; 00245 isect.x = u.x + dist*x; isect.y = u.y + dist*y; 00246 } 00247 00252 static void PlaneNoTest (const csSegment2& uv, 00253 const csPlane2& p, csVector2& isect, float& dist) 00254 { 00255 PlaneNoTest (uv.Start (), uv.End (), p, isect, dist); 00256 } 00257 00263 static bool Planes (const csPlane2& p1, const csPlane2& p2, 00264 csVector2& isect); 00265 00266 }; 00267 00270 #endif // __CS_MATH2D_H__
Generated for Crystal Space by doxygen 1.2.18