tuple.cpp 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Tuples implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <tuple.h>
  10. #include <math.h>
  11. double Tuple::magnitude()
  12. {
  13. return sqrt(pow(this->x, 2) + pow(this->y, 2) + pow(this->z, 2) + pow(this->w, 2));
  14. }
  15. Tuple Tuple::normalise()
  16. {
  17. double mag = this->magnitude();
  18. if (mag == 0)
  19. {
  20. return Tuple(0, 0, 0, 0);
  21. }
  22. return Tuple(this->x / mag, this->y / mag, this->z / mag, this->w / mag);
  23. }
  24. double Tuple::dot(const Tuple &b)
  25. {
  26. return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w;
  27. }
  28. Tuple Tuple::cross(const Tuple &b) const
  29. {
  30. return Tuple(this->y * b.z - this->z * b.y,
  31. this->z * b.x - this->x * b.z,
  32. this->x * b.y - this->y * b.x,
  33. 0);
  34. }
  35. Tuple Tuple::reflect(const Tuple &normal)
  36. {
  37. return *this - normal * 2 * this->dot(normal);
  38. }