File: vector.h

package info (click to toggle)
amoeba 1.1-19
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 816 kB
  • ctags: 966
  • sloc: cpp: 8,286; makefile: 173
file content (106 lines) | stat: -rw-r--r-- 1,790 bytes parent folder | download | duplicates (11)
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
#ifndef _VECTOR_H
#define _VECTOR_H

#include <math.h>

class Vector {
public:
	Vector()
	{
	}
	
	Vector(const float x, const float y, const float z)
	{
		this->x = x;
		this->y = y;
		this->z = z;
	}
	
	~Vector()
	{
	}
	
	inline Vector operator+(const Vector v) const
	{
		return Vector(this->x + v.x, this->y + v.y, this->z + v.z);
	}
	
	inline Vector &operator+=(const Vector v)
	{
		this->x += v.x;
		this->y += v.y;
		this->z += v.z;

		return *this;
	}
	
	inline Vector operator-(const Vector v) const
	{
		return Vector(this->x - v.x, this->y - v.y, this->z - v.z);
	}
	
	inline Vector &operator-=(const Vector v)
	{
		this->x -= v.x;
		this->y -= v.y;
		this->z -= v.z;

		return *this;
	}
	
	inline Vector operator*(const float s) const
	{
		return Vector(this->x * s, this->y * s, this->z * s);
	}
	
	inline Vector &operator*=(const float s)
	{
		this->x *= s;
		this->y *= s;
		this->z *= s;

		return *this;
	}
	
	inline float operator*(const Vector v) const
	{
		return (this->x * v.x + this->y * v.y + this->z * v.z) /
			(float)sqrt((this->x * this->x + this->y * this->y + this->z * this->z) *
			     (v.x * v.x + v.y * v.y + v.z * v.z));
	}
	
	inline Vector operator/(const float s) const
	{
		return *this * (1.0f / s);
	}
	
	inline Vector &operator/=(const float s)
	{
		*this *= (1.0f / s);
		return *this;
	}
	
	inline Vector cross_product(const Vector v) const
	{
		return Vector(this->y * v.z - this->z * v.y,
			      this->z * v.x - this->x * v.z,
			      this->x * v.y - this->y * v.x);
	}
	
	inline float magnitude() const
	{
		return sqrt(this->x * this->x +
			    this->y * this->y +
			    this->z * this->z);
	}
	
	inline Vector &normalize()
	{
		*this *= (1.0f / this->magnitude());
		return *this;
	}

	float x, y, z;
};

#endif /* defined(_VECTOR_H) */