#include <d3circlepartition.h>

#include <mathlib.h>


bool const d3circlepartition::intersection
(
  pt3 & x, 
  pt3c & w, 
  pt3c & b
) const
{
  pt2c w2(w.x-x0.x,w.y-x0.y);
  pt2c b2(b.x-x0.x,b.y-x0.y);

  pt2c b3(b2-w2);
  double const q0 = b3.dot();
  double const q1 = w2.dot(b3)*2.0;
  double const q2 = w2.dot()-radius*radius;

  double t0;
  double t1;
  bool res = solvequadratic(t0,t1,q0,q1,q2);
  if (res==false)
    return false;

  double t = -1;
  if ((t0>=0.0) && (t0<=1.0))
    t = t0;
  if ((t1>=0.0) && (t1<=1.0))
    t = t1; 
  x = w + (b-w)*t;

  if (t==-1)
    return false;
 
  return true; 
}

bool const d3circlepartition::isOnBoundary
(
  pt3c & w, 
  double const zero
) const
{
  pt2c z(w.x-x0.x,w.y-x0.y);
  double const d = z.dot() - radius*radius;

  if (0 < (d + zero))
  {
    //if ( zero + d < zero + zero )
    if ( d < zero )
      return true;
  }

  return false;
}



