Flatland

Documentation

SourceForge.net Logo

vector.hpp

Go to the documentation of this file.
00001 // Summary: Declares vec2 and related math and graphics methods.
00002 // Copyright: 2007  Philip Rideout.  All rights reserved.
00003 // License: see source/bsd-license.txt
00004 
00005 #pragma once
00006 
00007 namespace Flatland
00008 {
00009     const float pi = 3.1415926535897932384626433832795f;
00010     bool is_nan(float f);
00011     float round(float f);
00012 
00013     struct vec2;
00014     float dot(const vec2 &a, const vec2 &b);
00015     float cross(vec2 a, vec2 b);
00016 
00017     /// 2D vector
00018     struct vec2
00019     {
00020         vec2() {}
00021         vec2(float x, float y) : x(x), y(y) {}
00022         vec2(const float *f) : x(f[0]), y(f[1]) {}
00023         float length() const;
00024         vec2 rotate(float d) const;
00025         float squared_length() const { return dot(*this, *this); }
00026         void normalize() { *this /= length(); }
00027         vec2 rotate(vec2 v) const { return vec2(x * v.x + y * v.y, v.x * y - x * v.y); }
00028         vec2 hat() const { return *this / length(); }
00029         vec2 perp() const { return vec2(-y, x); }
00030         vec2 flip() const { return vec2(y, x); }
00031         void snap(float epsilon);
00032         vec2 operator-() const { return vec2(-x, -y); }
00033         vec2 &operator-=(vec2 v) { x -= v.x; y -= v.y; return *this; }
00034         vec2 &operator+=(vec2 v) { x += v.x; y += v.y; return *this; }
00035         vec2 &operator/=(float f) { x /= f; y /= f; return *this; }
00036         vec2 operator+(vec2 v) const { return vec2(x + v.x, y + v.y); }
00037         vec2 operator-(vec2 v) const { return vec2(x - v.x, y - v.y); }
00038         vec2 operator*(float f) const { return vec2(x * f, y * f); }
00039         vec2 operator/(float f) const { return vec2(x / f, y / f); }
00040         float x, y;
00041     };
00042 
00043     inline float dot(const vec2 &a, const vec2 &b) { return a.x * b.x + a.y * b.y; }
00044     inline float cross(vec2 a, vec2 b) { return a.x * b.y - a.y * b.x; }
00045 }

Generated on Sat Jan 13 17:20:21 2007 for Flatland by doxygen 1.5.1