CrystalSpace

Public API Reference

csgeom/plane2.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_PLANE2_H__
00021 #define __CS_PLANE2_H__
00022 
00023 
00024 #include "csextern.h"
00025 
00026 #include "csgeom/segment.h"
00027 #include "csgeom/vector2.h"
00028 
00036 enum 
00037 {
00038   CS_POLY_IN = 1,
00039   CS_POLY_ON = 0,
00040   CS_POLY_OUT = -1
00041 };
00042 
00048 class csPlane2
00049 {
00050 public:
00052   csVector2 norm;
00053 
00055   float CC;
00056 
00058   csPlane2 () : norm (0,1), CC (0) {}
00059 
00061   csPlane2 (const csVector2& plane_norm, float c=0)
00062         : norm (plane_norm), CC (c) {}
00063 
00065   csPlane2 (float a, float b, float c=0) : norm (a,b), CC (c) {}
00066 
00068   inline void Set (const csVector2& v1, const csVector2& v2)
00069   {
00070     norm.x = v2.y-v1.y;
00071     norm.y = -(v2.x-v1.x);
00072     CC = - (v2 * norm);
00073   }
00074 
00076   inline void Set (const csSegment2& s)
00077   {
00078     Set (s.Start (), s.End ());
00079   }
00080 
00082   csPlane2 (const csVector2& v1, const csVector2& v2)
00083   {
00084     Set (v1, v2);
00085   }
00086 
00088   csPlane2 (const csSegment2& s)
00089   {
00090     Set (s);
00091   }
00092 
00094   inline csVector2& Normal () { return norm; }
00095 
00097   inline csVector2 GetNormal () const { return norm; }
00098 
00100   inline float A () const { return norm.x; }
00102   inline float B () const { return norm.y; }
00104   inline float C () const { return CC; }
00105 
00107   inline float& A () { return norm.x; }
00109   inline float& B () { return norm.y; }
00111   inline float& C () { return CC; }
00112 
00114   inline void Set (float a, float b, float c)
00115   { norm.x = a; norm.y = b; CC = c; }
00116 
00118   inline float Classify (const csVector2& pt) const { return norm*pt+CC; }
00119 
00121   static float Classify (float A, float B, float C,
00122                          const csVector2& pt)
00123   { return A*pt.x + B*pt.y + C; }
00124 
00130   inline float Distance (const csVector2& pt) const
00131   { return ABS (Classify (pt)); }
00132 
00139   inline float SquaredDistance (const csVector2& pt) const
00140   {
00141     return Classify (pt) / norm.SquaredNorm ();
00142   }
00143 
00145   inline void Invert () { norm = -norm;  CC = -CC; }
00146 
00148   inline void Normalize ()
00149   {
00150     float f = norm.Norm ();
00151     if (f) { norm /= f;  CC /= f; }
00152   }
00153 };
00154 
00157 #endif // __CS_PLANE2_H__

Generated for Crystal Space 1.0.2 by doxygen 1.4.7