| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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
    00031 #include "csextern.h"
    00032
    00033 #include "csgeom/plane2.h"
    00034 #include "csgeom/segment.h"
    00035 #include "csgeom/vector2.h"
    00036
    00037 class csBox2;
    00038 class csPoly2D;
    00039
    00044 class CS_CRYSTALSPACE_EXPORT 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 CS_CRYSTALSPACE_EXPORT csIntersect2
    00158 {
    00159 public:
    00166   static bool PlanePolygon (const csPlane2& plane, csPoly2D* poly,
    00167         csSegment2& segment);
    00168
    00175   static bool SegmentSegment (
    00176     const csSegment2& a, const csSegment2& b,   // Two segments.
    00177     csVector2& isect, float& dist);         // intersection point and distance
    00178
    00185   static bool SegmentLine (
    00186     const csSegment2& a,                // First segment.
    00187     const csSegment2& b,                // A line (end is only direction)
    00188     csVector2& isect, float& dist);     // intersection point and distance
    00189
    00195   static bool LineLine (
    00196     // Two lines (end is only direction).
    00197     const csSegment2& a, const csSegment2& b,
    00198     csVector2& isect);                      // intersection point
    00199
    00209   static bool SegmentPlane (
    00210     const csVector2& u, const csVector2& v,
    00211     const csPlane2& p,                     // plane Ax+By+Cz+D=0
    00212     csVector2& isect,                     // intersection point
    00213     float& dist);                       // distance from u to isect
    00214
    00224   static bool SegmentPlane (
    00225     const csSegment2& uv,       // Segment.
    00226     const csPlane2& p,                     // plane Ax+By+Cz+D=0
    00227     csVector2& isect,                     // intersection point
    00228     float& dist)                        // distance from u to isect
    00229   {
    00230     return SegmentPlane (uv.Start (), uv.End (), p, isect, dist);
    00231   }
    00232
    00237   static void SegmentPlaneNoTest (const csVector2& u, const csVector2& v,
    00238                      const csPlane2& p, csVector2& isect, float& dist)
    00239   {
    00240     float x,y, denom;
    00241     x = v.x-u.x;  y = v.y-u.y;
    00242     denom = p.norm.x*x + p.norm.y*y;
    00243     dist = -(p.norm*u + p.CC) / denom;
    00244     isect.x = u.x + dist*x;  isect.y = u.y + dist*y;
    00245   }
    00246
    00251   static void SegmentPlaneNoTest (const csSegment2& uv,
    00252                      const csPlane2& p, csVector2& isect, float& dist)
    00253   {
    00254     SegmentPlaneNoTest (uv.Start (), uv.End (), p, isect, dist);
    00255   }
    00256
    00263   static bool PlanePlane (const csPlane2& p1, const csPlane2& p2,
    00264                       csVector2& isect);
    00265
    00266 };
    00267
    00270 #endif // __CS_MATH2D_H__