Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
vec3d.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef VEC3D_H
7#define VEC3D_H
8
9#include <iostream>
10#include <cmath>
11
12class Vec3D
13{
14public:
15 float x,y,z;
16
17 Vec3D(float x0 = 0.0f, float y0 = 0.0f, float z0 = 0.0f) : x(x0), y(y0), z(z0) {}
18
19 Vec3D(const Vec3D& v) : x(v.x), y(v.y), z(v.z) {}
20
21 Vec3D& operator= (const Vec3D &v) {
22 x = v.x;
23 y = v.y;
24 z = v.z;
25 return *this;
26 }
27
28 Vec3D operator+ (const Vec3D &v) const
29 {
30 Vec3D r(x+v.x,y+v.y,z+v.z);
31 return r;
32 }
33
34 Vec3D operator- (const Vec3D &v) const
35 {
36 Vec3D r(x-v.x,y-v.y,z-v.z);
37 return r;
38 }
39
40 float operator* (const Vec3D &v) const
41 {
42 return x*v.x + y*v.y + z*v.z;
43 }
44
45 Vec3D operator* (float d) const
46 {
47 Vec3D r(x*d,y*d,z*d);
48 return r;
49 }
50
51 friend Vec3D operator* (float d, const Vec3D& v)
52 {
53 return v * d;
54 }
55
56 Vec3D operator% (const Vec3D &v) const
57 {
58 Vec3D r(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
59 return r;
60 }
61
63 {
64 x += v.x;
65 y += v.y;
66 z += v.z;
67 return *this;
68 }
69
71 {
72 x -= v.x;
73 y -= v.y;
74 z -= v.z;
75 return *this;
76 }
77
79 {
80 x *= d;
81 y *= d;
82 z *= d;
83 return *this;
84 }
85
86 float lengthSquared() const
87 {
88 return x*x+y*y+z*z;
89 }
90
91 float length() const
92 {
93 return sqrt(x*x+y*y+z*z);
94 }
95
97 {
98 this->operator*= (1.0f/length());
99 return *this;
100 }
101
103 {
104 Vec3D r(*this);
105 r.normalize();
106 return r;
107 }
108
109 friend std::istream& operator>>(std::istream& in, Vec3D& v)
110 {
111 in >> v.x >> v.y >> v.z;
112 return in;
113 }
114
115 friend std::ostream& operator<<(std::ostream& out, const Vec3D& v)
116 {
117 out << v.x << " " << v.y << " " << v.z;
118 return out;
119 }
120
121 operator float*()
122 {
123 return (float*)this;
124 }
125};
126
127
128class Vec2D
129{
130public:
131 float x,y;
132
133 Vec2D(float x0 = 0.0f, float y0 = 0.0f) : x(x0), y(y0) {}
134
135 Vec2D(const Vec2D& v) : x(v.x), y(v.y) {}
136
137 Vec2D& operator= (const Vec2D &v) {
138 x = v.x;
139 y = v.y;
140 return *this;
141 }
142
143 Vec2D operator+ (const Vec2D &v) const
144 {
145 Vec2D r(x+v.x,y+v.y);
146 return r;
147 }
148
149 Vec2D operator- (const Vec2D &v) const
150 {
151 Vec2D r(x-v.x,y-v.y);
152 return r;
153 }
154
155 float operator* (const Vec2D &v) const
156 {
157 return x*v.x + y*v.y;
158 }
159
160 Vec2D operator* (float d) const
161 {
162 Vec2D r(x*d,y*d);
163 return r;
164 }
165
166 friend Vec2D operator* (float d, const Vec2D& v)
167 {
168 return v * d;
169 }
170
172 {
173 x += v.x;
174 y += v.y;
175 return *this;
176 }
177
179 {
180 x -= v.x;
181 y -= v.y;
182 return *this;
183 }
184
186 {
187 x *= d;
188 y *= d;
189 return *this;
190 }
191
192 float lengthSquared() const
193 {
194 return x*x+y*y;
195 }
196
197 float length() const
198 {
199 return sqrt(x*x+y*y);
200 }
201
203 {
204 this->operator*= (1.0f/length());
205 return *this;
206 }
207
209 {
210 Vec2D r(*this);
211 r.normalize();
212 return r;
213 }
214
215
216 friend std::istream& operator>>(std::istream& in, Vec2D& v)
217 {
218 in >> v.x >> v.y;
219 return in;
220 }
221
222 operator float*()
223 {
224 return (float*)this;
225 }
226};
227
228inline void rotate(float x0, float y0, float *x, float *y, float angle)
229{
230 float xa = *x - x0, ya = *y - y0;
231 *x = xa*cosf(angle) - ya*sinf(angle) + x0;
232 *y = xa*sinf(angle) + ya*cosf(angle) + y0;
233}
234
235#endif
Vec2D(float x0=0.0f, float y0=0.0f)
Definition vec3d.h:133
Vec2D & operator-=(const Vec2D &v)
Definition vec3d.h:178
Vec2D & operator+=(const Vec2D &v)
Definition vec3d.h:171
float operator*(const Vec2D &v) const
Definition vec3d.h:155
float length() const
Definition vec3d.h:197
friend std::istream & operator>>(std::istream &in, Vec2D &v)
Definition vec3d.h:216
Vec2D operator~() const
Definition vec3d.h:208
Vec2D & normalize()
Definition vec3d.h:202
float x
Definition vec3d.h:131
Vec2D & operator*=(float d)
Definition vec3d.h:185
Vec2D(const Vec2D &v)
Definition vec3d.h:135
float lengthSquared() const
Definition vec3d.h:192
Vec2D operator+(const Vec2D &v) const
Definition vec3d.h:143
Vec2D & operator=(const Vec2D &v)
Definition vec3d.h:137
Vec2D operator-(const Vec2D &v) const
Definition vec3d.h:149
float y
Definition vec3d.h:131
float x
Definition vec3d.h:15
Vec3D & operator+=(const Vec3D &v)
Definition vec3d.h:62
float y
Definition vec3d.h:15
Vec3D operator+(const Vec3D &v) const
Definition vec3d.h:28
float z
Definition vec3d.h:15
Vec3D(float x0=0.0f, float y0=0.0f, float z0=0.0f)
Definition vec3d.h:17
Vec3D operator%(const Vec3D &v) const
Definition vec3d.h:56
Vec3D operator-(const Vec3D &v) const
Definition vec3d.h:34
float lengthSquared() const
Definition vec3d.h:86
friend std::istream & operator>>(std::istream &in, Vec3D &v)
Definition vec3d.h:109
Vec3D & operator*=(float d)
Definition vec3d.h:78
Vec3D(const Vec3D &v)
Definition vec3d.h:19
float operator*(const Vec3D &v) const
Definition vec3d.h:40
float length() const
Definition vec3d.h:91
Vec3D & normalize()
Definition vec3d.h:96
Vec3D operator~() const
Definition vec3d.h:102
Vec3D & operator=(const Vec3D &v)
Definition vec3d.h:21
Vec3D & operator-=(const Vec3D &v)
Definition vec3d.h:70
friend std::ostream & operator<<(std::ostream &out, const Vec3D &v)
Definition vec3d.h:115
void rotate(float x0, float y0, float *x, float *y, float angle)
Definition vec3d.h:228