This is a simple interactive applet demonstrating the calculation of a convex hull...
[micahsconvexhulldemo.git] / Vec2f.h
blob6ae64762de8b8ea7b4e2c66a976909c7ed3c00c5
1 #ifndef VECTOR_2_FLOAT_H
2 #define VECTOR_2_FLOAT_H
4 #include <cmath>
5 #include <iostream>
7 class Vec2f
9 public:
10 union
12 float V[2];
13 struct
15 float x,y;
19 Vec2f()
21 x = 0;
22 y = 0;
24 Vec2f(float X,float Y)
26 x = X;
27 y = Y;
30 inline bool operator==(const Vec2f &rhs) const
32 return (x == rhs.x && y == rhs.y);
35 inline bool operator!=(const Vec2f &rhs) const
37 return (x != rhs.x || y != rhs.y);
40 inline Vec2f& operator=(const Vec2f &rhs)
42 x = rhs.x;
43 y = rhs.y;
44 return *this;
46 inline Vec2f& operator*=(const Vec2f &param)
48 x *= param.x;
49 y *= param.y;
50 return *this;
52 inline Vec2f operator+ (const Vec2f& v) const
54 return Vec2f(x + v.x, y + v.y);
56 inline Vec2f operator- (const Vec2f& v) const
58 return Vec2f(x - v.x, y - v.y);
60 inline Vec2f operator* (const Vec2f& v) const
62 return Vec2f(x*x, y*y);
64 inline Vec2f operator* (float s) const
66 return Vec2f(x*s, y*s);
68 inline Vec2f operator/ (float s) const
70 return Vec2f(x/s, y/s);
72 inline Vec2f operator/ (const Vec2f& v) const
74 return Vec2f(x/v.x, y/v.y);
76 inline float Normalize()
78 float len = sqrtf(x*x + y*y);
79 if (len > 0.0)
81 x /= len;
82 y /= len;
84 return len;
86 inline float dot(const Vec2f& v) const
88 return (x*v.x + y*v.y);
91 inline float length() const
93 return sqrtf(x*x + y*y);
95 inline float lengthSqr() const
97 return x*x + y*y;
101 friend std::ostream& operator<< (std::ostream &lhs,const Vec2f &rhs) ;
102 friend std::istream& operator>> (std::istream &lhs,const Vec2f &rhs);
105 inline float length(const Vec2f& v)
107 return sqrtf(v.x*v.x + v.y*v.y);
109 inline float lengthSqr(const Vec2f& v)
111 return v.x*v.x + v.y*v.y;
114 inline Vec2f floor(const Vec2f& v)
116 return Vec2f(floor(v.x),floor(v.y));
119 inline Vec2f abs(const Vec2f& v)
121 return Vec2f(fabs(v.x),fabs(v.y));
124 #endif