Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __GEOMETRY_HPP__
- #define __GEOMETRY_HPP__
- #include <iostream>
- //Parameterized point, for simple vector arithmetic
- template <class T>
- struct Point
- {
- T x;
- T y;
- Point()
- {
- x = y = 0;
- }
- Point(const T& tx, const T& ty)
- {
- x = tx;
- y = ty;
- }
- //POINT ADDITION//
- Point& operator=(const Point& p)
- {
- x = p.x;
- y = p.y;
- return *this;
- }
- Point& operator+=(const Point& p)
- {
- x += p.x;
- y += p.y;
- return *this;
- }
- Point& operator-=(const Point& p)
- {
- x -= p.x;
- y -= p.y;
- return *this;
- }
- Point& operator*=(const Point& p)
- {
- x *= p.x;
- y *= p.y;
- return *this;
- }
- Point& operator/=(const Point& p)
- {
- x /= p.x;
- y /= p.y;
- return *this;
- }
- Point operator+(const Point& p)
- {
- Point<T> res = *this;
- res += p;
- return res;
- }
- Point operator-(const Point& p)
- {
- Point<T> res = *this;
- res -= p;
- return res;
- }
- Point operator*(const Point& p)
- {
- Point<T> res = *this;
- res *= p;
- return res;
- }
- Point operator/(const Point& p)
- {
- Point<T> res = *this;
- res /= p;
- return res;
- }
- //INTEGER ADDITION//
- Point& operator=(const int& i)
- {
- x = i;
- y = i;
- return *this;
- }
- Point& operator+=(const int& i)
- {
- x += i;
- y += i;
- return *this;
- }
- Point& operator-=(const int& i)
- {
- x -= i;
- y -= i;
- return *this;
- }
- Point& operator*=(const int& i)
- {
- x *= i;
- y *= i;
- return *this;
- }
- Point& operator/=(const int& i)
- {
- x /= i;
- y /= i;
- return *this;
- }
- Point operator+(const int& i)
- {
- Point<T> res = *this;
- res += i;
- return res;
- }
- Point operator-(const int& i)
- {
- Point<T> res = *this;
- res -= i;
- return res;
- }
- Point operator*(const int& i)
- {
- Point<T> res = *this;
- res *= i;
- return res;
- }
- Point operator/(const int& i)
- {
- Point<T> res = *this;
- res /= i;
- return res;
- }
- //UTILITIES//
- T Length()
- {
- return sqrt(x*x + y*y);
- }
- friend std::ostream& operator<<(std::ostream& os, const Point& p)
- {
- os << p.x << " X, " << p.y << " Y";
- return os;
- }
- friend std::istream& operator>>(std::istream& is, Point& p)
- {
- while(!(is >> p.x))
- {
- is.clear();
- is.sync();
- }
- while(!(is >> p.y))
- {
- is.clear();
- is.sync();
- }
- return is;
- }
- };
- //Parameterized lines, with two endpoints
- template <class T>
- struct Line
- {
- Point<T> p1;
- Point<T> p2;
- Line()
- {
- p1 = p2 = 0;
- }
- Line(const Point<T>& p1_, const Point<T>& p2_)
- {
- p1 = p1_;
- p2 = p2_;
- }
- Line& operator=(const Line& l)
- {
- p1 = l.p1;
- p2 = l.p2;
- return *this;
- }
- Point<T> Displacement()
- {
- return p2 - p1;
- }
- T Length()
- {
- return Displacement().Length();
- }
- //TRANSLATION//
- Line& operator+=(Line& l)
- {
- //Get X and Y displacement of the line
- Point<T> tmp = l.Displacement();
- //Add this displacement to both parts of the line
- p1 += tmp;
- p2 += tmp;
- return *this;
- }
- Line& operator-=(Line& l)
- {
- //Get X and Y displacement of the line
- Point<T> tmp = l.Displacement();
- //Subtract this displacement from both parts of the line
- p1 -= tmp;
- p2 -= tmp;
- return *this;
- }
- //DILATION//
- Line& operator*=(Line& l)
- {
- //Get length of the line
- T tmp = l.Length();
- //Dilate the line based on this displacement
- p1 *= tmp;
- p2 *= tmp;
- return *this;
- }
- Line& operator/=(Line& l)
- {
- //Get length of the line
- T tmp = l.Length();
- //Dilate the line based on this displacement
- p1 /= tmp;
- p2 /= tmp;
- return *this;
- }
- Line operator+(Line& l)
- {
- Line<T> res = *this;
- res += l;
- return res;
- }
- Line operator-(Line& l)
- {
- Line<T> res = *this;
- res -= l;
- return res;
- }
- Line operator*(Line& l)
- {
- Line<T> res = *this;
- res *= l;
- return res;
- }
- Line operator/(Line& l)
- {
- Line<T> res = *this;
- res /= l;
- return res;
- }
- //UTILITIES//
- friend std::ostream& operator<<(std::ostream& os, Line& l)
- {
- os << "(" << l.p1 << "), (" << l.p2 << ")";
- return os;
- }
- friend std::istream& operator>>(std::istream& is, Line& l)
- {
- while(!(is >> l.p1))
- {
- is.clear();
- is.sync();
- }
- while(!(is >> l.p2))
- {
- is.clear();
- is.sync();
- }
- return is;
- }
- };
- #endif //GEOMETRY_HPP
Add Comment
Please, Sign In to add comment