Crystal Space: csgeom/poly2d.h Source File (Crystal Space Public API Reference)

| ## Public API Reference

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

- File List
- File Members

# csgeom/poly2d.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 1998,2000 by Jorrit Tyberghein
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_POLY2D_H__
    00020 #define __CS_POLY2D_H__
    00021
    00022 #include "csextern.h"
    00023
    00024 #include "csgeom/vector2.h"
    00032 class csBox2;
    00033 class csPlane2;
    00034
    00035 struct iClipper2D;
    00036
    00040 class CS_CRYSTALSPACE_EXPORT csPoly2D
    00041 {
    00042 protected:
    00044   csVector2* vertices;
    00046   size_t num_vertices;
    00048   size_t max_vertices;
    00049 public:
    00053   csPoly2D (size_t start_size = 10);
    00054
    00056   csPoly2D (const csPoly2D& copy);
    00057
    00059   ~csPoly2D ();
    00060
    00062   csPoly2D& operator= (const csPoly2D& other);
    00063
    00067   void MakeEmpty ();
    00068
    00072   size_t GetVertexCount () const { return num_vertices; }
    00073
    00077   csVector2* GetVertices () { return vertices; }
    00078
    00082   const csVector2* GetVertices () const { return vertices; }
    00083
    00087   csVector2* GetVertex (size_t i)
    00088   {
    00089     if (i>=num_vertices) return 0;
    00090     return &vertices[i];
    00091   }
    00092
    00096   csVector2& operator[] (size_t i)
    00097   {
    00098     CS_ASSERT (i < num_vertices);
    00099     return vertices[i];
    00100   }
    00101
    00105   const csVector2& operator[] (size_t i) const
    00106   {
    00107     CS_ASSERT (i < num_vertices);
    00108     return vertices[i];
    00109   }
    00110
    00114   csVector2* GetFirst ()
    00115   { if (num_vertices<=0) return 0;  else return vertices; }
    00116
    00120   csVector2* GetLast ()
    00121   { if (num_vertices<=0) return 0;  else return &vertices[num_vertices-1]; }
    00122
    00126   bool In (const csVector2& v);
    00127
    00131   static bool In (csVector2* poly, size_t num_poly, const csVector2& v);
    00132
    00136   void MakeRoom (size_t new_max);
    00137
    00141   void SetVertexCount (size_t n)
    00142   {
    00143     MakeRoom (n);
    00144     num_vertices = n;
    00145   }
    00146
    00151   size_t AddVertex (const csVector2& v) { return AddVertex (v.x, v.y); }
    00152
    00157   size_t AddVertex (float x, float y);
    00158
    00163   void SetVertices (csVector2 const* v, size_t num)
    00164   {
    00165     MakeRoom (num);
    00166     memcpy (vertices, v, (num_vertices = num) * sizeof (csVector2));
    00167   }
    00168
    00177   bool ClipAgainst (iClipper2D* view);
    00178
    00187   void Intersect (const csPlane2& plane, csPoly2D& left,
    00188         csPoly2D& right) const;
    00189
    00194   void ClipPlane (const csPlane2& plane, csPoly2D& right) const;
    00195
    00204   void ExtendConvex (const csPoly2D& other, size_t this_edge);
    00205
    00209   float GetSignedArea();
    00210
    00216   void Random (size_t num, const csBox2& max_bbox);
    00217 };
    00218
    00224 class csPoly2DFactory
    00225 {
    00226 public:
    00227   // Needed for GCC4. Otherwise emits a flood of "virtual functions but
    00228   // non-virtual destructor" warnings.
    00229   virtual ~csPoly2DFactory() {}
    00230
    00232    CS_DECLARE_STATIC_CLASSVAR(sharedFactory,SharedFactory,csPoly2DFactory)
    00233
    00234
    00235   virtual csPoly2D* Create ()
    00236   {
    00237     csPoly2D* p = new csPoly2D ();
    00238     return p;
    00239   }
    00240 };
    00241
    00244 #endif // __CS_POLY2D_H__