-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVec3.h
More file actions
68 lines (49 loc) · 1.34 KB
/
Vec3.h
File metadata and controls
68 lines (49 loc) · 1.34 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
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
//
// Created by yamisaaf on 06/02/2020.
//
#include <iostream>
#include <fstream>
#ifndef RAYTRACER_VECTOR_H
#define RAYTRACER_VECTOR_H
#include "cmath"
namespace Raytracer{
template<typename T>
class Vec3 {
private:
public:
T x,y,z;
Vec3() : x(0),y(0),z(0) {}
~Vec3(){}
Vec3(const T & _x,const T &_y,const T & _z) : x(_x),y(_y),z(_z){}
T operator*(const Vec3 & vec) const{
return x * vec.x + y*vec.y + z * vec.z;
}
Vec3 operator*(const T &value ) const{
return Vec3(x * value , y * value , z*value);
}
Vec3 operator+(const Vec3 & vec) const{
return Vec3(x + vec.x , y+ vec.y,z + vec.z);
}
Vec3 operator-(const Vec3 & vec) const{
return Vec3(x - vec.x , y - vec.y , z - vec.z);
}
Vec3 getNormalize() const{
Vec3 result(x,y,z);
result.normalize();
return result;
}
T norm(){
return x * x + y *y+ z*z;
}
Vec3& normalize(){
float n = norm();
if (n > 0) {
float factor = 1 / sqrt(n);
x *= factor, y *= factor, z *= factor;
}
return *this;
}
};
}
typedef Raytracer::Vec3<float> Vec3f;
#endif //RAYTRACER_VECTOR_H