Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 7641cbce8bd302f90856f1a2b3ce8506dd8ef819 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
   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);
	
	/**
	 * @brief scalar multiplication assignment
	 * @param scalar multiplication factor
	 * */
	Vector2f& operator*=(const float scalar);

	/**
	 * @brief scalar division assignment
	 * @param scalar divider
	 * */
	Vector2f& operator/=(const float scalar);

	/// 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];
	}

	/// x coordinate
	inline float width() const {
		return coord[0];
	}

	/// y coordinate
	inline float height() const {
		return coord[1];
	}

	/// x coordinate
	inline float x() const {
		return coord[0];
	}

	/// y coordinate
	inline float y() const {
		return coord[1];
	}
	
	/// cartesian length
	float length() const;


	/// mutable reference to the x coordinate
	inline float & get_x() {
		return coord[0];
	}

	/// mutable reference to the y coordinate
	inline float & get_y() {
		return coord[1];
	}

	/// 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]));
	}

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);

/// scalar * Vector2f operators
Vector2f operator*(float scalar, const Vector2f & vector);

}
#endif // __INCLUDED_MATH_VECTOR2F_H__