tuple.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /*
  25. double Tuple::dot(const Tuple &b)
  26. {
  27. return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w;
  28. }
  29. Tuple Tuple::cross(const Tuple &b) const
  30. {
  31. return Tuple(this->y * b.z - this->z * b.y,
  32. this->z * b.x - this->x * b.z,
  33. this->x * b.y - this->y * b.x,
  34. 0);
  35. }
  36. */
  37. Tuple Tuple::reflect(const Tuple &normal)
  38. {
  39. return *this - normal * 2 * this->dot(normal);
  40. }
  41. bool Tuple::isRepresentable()
  42. {
  43. return !(isnan(this->x) || isnan(this->y) || isnan(this->z) ||
  44. isinf(this->x) || isinf(this->y) || isinf(this->z));
  45. }