tuple.cpp 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. return Tuple(this->x / mag, this->y / mag, this->z / mag, this->w / mag);
  19. }
  20. double Tuple::dot(const Tuple &b)
  21. {
  22. return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w;
  23. }
  24. Tuple Tuple::cross(const Tuple &b) const
  25. {
  26. return Tuple(this->y * b.z - this->z * b.y,
  27. this->z * b.x - this->x * b.z,
  28. this->x * b.y - this->y * b.x,
  29. 0);
  30. }
  31. Tuple Tuple::reflect(const Tuple &normal)
  32. {
  33. return *this - normal * 2 * this->dot(normal);
  34. }