-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVec3.cpp
More file actions
39 lines (31 loc) · 1.14 KB
/
Vec3.cpp
File metadata and controls
39 lines (31 loc) · 1.14 KB
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
//
// Created by Herman Karlsson on 2017-10-07.
//
#include "Vec3.h"
Vec3 Vec3::Null(-1e20,-1e20,-1e20);
Vec3 Vec3::operator+(Vec3 other) { return Vec3(x + other.x, y + other.y, z + other.z); }
Vec3 Vec3::operator-() { return Vec3(-x, -y, -z); }
Vec3 Vec3::operator-(Vec3 other) { return *this + -other; }
double Vec3::operator*(Vec3 other) { return x * other.x + y * other.y + z * other.z; }
Vec3 Vec3::operator*(double k) { return Vec3(k * x, k * y, k * z); }
Vec3 Vec3::operator/(double k) { return *this * (1/k); }
bool Vec3::operator==(Vec3 other) {
return std::abs(x - other.x) < eps && std::abs(y - other.y) < eps && std::abs(z - other.z) < eps;
}
double Vec3::norm() {
return sqrt(x * x + y * y + z * z);
}
Vec3 Vec3::normalize() {
return *this / this->norm();
}
Vec3 Vec3::reflect_as_normal(Vec3 normal) {
return *this - normal * 2 * (*this * normal);
}
Vec3 Vec3::mul(Vec3 other) {
return Vec3(x * other.x, y * other.y, z * other.z);
}
Vec3 min_dist_point(Vec3 to_1, Vec3 to_2, Vec3 from) {
if (to_1 == Vec3::Null) return to_2;
if (to_2 == Vec3::Null) return to_1;
return (to_1 - from).norm() < (to_2 - from).norm() ? to_1 : to_2;
}