Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <iostream>
- template <typename T> class Vector2D
- {
- private:
- T x;
- T y;
- public:
- explicit Vector2D(const T& x=0, const T& y=0) : x(x), y(y) {}
- Vector2D(const Vector2D<T>& src) : x(src.x), y(src.y) {}
- virtual ~Vector2D() {}
- // Accessors
- inline T X() const { return x; }
- inline T Y() const { return y; }
- inline T X(const T& x) { this->x = x; }
- inline T Y(const T& y) { this->y = y; }
- // Vector arithmetic
- inline Vector2D<T> operator-() const
- { return Vector2D<T>(-x, -y); }
- inline Vector2D<T> operator+() const
- { return Vector2D<T>(+x, +y); }
- inline Vector2D<T> operator+(const Vector2D<T>& v) const
- { return Vector2D<T>(x+v.x, y+v.y); }
- inline Vector2D<T> operator-(const Vector2D<T>& v) const
- { return Vector2D<T>(x-v.x, y-v.y); }
- inline Vector2D<T> operator*(const T& s) const
- { return Vector2D<T>(x*s, y*s); }
- // Dot product
- inline T operator*(const Vector2D<T>& v) const
- { return x*v.x + y*v.y; }
- // l-2 norm
- inline T norm() const { return sqrt(x*x + y*y); }
- // inner angle (radians)
- static T angle(const Vector2D<T>& v1, const Vector2D<T>& v2)
- {
- return acos( (v1 * v2) / (v1.norm() * v2.norm()) );
- }
- };
- int main()
- {
- Vector2D<double> p1(215, 294);
- Vector2D<double> p2(174, 228);
- Vector2D<double> p3(303, 294);
- double rad = Vector2D<double>::angle(p2-p1, p3-p1);
- double deg = rad * 180.0 / M_PI;
- std::cout << "rad = " << rad << "\tdeg = " << deg << std::endl;
- p1 = Vector2D<double>(153, 457);
- p2 = Vector2D<double>(19, 457);
- p3 = Vector2D<double>(15, 470);
- rad = Vector2D<double>::angle(p2-p1, p3-p1);
- deg = rad * 180.0 / M_PI;
- std::cout << "rad = " << rad << "\tdeg = " << deg << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement