mirror of
https://github.com/okalachev/flix.git
synced 2026-02-17 23:51:25 +00:00
Restore accidentally removed files
This commit is contained in:
90
flix/vector.h
Normal file
90
flix/vector.h
Normal file
@@ -0,0 +1,90 @@
|
||||
// Lightweight vector library
|
||||
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
|
||||
// Repository: https://github.com/okalachev/flix
|
||||
|
||||
#pragma once
|
||||
|
||||
class Vector : public Printable
|
||||
{
|
||||
public:
|
||||
float x, y, z;
|
||||
|
||||
Vector(): x(0), y(0), z(0) {};
|
||||
|
||||
Vector(float x, float y, float z): x(x), y(y), z(z) {};
|
||||
|
||||
float norm() const
|
||||
{
|
||||
return sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
bool zero() const
|
||||
{
|
||||
return x == 0 && y == 0 && z == 0;
|
||||
}
|
||||
|
||||
void normalize()
|
||||
{
|
||||
float n = norm();
|
||||
x /= n;
|
||||
y /= n;
|
||||
z /= n;
|
||||
}
|
||||
|
||||
Vector operator * (float b)
|
||||
{
|
||||
return Vector(x * b, y * b, z * b);
|
||||
}
|
||||
|
||||
Vector operator + (const Vector& b) const
|
||||
{
|
||||
return Vector(x + b.x, y + b.y, z + b.z);
|
||||
}
|
||||
|
||||
Vector operator - (const Vector& b) const
|
||||
{
|
||||
return Vector(x - b.x, y - b.y, z - b.z);
|
||||
}
|
||||
|
||||
inline bool operator == (const Vector& b) const
|
||||
{
|
||||
return x == b.x && y == b.y && z == b.z;
|
||||
}
|
||||
|
||||
inline bool operator != (const Vector& b) const
|
||||
{
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
inline bool finite() const
|
||||
{
|
||||
return isfinite(x) && isfinite(y) && isfinite(z);
|
||||
}
|
||||
|
||||
static float dot(const Vector& a, const Vector& b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
static Vector cross(const Vector& a, const Vector& b)
|
||||
{
|
||||
return Vector(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
||||
}
|
||||
|
||||
static float angleBetweenVectors(const Vector& a, const Vector& b)
|
||||
{
|
||||
return acos(dot(a, b) / (a.norm() * b.norm()));
|
||||
}
|
||||
|
||||
static Vector angularRatesBetweenVectors(const Vector& u, const Vector& v)
|
||||
{
|
||||
return cross(u, v);
|
||||
}
|
||||
|
||||
size_t printTo(Print& p) const {
|
||||
return
|
||||
p.print(x, 15) + p.print(" ") +
|
||||
p.print(y, 15) + p.print(" ") +
|
||||
p.print(z, 15);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user