struct Vec3 {
float x, y, z;
Vec3() : x(0), y(0), z(0) {}
Vec3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
Vec3 operator+(const Vec3& other) const {
return Vec3(x + other.x, y + other.y, z + other.z);
}
Vec3 operator-(const Vec3& other) const {
return Vec3(x - other.x, y - other.y, z - other.z);
}
Vec3 operator*(const float other) const {
return Vec3(x * other, y * other, z * other);
}
};
$$ ||\mathbf{v}||=\sqrt{x^2+y^2+z^2} $$
// 벡터의 길이를 구하는 함수
float length() const {
return std::sqrt(x * x + y * y + z * z);
}
$$ \mathbf{v}_{norm}=\frac{\mathbf{v}}{||\mathbf{v}||} $$
// 벡터를 정규화하는 함수
void normalize() {
float norm = length();
if (norm != 0) {
x /= norm;
y /= norm;
z /= norm;
}
}
$$ \mathbf{A} \cdot \mathbf{B} = (A_x \times B_x) + (A_y \times B_y) + (A_z \times B_z) $$
// 내적 계산 함수
float dot(const Vec3& other) const {
return (x * other.x + y * other.y + z * other.z);
}
$$ X = A_y \times B_z - A_z \times B_y \\ Y = A_z \times B_x - A_x \times B_z \\ Z = A_x \times B_y - A_y \times B_x $$