Guest User

Geometry

a guest
Sep 17th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #ifndef __GEOMETRY_HPP__
  2. #define __GEOMETRY_HPP__
  3.  
  4. #include <iostream>
  5.  
  6. //Parameterized point, for simple vector arithmetic
  7. template <class T>
  8. struct Point
  9. {
  10.     T       x;
  11.     T       y;
  12.  
  13.             Point()
  14.     {
  15.         x = y = 0;
  16.     }
  17.             Point(const T& tx, const T& ty)
  18.     {
  19.         x = tx;
  20.         y = ty;
  21.     }
  22.    
  23.     //POINT ADDITION//
  24.  
  25.     Point&  operator=(const Point& p)
  26.     {
  27.         x = p.x;
  28.         y = p.y;
  29.         return *this;
  30.     }
  31.  
  32.     Point&  operator+=(const Point& p)
  33.     {
  34.         x += p.x;
  35.         y += p.y;
  36.         return *this;  
  37.     }
  38.     Point&  operator-=(const Point& p)
  39.     {
  40.         x -= p.x;
  41.         y -= p.y;
  42.         return *this;  
  43.     }
  44.     Point&  operator*=(const Point& p)
  45.     {
  46.         x *= p.x;
  47.         y *= p.y;
  48.         return *this;  
  49.     }
  50.     Point&  operator/=(const Point& p)
  51.     {
  52.         x /= p.x;
  53.         y /= p.y;
  54.         return *this;
  55.     }
  56.  
  57.     Point   operator+(const Point& p)
  58.     {
  59.         Point<T> res = *this;
  60.         res += p;
  61.         return res;
  62.     }
  63.     Point   operator-(const Point& p)
  64.     {
  65.         Point<T> res = *this;
  66.         res -= p;
  67.         return res;
  68.     }
  69.     Point   operator*(const Point& p)
  70.     {
  71.         Point<T> res = *this;
  72.         res *= p;
  73.         return res;
  74.     }
  75.     Point   operator/(const Point& p)
  76.     {
  77.         Point<T> res = *this;
  78.         res /= p;
  79.         return res;
  80.     }
  81.  
  82.     //INTEGER ADDITION//
  83.    
  84.     Point&  operator=(const int& i)
  85.     {
  86.         x = i;
  87.         y = i;
  88.         return *this;
  89.     }
  90.  
  91.     Point&  operator+=(const int& i)
  92.     {
  93.         x += i;
  94.         y += i;
  95.         return *this;  
  96.     }
  97.     Point&  operator-=(const int& i)
  98.     {
  99.         x -= i;
  100.         y -= i;
  101.         return *this;  
  102.     }
  103.     Point&  operator*=(const int& i)
  104.     {
  105.         x *= i;
  106.         y *= i;
  107.         return *this;  
  108.     }
  109.     Point&  operator/=(const int& i)
  110.     {
  111.         x /= i;
  112.         y /= i;
  113.         return *this;
  114.     }
  115.  
  116.     Point   operator+(const int& i)
  117.     {
  118.         Point<T> res = *this;
  119.         res += i;
  120.         return res;
  121.     }
  122.     Point   operator-(const int& i)
  123.     {
  124.         Point<T> res = *this;
  125.         res -= i;
  126.         return res;
  127.     }
  128.     Point   operator*(const int& i)
  129.     {
  130.         Point<T> res = *this;
  131.         res *= i;
  132.         return res;
  133.     }
  134.     Point   operator/(const int& i)
  135.     {
  136.         Point<T> res = *this;
  137.         res /= i;
  138.         return res;
  139.     }
  140.  
  141.     //UTILITIES//
  142.  
  143.     T       Length()
  144.     {  
  145.         return sqrt(x*x + y*y);
  146.     }
  147.  
  148.     friend std::ostream&    operator<<(std::ostream& os, const Point& p)
  149.     {
  150.         os  << p.x << " X,  " << p.y << " Y";
  151.         return os;
  152.     }
  153.  
  154.     friend std::istream&    operator>>(std::istream& is, Point& p)
  155.     {
  156.         while(!(is >> p.x))
  157.         {
  158.             is.clear();
  159.             is.sync();
  160.         }
  161.         while(!(is >> p.y))
  162.         {
  163.             is.clear();
  164.             is.sync();
  165.         }
  166.         return is;
  167.     }
  168. };
  169.  
  170. //Parameterized lines, with two endpoints
  171.  
  172. template <class T>
  173. struct Line
  174. {
  175.     Point<T>    p1;
  176.     Point<T>    p2;
  177.  
  178.                 Line()
  179.     {
  180.         p1 = p2 = 0;
  181.     }
  182.  
  183.                 Line(const Point<T>& p1_, const Point<T>& p2_)
  184.     {
  185.         p1  = p1_;
  186.         p2  = p2_;
  187.     }
  188.  
  189.     Line&       operator=(const Line& l)
  190.     {
  191.         p1  = l.p1;
  192.         p2  = l.p2;
  193.         return *this;
  194.     }
  195.  
  196.     Point<T>    Displacement()
  197.     {
  198.         return p2 - p1;
  199.     }
  200.  
  201.     T           Length()
  202.     {
  203.         return Displacement().Length();
  204.     }
  205.  
  206.     //TRANSLATION//
  207.    
  208.     Line&       operator+=(Line& l)
  209.     {
  210.         //Get X and Y displacement of the line
  211.         Point<T> tmp    = l.Displacement();
  212.  
  213.         //Add this displacement to both parts of the line
  214.         p1 += tmp;
  215.         p2 += tmp;
  216.  
  217.         return *this;
  218.     }
  219.  
  220.     Line&       operator-=(Line& l)
  221.     {
  222.         //Get X and Y displacement of the line
  223.         Point<T> tmp    = l.Displacement();
  224.  
  225.         //Subtract this displacement from both parts of the line
  226.         p1 -= tmp;
  227.         p2 -= tmp;
  228.  
  229.         return *this;
  230.     }
  231.  
  232.     //DILATION//
  233.  
  234.     Line&       operator*=(Line& l)
  235.     {
  236.         //Get length of the line
  237.         T tmp   = l.Length();
  238.  
  239.         //Dilate the line based on this displacement
  240.         p1 *= tmp;
  241.         p2 *= tmp;
  242.  
  243.         return *this;
  244.     }
  245.  
  246.     Line&       operator/=(Line& l)
  247.     {
  248.         //Get length of the line
  249.         T tmp   = l.Length();
  250.  
  251.         //Dilate the line based on this displacement
  252.         p1 /= tmp;
  253.         p2 /= tmp;
  254.  
  255.         return *this;
  256.     }
  257.  
  258.     Line        operator+(Line& l)
  259.     {
  260.         Line<T> res = *this;
  261.         res += l;
  262.         return res;
  263.     }
  264.  
  265.     Line        operator-(Line& l)
  266.     {
  267.         Line<T> res = *this;
  268.         res -= l;
  269.         return res;
  270.     }
  271.  
  272.     Line        operator*(Line& l)
  273.     {
  274.         Line<T> res = *this;
  275.         res *= l;
  276.         return res;
  277.     }
  278.  
  279.     Line        operator/(Line& l)
  280.     {
  281.         Line<T> res = *this;
  282.         res /= l;
  283.         return res;
  284.     }
  285.  
  286.     //UTILITIES//
  287.  
  288.     friend std::ostream&    operator<<(std::ostream& os, Line& l)
  289.     {
  290.         os  << "(" << l.p1 << "),   (" << l.p2 << ")";
  291.         return os;
  292.     }
  293.  
  294.     friend std::istream&    operator>>(std::istream& is, Line& l)
  295.     {
  296.         while(!(is >> l.p1))
  297.         {
  298.             is.clear();
  299.             is.sync();
  300.         }
  301.         while(!(is >> l.p2))
  302.         {
  303.             is.clear();
  304.             is.sync();
  305.         }
  306.         return is;
  307.     }
  308. };
  309.  
  310. #endif  //GEOMETRY_HPP
Add Comment
Please, Sign In to add comment