fork download
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <cstdint>
  5.  
  6. namespace Math
  7. {
  8. template <typename Type>
  9. struct P2
  10. {
  11. union
  12. {
  13. struct
  14. {
  15. Type X, Y;
  16. };
  17. Type Data[2];
  18. };
  19. };
  20. template <typename Type>
  21. struct P3
  22. {
  23. union
  24. {
  25. struct
  26. {
  27. Type X, Y, Z;
  28. };
  29. Type Data[3];
  30. };
  31. };
  32. template <typename Type>
  33. struct P4
  34. {
  35. union
  36. {
  37. struct
  38. {
  39. Type X, Y, Z, W;
  40. };
  41. Type Data[4];
  42. };
  43. };
  44.  
  45. // Generic case, no named variables
  46. template <typename T, int Count>
  47. struct CountToVectorData
  48. {
  49. typedef struct
  50. {
  51. T Data[Count];
  52. } type;
  53. };
  54.  
  55. // Specializations...
  56. template <typename T>
  57. struct CountToVectorData <T, 2>
  58. {
  59. typedef P2<T> type;
  60. };
  61. template <typename T>
  62. struct CountToVectorData <T, 3>
  63. {
  64. typedef P3<T> type;
  65. };
  66. template <typename T>
  67. struct CountToVectorData <T, 4>
  68. {
  69. typedef P4<T> type;
  70. };
  71.  
  72. template <typename Type, size_t TemplateElementCount>
  73. class TVector :public CountToVectorData <Type, TemplateElementCount>::type
  74. {
  75. public:
  76. TVector(void)
  77. {
  78. for(unsigned cont = 0; cont < TemplateElementCount; ++cont)
  79. this->Data[cont] = 0;
  80. }
  81. TVector(Type V1, Type V2 = 0, Type V3 = 0, Type V4 = 0)
  82. {
  83. this->Data[0] = V1;
  84. if(TemplateElementCount >=2)
  85. {
  86. this->Data[1] = V2;
  87. if(TemplateElementCount >=3)
  88. {
  89. this->Data[2] = V3;
  90. if(TemplateElementCount == 4)
  91. this->Data[3] = V4;
  92. }
  93. }
  94. }
  95.  
  96. TVector(TVector <Type, TemplateElementCount-1> &OtherVector, Type LastValue = 0)
  97. {
  98. for(unsigned cont = 0; cont < TemplateElementCount-1; ++cont)
  99. {
  100. this->Data[cont] = OtherVector.Data[cont];
  101. }
  102. this->Data[TemplateElementCount-1] = LastValue;
  103. }
  104.  
  105. void SetValues(Type V1, Type V2 = 0, Type V3 = 0, Type V4 = 0)
  106. {
  107. this->Data[0] = V1;
  108. if(TemplateElementCount >=2)
  109. {
  110. this->Data[1] = V2;
  111. if(TemplateElementCount >=3)
  112. {
  113. this->Data[2] = V3;
  114. if(TemplateElementCount == 4)
  115. this->Data[3] = V4;
  116. }
  117. }
  118. }
  119.  
  120.  
  121. // TVector ( Type First, ... )
  122. // {
  123. // this->Data[0] = First;
  124. // va_list arguments;
  125. // va_start ( arguments, First );
  126. // for ( unsigned cont = 1; cont < TemplateElementCount; ++cont )
  127. // this->Data[cont] = va_arg ( arguments, Type );
  128. // va_end ( arguments );
  129. // }
  130.  
  131. double Length(void)
  132. {
  133. Type Accum = 0;
  134. for(unsigned cont = 0; cont < TemplateElementCount; ++cont)
  135. Accum += (this->Data[cont]*this->Data[cont]);
  136. return sqrt(Accum);
  137. }
  138.  
  139. void SetAllElementsTo(Type In_Value)
  140. {
  141. for(unsigned cont = 0; cont < TemplateElementCount; ++cont)
  142. this->Data[cont] = In_Value;
  143. }
  144. void Normalize(void)
  145. {
  146. double VecLength = Length();
  147. if(VecLength == 0) return;
  148. for(unsigned cont = 0; cont < TemplateElementCount; ++cont) \
  149. this->Data[cont] /= VecLength ;
  150. }
  151. Type operator [](const int Index) const
  152. {
  153. return this->Data[Index];
  154. }
  155. Type &operator [](const int Index)
  156. {
  157. return this->Data[Index];
  158. }
  159. /*
  160. TVector <Type,TemplateElementCount> operator - ( const TVector <Type,TemplateElementCount> &Second )
  161. {
  162. TVector <Type,TemplateElementCount> Result;
  163. for ( unsigned cont = 0; cont < 3; ++cont )
  164. Result.Data[cont] = this->Data[cont] - Second.Data[cont];
  165. return Result;
  166. }*/
  167. };
  168. }
  169.  
  170. template <typename Type, unsigned TemplateElementCount>
  171. Math::TVector <Type,TemplateElementCount> operator - (Math::TVector <Type,TemplateElementCount> &First, Math::TVector <Type,TemplateElementCount> &Second)
  172. {
  173. Math::TVector <Type,TemplateElementCount> Result;
  174. for(unsigned cont = 0; cont < TemplateElementCount; ++cont)
  175. Result.Data[cont] = First.Data[cont] - Second.Data[cont];
  176. return Result;
  177. }
  178.  
  179. int main(int argc, char **argv)
  180. {
  181. Math::TVector <int32_t, 3> Vector1, Vector2;
  182. Vector1.X = 1;
  183. Vector1.Y = 2;
  184. Vector1.Z = 3;
  185. Vector2.SetValues(0,0,1);
  186. for(unsigned cont = 0; cont < 3; ++cont)
  187. {
  188. std::cout << " -> " << Vector1.Data[cont] << std::endl;
  189. }
  190.  
  191. Vector1 = Vector1 - Vector2;
  192. for(unsigned cont = 0; cont < 3; ++cont)
  193. {
  194. std::cout << " -> " << Vector1.Data[cont] << std::endl;
  195. }
  196.  
  197. return 0;
  198. }
  199.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
 -> 1
 -> 2
 -> 3
 -> 1
 -> 2
 -> 2