POINT.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name: Command & Conquer *
  23. * *
  24. * Archive: /Sun/Point.h *
  25. * *
  26. * Author: Joe_b *
  27. * *
  28. * Modtime: 2/02/98 10:09a *
  29. * *
  30. * Revision: 24 *
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #pragma once
  36. #ifndef POINT_H
  37. #define POINT_H
  38. #include <math.h>
  39. //#include "always.h"
  40. //#include "cctypes.h"
  41. //#ifdef __cplusplus
  42. //extern "C"{
  43. //#endif
  44. //#pragma pack(1)
  45. typedef struct Point2DStruct
  46. {
  47. int X;
  48. int Y;
  49. } Point2DStruct;
  50. //#pragma pack()
  51. //#ifdef __cplusplus
  52. //}
  53. //#endif
  54. template<class T> class TRect;
  55. /***********************************************************************************************
  56. ** This class describes a point in 2 dimensional space using arbitrary
  57. ** components. The interpretation of which is outside the scope
  58. ** of this class. This class is the successor to the old style COORDINATE
  59. ** and CELL types but also serves anywhere an X and Y value are treated
  60. ** as a logical object (e.g., pixel location).
  61. */
  62. template<class T>
  63. class TPoint2D {
  64. public:
  65. TPoint2D(void) {} // Default constructor does nothing by design.
  66. TPoint2D(T x, T y) : X(x), Y(y) {}
  67. // Equality comparison operators.
  68. bool operator == (TPoint2D<T> const & rvalue) const {return(X==rvalue.X && Y==rvalue.Y);}
  69. bool operator != (TPoint2D<T> const & rvalue) const {return(X!=rvalue.X || Y!=rvalue.Y);}
  70. // Addition and subtraction operators.
  71. TPoint2D<T> const & operator += (TPoint2D<T> const & rvalue) {X += rvalue.X;Y += rvalue.Y;return(*this);}
  72. TPoint2D<T> const & operator -= (TPoint2D<T> const & rvalue) {X -= rvalue.X;Y -= rvalue.Y;return(*this);}
  73. TPoint2D<T> const operator - (TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(T(X - rvalue.X), T(Y - rvalue.Y)));}
  74. TPoint2D<T> const operator + (TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(T(X + rvalue.X), T(Y + rvalue.Y)));}
  75. // Scalar multiplication and division.
  76. TPoint2D<T> const operator * (T rvalue) const {return(TPoint2D<T>(T(X * rvalue), T(Y * rvalue)));}
  77. TPoint2D<T> const & operator *= (T rvalue) {X *= rvalue; Y *= rvalue;return(*this);}
  78. TPoint2D<T> const operator / (T rvalue) const {if (rvalue == T(0)) return(TPoint2D<T>(0,0));return(TPoint2D<T>(T(X / rvalue), T(Y / rvalue)));}
  79. TPoint2D<T> const & operator /= (T rvalue) {if (rvalue != T(0)) {X /= rvalue;Y /= rvalue;}return(*this);}
  80. // Dot and cross product.
  81. TPoint2D<T> const operator * (TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(T(X * rvalue.X), T(Y * rvalue.Y)));}
  82. T Dot_Product(TPoint2D<T> const & rvalue) const {return((T(X * rvalue.X + Y * rvalue.Y)));}
  83. TPoint2D<T> const Cross_Product(TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(T(Y - rvalue.Y), T(rvalue.X - X)));}
  84. // Negation operator -- simple and effective
  85. TPoint2D<T> const operator - (void) const {return(TPoint2D<T>(T(-X), T(-Y)));}
  86. // Vector support functions.
  87. T Length(void) const {return(T(sqrt(double(X*X + Y*Y))));}
  88. TPoint2D<T> const Normalize(void) const {
  89. double len = sqrt(X*X + Y*Y);
  90. if (len != 0.0) {
  91. return(TPoint2D<T>((T)(X / len), (T)(Y / len)));
  92. } else {
  93. return(*this);
  94. }
  95. }
  96. // Find distance between points.
  97. T Distance_To(TPoint2D<T> const & point) const {return((*this - point).Length());}
  98. public:
  99. T X;
  100. T Y;
  101. };
  102. /***********************************************************************************************
  103. ** This typedef provides an uncluttered type name for use by simple integer points.
  104. */
  105. class Point2D : public TPoint2D<int>
  106. {
  107. public:
  108. Point2D(void) {} // Default constructor does nothing by design.
  109. Point2D(int x, int y) : TPoint2D<int>(x, y) {}
  110. Point2D(Point2DStruct const & rvalue) : TPoint2D<int>(rvalue.X, rvalue.Y) {}
  111. Point2D(TPoint2D<int> const & rvalue) : TPoint2D<int>(rvalue) {}
  112. operator Point2DStruct (void) const {Point2DStruct pt;pt.X = X;pt.Y = Y;return(pt);}
  113. Point2D const & operator += (Point2D const & rvalue) {X += rvalue.X;Y += rvalue.Y;return(*this);}
  114. Point2D const & operator -= (Point2D const & rvalue) {X -= rvalue.X;Y -= rvalue.Y;return(*this);}
  115. Point2D const operator - (Point2D const & rvalue) const {return(Point2D(int(X - rvalue.X), int(Y - rvalue.Y)));}
  116. Point2D const operator + (Point2D const & rvalue) const {return(Point2D(int(X + rvalue.X), int(Y + rvalue.Y)));}
  117. };
  118. template<class T>
  119. T Distance(TPoint2D<T> const & point1, TPoint2D<T> const & point2)
  120. {
  121. return((point1 - point2).Length());
  122. }
  123. template<class T>
  124. TPoint2D<T> const Cross_Product(TPoint2D<T> const & lvalue, TPoint2D<T> const & rvalue)
  125. {
  126. return(lvalue.Cross_Product(rvalue));
  127. }
  128. /***********************************************************************************************
  129. ** This describes a point in 3 dimensional space using arbitrary
  130. ** components. This is the successor to the COORDINATE type for those
  131. ** times when height (Z axis) needs to be tracked.
  132. **
  133. ** Notice that it is NOT implemented as a virtually derived class. This
  134. ** is for efficiency reasons. This class chooses to be smaller and faster at the
  135. ** expense of polymorphism. However, since it is publicly derived, inheritance is
  136. ** the next best thing.
  137. */
  138. template<class T>
  139. class TPoint3D : public TPoint2D<T> {
  140. typedef TPoint2D<T> BASECLASS;
  141. public:
  142. TPoint3D(void) {} // Default constructor does nothing by design.
  143. TPoint3D(T x, T y, T z) : BASECLASS(x, y), Z(z) {}
  144. TPoint3D(BASECLASS const & rvalue, T z /*= 0*/) : BASECLASS(rvalue), Z(z) {}
  145. // Equality comparison operators.
  146. bool operator == (TPoint3D<T> const & rvalue) const {return(X==rvalue.X && Y==rvalue.Y && Z==rvalue.Z);}
  147. bool operator != (TPoint3D<T> const & rvalue) const {return(X!=rvalue.X || Y!=rvalue.Y || Z!=rvalue.Z);}
  148. // Addition and subtraction operators.
  149. TPoint3D<T> const & operator += (TPoint3D<T> const & rvalue) {X += rvalue.X;Y += rvalue.Y;Z += rvalue.Z;return(*this);}
  150. TPoint2D<T> const & operator += (TPoint2D<T> const & rvalue) {BASECLASS::operator += (rvalue);return(*this);}
  151. TPoint3D<T> const & operator -= (TPoint3D<T> const & rvalue) {X -= rvalue.X;Y -= rvalue.Y;Z -= rvalue.Z;return(*this);}
  152. TPoint2D<T> const & operator -= (TPoint2D<T> const & rvalue) {BASECLASS::operator -= (rvalue);return(*this);}
  153. TPoint3D<T> const operator - (TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X - rvalue.X, Y - rvalue.Y, Z - rvalue.Z));}
  154. TPoint3D<T> const operator - (TPoint2D<T> const & rvalue) const {return(TPoint3D<T>(X - rvalue.X, Y - rvalue.Y, Z));}
  155. TPoint3D<T> const operator + (TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X + rvalue.X, Y + rvalue.Y, Z + rvalue.Z));}
  156. TPoint3D<T> const operator + (TPoint2D<T> const & rvalue) const {return(TPoint3D<T>(X + rvalue.X, Y + rvalue.Y, Z));}
  157. // Scalar multiplication and division.
  158. TPoint3D<T> const operator * (T rvalue) const {return(TPoint3D<T>(X * rvalue, Y * rvalue, Z * rvalue));}
  159. TPoint3D<T> const & operator *= (T rvalue) {X *= rvalue;Y *= rvalue;Z *= rvalue;return(*this);}
  160. TPoint3D<T> const operator / (T rvalue) const {if (rvalue == T(0)) return(TPoint3D<T>(0,0,0));return(TPoint3D<T>(X / rvalue, Y / rvalue, Z / rvalue));}
  161. TPoint3D<T> const & operator /= (T rvalue) {if (rvalue != T(0)) {X /= rvalue;Y /= rvalue;Z /= rvalue;}return(*this);}
  162. // Dot and cross product.
  163. TPoint3D<T> const operator * (TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X * rvalue.X, Y * rvalue.Y, Z * rvalue.Z));}
  164. T Dot_Product(TPoint3D<T> const & rvalue) const {return(T(X * rvalue.X + Y * rvalue.Y + Z * rvalue.Z));}
  165. TPoint3D<T> const Cross_Product(TPoint3D<T> const & rvalue) const {return TPoint3D<T>(Y * rvalue.Z - Z * rvalue.Y, Z * rvalue.X - X * rvalue.Z, X * rvalue.Y - Y * rvalue.X);}
  166. // Negation operator -- simple and effective
  167. TPoint3D<T> const operator - (void) const {return(TPoint3D<T>(-X, -Y, -Z));}
  168. // Vector support functions.
  169. T Length(void) const {return(T(sqrt(double(X*X + Y*Y + Z*Z))));}
  170. TPoint3D<T> const Normalize(void) const {
  171. double len = sqrt(X*X + Y*Y + Z*Z);
  172. if (len != 0.0) {
  173. return(TPoint3D<T>((T)(X / len), (T)(Y / len), (T)(Z / len)));
  174. } else {
  175. return(*this);
  176. }
  177. }
  178. public:
  179. /*
  180. ** The Z component of this point.
  181. */
  182. T Z;
  183. };
  184. /***********************************************************************************************
  185. ** This typedef provides a simple uncluttered type name for use by
  186. ** integer 3D points.
  187. */
  188. typedef TPoint3D<int> Point3D;
  189. template<class T>
  190. TPoint3D<T> const Cross_Product(TPoint3D<T> const & lvalue, TPoint3D<T> const & rvalue)
  191. {
  192. return(lvalue.Cross_Product(rvalue));
  193. }
  194. #endif