tuples.cpp 759 B

1234567891011121314151617181920212223242526272829303132333435
  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 <tuples.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. Vector Vector::cross(const Vector &b) const
  25. {
  26. return Vector(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. }