home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / math3d.h < prev    next >
C/C++ Source or Header  |  1998-01-15  |  3KB  |  144 lines

  1. #ifndef MATH3D_H
  2. #define MATH3D_H
  3.  
  4. #define POLY_IN 1
  5. #define POLY_ON 0
  6. #define POLY_OUT -1
  7.  
  8. #ifndef DEF_H
  9. #include "def.h"
  10. #endif
  11.  
  12. class Vector2;
  13. class Vector3;
  14. class Box;
  15.  
  16. class Tables
  17. {
  18. private:
  19.  
  20. public:
  21.   Tables ();
  22. };
  23.  
  24. extern Tables tables;
  25.  
  26. class Segment
  27. {
  28. public:
  29.   static int intersect_segments (
  30.     Vector2& a, Vector2& b, // First segment
  31.     Vector2& c, Vector2& d, // Second segment
  32.     Vector2& isect,         // Intersection vertex
  33.     float* rp);            // 'r' index value
  34.   static int intersect_segment_line (
  35.     Vector2& a, Vector2& b, // First segment
  36.     Vector2& c, Vector2& d, // Second line
  37.     Vector2& isect,         // Intersection vertex
  38.     float* rp);            // 'r' index value
  39.  
  40.   static float intersect_z_plane_3d (
  41.                      float z,            // Z plane coordinate
  42.                      Vector3& a, Vector3& b,    // Segment
  43.                      Vector3& i);        // Intersection vertex
  44.  
  45. };
  46.  
  47. class Vector3
  48. {
  49. public:
  50.   float x, y, z;
  51.  
  52.   Vector3 () { }
  53.   Vector3 (float x, float y, float z) { Vector3::x = x; Vector3::y = y; Vector3::z = z; }
  54.   void dump (char* name);
  55.  
  56.   Vector3& operator+= (Vector3& v);
  57.   Vector3& operator-= (Vector3& v);
  58.  
  59.   void save (FILE* fp, int indent);
  60.   void load (char** buf);
  61.  
  62.   static void between (Vector3& v1, Vector3& v2, Vector3& v, float pct, float wid);
  63. };
  64.  
  65. class Vector2
  66. {
  67. public:
  68.   float x, y;
  69.  
  70.   Vector2 () { }
  71.   Vector2 (float x, float y) { Vector2::x = x; Vector2::y = y; }
  72.   void dump (char* name);
  73.  
  74.   // Return -1 if vector is left of 'v1-v2'. 1 is vector is right of 'v1-v2'
  75.   // or 0 is vector is on 'v1-v2'.
  76.   int which_side_2d (Vector2& v1, Vector2& v2);
  77.  
  78.   // Return POLY_IN, POLY_OUT, or POLY_ON for this vector with respect
  79.   // to the given polygon. The polygon is given as an array of 2D vectors
  80.   // with a bounding box.
  81.   int in_poly_2d (Vector2* P, int n, Box* bounding_box);
  82.  
  83.   // Returns twice the signed area of the triangle determined by a,b,c,
  84.   // positive if a,b,c are oriented ccw, and negative if cw.
  85.   static float Area2 (float ax, float ay, float bx, float by, float cx, float cy);
  86.  
  87.   // Returns true iff c is strictly to the right of the directed
  88.   // line through a to b.
  89.   static int Right (float ax, float ay, float bx, float by, float cx, float cy);
  90.  
  91.   // Returns true iff c is strictly to the left of the directed
  92.   // line through a to b.
  93.   static int Left (float ax, float ay, float bx, float by, float cx, float cy);
  94. };
  95.  
  96. class Box
  97. {
  98. public:
  99.   float minx, miny, maxx, maxy;
  100.  
  101.   int in (float x, float y);
  102.   int overlap (Box* box);
  103.   void start_bounding_box ();
  104.   void add_bounding_vertex (float x, float y);
  105.   void add_bounding_vertex (Vector2& v) { add_bounding_vertex (v.x, v.y); }
  106.  
  107.   void dump ();
  108. };
  109.  
  110. class Matrix3
  111. {
  112. public:
  113.   float m11, m12, m13;
  114.   float m21, m22, m23;
  115.   float m31, m32, m33;
  116.  
  117. public:
  118.   Matrix3 ();
  119.   Matrix3 (float m11, float m12, float m13,
  120.          float m21, float m22, float m23,
  121.          float m31, float m32, float m33);
  122.   ~Matrix3 ();
  123.  
  124.   static void init ();
  125.  
  126.   Matrix3& operator+= (Matrix3& m);
  127.   Matrix3& operator-= (Matrix3& m);
  128.   Matrix3& operator*= (Matrix3& m);
  129.   Matrix3& operator*= (float s);
  130.   void transpose ();
  131.   void inverse ();
  132.   float determinant ();
  133.   void transform (Vector3& f, Vector3& t);
  134.   void transform (float x, float y, float z, Vector3& t);
  135.   void identity ();
  136.  
  137.   void dump (char* name);
  138.   void save (FILE* fp, int indent);
  139.   void load (char** buf);
  140. };
  141.  
  142. #endif /*MATH3D_H*/
  143.  
  144.