Advertisement
Armandur

Coordinate class

May 24th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. Coordinate.h
  2.  
  3. #ifndef CORDINATE_H
  4. #define CORDINATE_H
  5.  
  6. #include <math.h>
  7.  
  8. class Coordinate
  9. {
  10.     public:
  11.         Coordinate(void);
  12.  
  13.         Coordinate(int x, int y);
  14.         Coordinate(int x, int y, int z);
  15.  
  16.         Coordinate(double x, double y);
  17.         Coordinate(double x, double y, double z);
  18.  
  19.         Coordinate(Coordinate &c);
  20.  
  21.         ~Coordinate(void);
  22.  
  23.         Coordinate &operator=(Coordinate &c);
  24.         bool operator==(Coordinate &c);
  25.  
  26.         double operator[](int i);
  27.  
  28.         double getX();
  29.         double getY();
  30.         double getZ();
  31.  
  32.         void setPos(int x, int y);
  33.         void setPos(int x, int y, int z);
  34.  
  35.         void setPos(double x, double y);
  36.         void setPos(double x, double y, double z);
  37.  
  38.         void deltaPos(int x, int y);
  39.         void deltaPos(int x, int y, int z);
  40.  
  41.         void deltaPos(double x, double y);
  42.         void deltaPos(double x, double y, double z);
  43.  
  44.         double inline distanceTo(Coordinate &c)
  45.         {
  46.             double dx = x - c.getX();
  47.             double dy = y - c.getY();
  48.             double dz = z - c.getZ();
  49.  
  50.             return sqrt(dx * dx + dy * dy + dz * dz);
  51.         }
  52.  
  53.     protected:
  54.         double x, y, z;
  55.  
  56.     private:
  57.         double epsilon;
  58. };
  59.  
  60. #endif
  61.  
  62. Coordinate.cpp
  63. #include "Coordinate.h"
  64. #include <iostream>
  65.  
  66.  
  67. Coordinate::Coordinate(void)
  68. {
  69.     x = y = z = 0;
  70.  
  71.     epsilon = 0.001
  72. }
  73.  
  74. Coordinate::Coordinate(int x, int y)
  75. {
  76.     this->x = x;
  77.     this->y = y;
  78.     this->z = 0;
  79.  
  80.     epsilon = 0.001
  81. }
  82.  
  83. Coordinate::Coordinate(int x, int y, int z)
  84. {
  85.     this->x = x;
  86.     this->y = y;
  87.     this->z = z;
  88.  
  89.     epsilon = 0.001
  90. }
  91.  
  92. Coordinate::Coordinate(double x, double y)
  93. {
  94.     this->x = x;
  95.     this->y = y;
  96.     this->z = 0;
  97.  
  98.     epsilon = 0.001
  99. }
  100.  
  101. Coordinate::Coordinate(double x, double y, double z)
  102. {
  103.     this->x = x;
  104.     this->y = y;
  105.     this->z = z;
  106.  
  107.     epsilon = 0.001
  108. }
  109.  
  110. Coordinate::Coordinate(Coordinate &c)
  111. {
  112.     this->setPos(c.getX(), c.getY());
  113.  
  114.     epsilon = 0.001
  115. }
  116.  
  117. Coordinate::~Coordinate(void)
  118. {
  119. }
  120.  
  121. Coordinate& Coordinate::operator=(Coordinate &c)
  122. {
  123.     this->setPos(c.getX(), c.getY(), c.getZ());
  124.     return *this;
  125. }
  126.  
  127. bool Coordinate::operator==(Coordinate &c)
  128. {
  129.     double dist = distanceTo(c);
  130.  
  131.     if(dist > epsilon)
  132.     {
  133.         return false;
  134.     }
  135.     else if(dist < epsilon)
  136.     {
  137.         return false;
  138.     }
  139.     else
  140.     {
  141.         return true;
  142.     }
  143. }
  144.  
  145. double Coordinate::operator[](int i)
  146. {
  147.     if(i == 0)
  148.     {
  149.         return x;
  150.     }
  151.     else if(i == 1)
  152.     {
  153.         return y;
  154.     }
  155.     else if(i == 2)
  156.     {
  157.         return z;
  158.     }
  159.     else
  160.     {
  161.         std::cout << "Coordinate index is out of bounds" << std::endl;
  162.     }
  163. }
  164.  
  165.  
  166. double Coordinate::getX()
  167. {
  168.     return x;
  169. }
  170.  
  171. double Coordinate::getY()
  172. {
  173.     return y;
  174. }
  175.  
  176. double Coordinate::getZ()
  177. {
  178.     return z;
  179. }
  180.  
  181. void Coordinate::setPos(int x, int y)
  182. {
  183.     this->x = x;
  184.     this->y = y;
  185. }
  186.  
  187. void Coordinate::setPos(int x, int y, int z)
  188. {
  189.     this->x = x;
  190.     this->y = y;
  191.     this->z = z;
  192. }
  193.  
  194. void Coordinate::setPos(double x, double y)
  195. {
  196.     this->x = x;
  197.     this->y = y;
  198. }
  199.  
  200. void Coordinate::setPos(double x, double y, double z)
  201. {
  202.     this->x = x;
  203.     this->y = y;
  204.     this->z = z;
  205. }
  206.  
  207. void Coordinate::deltaPos(int x, int y)
  208. {
  209.     this->x = this->x + x;
  210.     this->y = this->y + y;
  211. }
  212.  
  213. void Coordinate::deltaPos(int x, int y, int z)
  214. {
  215.     this->x = this->x + x;
  216.     this->y = this->y + y;
  217.     this->z = this->z + z;
  218. }
  219.  
  220. void Coordinate::deltaPos(double x, double y)
  221. {
  222.     this->x = this->x + x;
  223.     this->y = this->y + y;
  224. }
  225.  
  226. void Coordinate::deltaPos(double x, double y, double z)
  227. {
  228.     this->x = this->x + x;
  229.     this->y = this->y + y;
  230.     this->z = this->z + z;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement