Guest User

Untitled

a guest
Jan 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. //Point.hpp
  2.  
  3. #ifndef POINT_HPP_INCLUDED
  4. #define POINT_HPP_INCLUDED
  5. namespace Ignatus{
  6. /// Point is for using velocities and angle movement. Also used for coords.
  7. /**
  8. This basicly as two values or a given type; x and y. This allows you to return two variables of the same type rather than only one value.
  9. Also has a few funtions for converting velocity to angles and speed.
  10. Supports basic math, like adding, also allows to check if Points have the same value.
  11. */
  12. template <typename T, typename S=T,typename U=T>
  13. class Point{
  14. public:
  15. /// This is the first of the the pair of coords, called x because of standard coords.
  16. /**
  17. This is also the type returned for funtions that return a spacific number.
  18. */
  19. T x;
  20. /// This is the first of the the pair of coords, called x because of standard coords.
  21. S y;
  22. /// This is the first of the the pair of coords, called x because of standard coords.
  23. U z;
  24. Point<T,S,U>();
  25. Point<T,S,U>(T X);
  26. Point<T,S,U>(T X,S Y);
  27. Point<T,S,U>(T X,S Y,U Z);
  28. #ifdef SFML_SYSTEM_HPP
  29. Point<T,S,U>(sf::Vector2<T> XY);
  30. Point<T,S,U>(sf::Vector3<T> XYZ);
  31.  
  32. sf::Vector2<T> TwoVector();
  33. sf::Vector3<T> ThreeVector();
  34. #ifdef SFML_GRAPHICS_HPP
  35. sf::Rect<T> TwoRect(Point<T,S,U> p2);
  36. static sf::Rect<T> GetRectofPoints(std::vector<Point<T,S,U> >* ps);
  37. #endif
  38. #endif
  39. ~Point<T,S,U>();
  40.  
  41. Point<T,S,U> TwoAngle();
  42. Point<T,S,U> TwoVelocity();
  43. Point<T,S,U> GetMidPoint(Point<T,S,U> p);
  44. Point<T,S,U> GetNearest(std::vector<Point<T,S,U> >* ps);
  45.  
  46. T Get_Distance(Point<T,S,U> p);
  47. T Get_Angle(Point<T,S,U> p);
  48. T GetMinMaxX(Point<T,S,U> p,bool min=true);
  49. S GetMinMaxY(Point<T,S,U> p,bool min=true);
  50. U GetMinMaxZ(Point<T,S,U> p,bool min=true);
  51.  
  52. static Point<T,S,U> GetMiddleofPoints(std::vector<Point<T,S,U> >* ps);
  53. bool operator==(Point<T,S,U> param);
  54. bool operator!=(Point<T,S,U> param);
  55. Point<T,S,U> operator+=(Point<T,S,U> param);
  56. Point<T,S,U> operator+(Point<T,S,U> param);
  57. Point<T,S,U> operator-=(Point<T,S,U> param);
  58. Point<T,S,U> operator-(Point<T,S,U> param);
  59. Point<T,S,U> operator*=(Point<T,S,U> param);
  60. Point<T,S,U> operator*(Point<T,S,U> param);
  61. Point<T,S,U> operator/=(Point<T,S,U> param);
  62. Point<T,S,U> operator/(Point<T,S,U> param);
  63. };
  64.  
  65. typedef Point<int> Pointi;
  66. typedef Point<float> Pointf;
  67. typedef Point<char> Pointc;
  68.  
  69. };// namespace Ignatus
  70. #include "Point.inl"
  71.  
  72. #endif // POINT_HPP_INCLUDED
  73.  
  74.  
  75. //Point.inl
  76.  
  77.  
  78. #define tp template<typename T,typename S,typename U>
  79. #define a2r 0.017453292519943295769236907684886
  80. #define r2a 57.295779513082320876798154814105
  81.  
  82. namespace Ignatus{
  83.  
  84. tp
  85. Point<T,S,U>::Point()
  86. {
  87. x=0;
  88. y=0;
  89. z=0;
  90. }
  91. tp
  92. Point<T,S,U>::Point(T X)
  93. {
  94. x=X;
  95. y=X;
  96. z=X;
  97. }
  98. tp
  99. Point<T,S,U>::Point(T X,S Y)
  100. {
  101. x=X;
  102. y=Y;
  103. z=0;
  104. }
  105. tp
  106. Point<T,S,U>::Point(T X,S Y,U Z)
  107. {
  108. x=X;
  109. y=Y;
  110. z=Z;
  111. }
  112. #ifdef SFML_SYSTEM_HPP
  113. tp
  114. Point<T,S,U>::Point(sf::Vector2<T> XY)
  115. {
  116. x=XY.x;
  117. y=XY.y;
  118. z=0;
  119. }
  120. tp
  121. Point<T,S,U>::Point(sf::Vector3<T> XYZ)
  122. {
  123. x=XYZ.x;
  124. y=XYZ.y;
  125. z=XYZ.z;
  126. }
  127. tp
  128. sf::Vector2<T> Point<T,S,U>::TwoVector(){
  129. return sf::Vector2<T>(x,y);
  130. }
  131. tp
  132. sf::Vector3<T> Point<T,S,U>::ThreeVector(){
  133. return sf::Vector3<T>(x,y,z);
  134. }
  135. #ifdef SFML_GRAPHICS_HPP
  136. tp
  137. sf::Rect<T> Point<T,S,U>::TwoRect(Point<T,S,U> p2){
  138. sf::Rect<T> ret;
  139. ret.Left=GetMinMaxX(p2);
  140. ret.Right=GetMinMaxX(p2,false);
  141. ret.Top=GetMinMaxY(p2);
  142. ret.Bottom=GetMinMaxY(p2,false);
  143. return ret;
  144. }
  145. tp
  146. sf::Rect<T> Point<T,S,U>::GetRectofPoints(std::vector<Point<T,S,U> >* ps){
  147. if(ps->size()>1){
  148. Point<Point<T>,Point<S> > ret;
  149. ret.x.x=(*ps)[0].x;
  150. ret.x.y=(*ps)[1].x;
  151. ret.y.x=(*ps)[0].y;
  152. ret.y.y=(*ps)[1].y;
  153. for(unsigned int a=0;a<ps->size();a++){
  154. if((*ps)[a].x<ret.x.x)ret.x.x=(*ps)[a].x;
  155. if((*ps)[a].x>ret.x.y)ret.x.y=(*ps)[a].x;
  156. if((*ps)[a].y<ret.y.x)ret.y.x=(*ps)[a].y;
  157. if((*ps)[a].y>ret.y.y)ret.y.y=(*ps)[a].y;
  158. }
  159. return sf::Rect<T>(ret.x.x,ret.x.y,ret.y.x,ret.y.y);
  160. }else{
  161. return sf::Rect<T>(0,0,0,0);
  162. }
  163. }
  164. #endif
  165. #endif
  166. tp
  167. Point<T,S,U>::~Point(){}
  168. tp
  169. Point<T,S,U> Point<T,S,U>::TwoAngle()
  170. {
  171. ///convert Z to zv and hor angle
  172. return Point<T,S,U>(Get_Distance(Point<T,S,U>()),Get_Angle(Point<T,S,U>())-180,0);
  173. }
  174. tp
  175. Point<T,S,U> Point<T,S,U>::TwoVelocity()
  176. {
  177. ///convert Z to zv and hor angle
  178. T X=x*cos(y*a2r);
  179. S Y=x*sin(y*a2r);
  180. return Point<T,S,U>(X,Y,0);
  181. }
  182. tp
  183. Point<T,S,U> Point<T,S,U>::GetNearest(std::vector<Point<T,S,U> >* ps){
  184. Point<T,S,U> ret=(*ps)[0];
  185. for(unsigned int a=0;a<ps->size();a++){
  186. if(ret.Get_Distance((*ps)[a])<Get_Distance(ret)){
  187. ret=(*ps)[a];
  188. }
  189. }
  190. return ret;
  191. }
  192. tp
  193. T Point<T,S,U>::Get_Distance(Point<T,S,U> p){
  194. return T(sqrt(T(x-p.x)*T(x-p.x)+T(y-p.y)*T(y-p.y)));
  195. }
  196. tp
  197. T Point<T,S,U>::Get_Angle(Point<T,S,U> p){
  198. return T(atan2(T(p.y-y),T(p.x-x))*r2a);
  199. }
  200. tp
  201. T Point<T,S,U>::GetMinMaxX(Point<T,S,U> p,bool min){
  202. if(x<p.x)if(min)return x;else return p.x;else if(!min)return x;else return p.x;
  203. }
  204. tp
  205. S Point<T,S,U>::GetMinMaxY(Point<T,S,U> p,bool min){
  206. if(y<p.y)if(min)return y;else return p.y;else if(!min)return y;else return p.y;
  207. }
  208. tp
  209. U Point<T,S,U>::GetMinMaxZ(Point<T,S,U> p,bool min){
  210. if(z<p.z)if(min)return z;else return p.z;else if(!min)return z;else return p.z;
  211. }
  212. tp
  213. Point<T,S,U> Point<T,S,U>::GetMidPoint(Point<T,S,U> p){
  214. return (*this+p)/Point<T,S,U>(2);
  215. }
  216. tp
  217. Point<T,S,U> Point<T,S,U>::GetMiddleofPoints(std::vector<Point<T,S,U> >* ps){
  218. T X=0;
  219. S Y=0;
  220. U Z=0;
  221. for(unsigned int a=0;a<ps->size();a++){
  222. X+=(*ps)[a].x;
  223. Y+=(*ps)[a].y;
  224. Z+=(*ps)[a].z;
  225. }
  226. X/=ps->size();
  227. Y/=ps->size();
  228. Z/=ps->size();
  229. return Point<T,S,U>(X,Y,Z);
  230. }
  231. ///OLD FUNCTIONS
  232. tp
  233. Point<T,S,U> Point<T,S,U>::operator+=(Point<T,S,U> param){
  234. *this=*this+param;
  235. return *this;
  236. }
  237. tp
  238. Point<T,S,U> Point<T,S,U>::operator-=(Point<T,S,U> param){
  239. *this=*this-param;
  240. return *this;
  241. }
  242. tp
  243. Point<T,S,U> Point<T,S,U>::operator*=(Point<T,S,U> param) {
  244. *this=*this*param;
  245. return *this;
  246. }
  247. tp
  248. Point<T,S,U> Point<T,S,U>::operator/=(Point<T,S,U> param) {
  249. *this=*this/param;
  250. return *this;
  251. }
  252. tp
  253. Point<T,S,U> Point<T,S,U>::operator+(Point<T,S,U> param)
  254. {
  255. return(Point<T,S,U>(x+param.x,y+param.y,z+param.z));
  256. }
  257. tp
  258. Point<T,S,U> Point<T,S,U>::operator-(Point<T,S,U> param)
  259. {
  260. return(Point<T,S,U>(x-param.x,y-param.y,z-param.z));
  261. }
  262. tp
  263. Point<T,S,U> Point<T,S,U>::operator*(Point<T,S,U> param)
  264. {
  265. return(Point<T,S,U>(x*param.x,y*param.y,z*param.z));
  266. }
  267. tp
  268. Point<T,S,U> Point<T,S,U>::operator/(Point<T,S,U> param)
  269. {
  270. Point<T,S,U> ret;
  271. if(param.x)ret.x=0;else ret.x=x/param.x;
  272. if(param.y)ret.y=0;else ret.y=y/param.y;
  273. if(param.z)ret.z=0;else ret.z=z/param.z;
  274. return(ret);
  275. }
  276. tp
  277. bool Point<T,S,U>::operator==(Point<T,S,U> param)
  278. {
  279. return (x==param.x and y==param.y and z==param.z);
  280. }
  281.  
  282. tp
  283. bool Point<T,S,U>::operator!=(Point<T,S,U> param)
  284. {
  285. return !(*this==param);
  286. }
  287.  
  288. }
Add Comment
Please, Sign In to add comment