Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 6258d8591d2754d9ae450e494e2d94da3d70cd27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
   math/vector2f.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_MATH_VECTOR2F_H__
#define __INCLUDED_MATH_VECTOR2F_H__

#include <iostream>

namespace math
{

/// a point or vector in 2D space
class Vector2f
{

public:
	/// default constructor
	Vector2f();
	
	/// assignment constructor
	Vector2f(const float x, const float y);
	
	/// copy constructor
	Vector2f(const Vector2f &other);
	
	/// assign (0,0)
	void clear();
	
	/// assignment operator
	void assign(const float x, const float y);
	
	/// assignment operator
	void assign(const Vector2f &other);
	
	/// assignment operator
	Vector2f & operator=(const Vector2f &other);
	
	/// vector subtraction
	Vector2f & operator-=(const Vector2f &other);
	
	/// vector sum
	Vector2f & operator+=(const Vector2f &other);
	
	/// vector subtraction
	inline Vector2f operator+(const Vector2f &other) const {
		Vector2f v(coord[0] + other.coord[0], coord[1]+other.coord[1]);
		return v;
	}
	
	/// vector sum
	inline Vector2f operator-(const Vector2f &other) const {
		Vector2f v(coord[0] - other.coord[0], coord[1] - other.coord[1]);
		return v;
	}
	
	/// scalar product
	inline Vector2f operator*(const float scalar) const {
		Vector2f v(coord[0]*scalar, coord[1]*scalar);
		return v;
	}
	
	/// scalar division
	inline Vector2f operator/(const float scalar) const {
		Vector2f v(coord[0]/scalar, coord[1]/scalar);
		return v;
	}
	
	/// array operator
	inline float& operator[](const size_t index) {
		return coord[index];
	}
	
	/// array operator
	inline float operator[](const size_t index) const {
		return coord[index];
	}
	
	/// a pointer to the internal data
	inline float *ptr() const {
		return (float *) coord;
	}
	
	inline bool contains(const Vector2f &other) const {
		return ((other.coord[0] >= 0) && (other.coord[1] >= 0) && (other.coord[0] <= coord[0]) && (other.coord[1] <= coord[1]));
	}
	
	inline bool contains(float x, float y) const {
		return ((x >= 0) && (y >= 0) && (x <= coord[0]) && (y <= coord[1]));
	}

	inline float width() const {
		return coord[0];
	}
	
	inline float height() const {
		return coord[1];
	}

	/// x coordinate
	float &x;
	
	/// y coordinate
	float &y;
	
private:
	float coord[2];
};

/// distance between two vectors
float distance(const Vector2f& first, const Vector2f& second);

/// distance between two vectors squared
float distancesquared(const Vector2f& first, const Vector2f& second);

}
#endif // __INCLUDED_MATH_VECTOR2F_H__