#include <stdio.h>

#include "./defines.hpp"

#include "./plane.hpp"
#include "./vec3.hpp"
#include "./color.hpp"
#include "./material.hpp"

namespace raytracer{

bool plane::intersects(const ray& ray, double& length, vec3& normal, material& material) const
{
  double a= ray.direction.dot(this->normal_);
  double j= (a< 0.0)? -a: a;
  bool inters= false;
  double len= -1;
  
  if(j< ACCURACY)//ray is parallel to the plane
  {
    inters= false;    
  }
  else//ray intersects plane
  {
    double b= this->normal_.dot(ray.origin.add(this->normal_.scale(this->distance_).negative()));     
    len= (-1*(b/a));
    inters= (len> ACCURACY);
  }

  if(!inters) return false;
  length= len;
  material= *this->material_;
  normal= this->normal_;
  return true;
};

}
