#include <iostream>
using namespace std;

#include <point.h>
#include <typedefs.h>
#include <print.h>

#include <halfspaceD2.h>
#include <halfspaceD2test.h>

void halfspaceD2test::test01()
{
  cout << "halfspace h1(0,1)" << endl;
  cout << "Enter a point to test isinside:  ";

  typedef point2<double> pt2;
  pt2 p;
  cin >> p.x >> p.y;
  
  halfspaceD2< pt2, double > h1(pt2(0.0,0.0),pt2(0.0,1.0));

  cout << SHOW (h1.isInside(p)) << endl;
  cout << SHOW (h1.isInsideOrOnBoundary(p)) << endl;
  cout << SHOW (partitionspace<pt2>::classifystring(h1.classify(p))) << endl;
}


void halfspaceD2test::test02()
{
  cout << "Testing finding the minimum point on the line to the given point" << endl;
  typedef point2<double> pt2;
  pt2 p0(0.0,0.0);
  pt2 p1(1.0,0.0);
  cout << "line from " << p0 << " to " << p1 << endl;

  halfspaceD2< pt2, double > h1(p0,p1);
  pt2 p(0.5,1.0);
  double t;
  h1.minimizepointtoline(t,p);
  pt2 pmin;
  h1.pointOnLine(pmin,t);
  cout << SHOW(t) << endl;
  cout << SHOW(pmin) << endl;

  cout << SHOW(h1.distancefromhalfspace(p)) << endl;
  p=pt2(0.5,2.0);
  cout << SHOW(p) << endl;
  cout << SHOW(h1.distancefromhalfspace(p)) << endl;
  p=pt2(0.3,0.7);
  cout << SHOW(p) << endl;
  cout << SHOW(h1.distancefromhalfspace(p)) << endl;
  p=pt2(0.1,0.7);
  cout << SHOW(p) << endl;
  cout << SHOW(h1.distancefromhalfspace(p)) << endl;
  p=pt2(0.1,0.4);
  cout << SHOW(p) << endl;
  cout << SHOW(h1.distancefromhalfspace(p)) << endl;
  
}

int halfspaceD2test::unittest01()
{
  cout << "Testing clipping in half-space D2." << endl;
  cout << "Constructing a vertical half-space left of y-axis." << endl;

  cout << "Sending lines on left, right and through the y-axis." << endl;

  typedef point2<double> pt2;

  // Half space defined left of y-axis.
  pt2 p0(0.0,0.0);
  pt2 p1(0.0,1.0);
  halfspaceD2< pt2, double > h1(p0,p1);

  // Vertical line
  pt2 a(0.0,0.5);
  pt2 m(1.0,0.0);

  point2<double> t0(.12, .7);
  //cout << SHOW(t0) << endl;
  assertreturnOS(h1.clip(t0[0],t0[1],a,m)==false);

  point2<double> t1(-.12, -.7);
  assertreturnOS(h1.clip(t1[0],t1[1],a,m)==true);

  point2<double> t2(-3, 4.2);
  assertreturnOS(h1.clip(t2[0],t2[1],a,m)==true);

  point2<double> t3(-3, 4.2);
  assertreturnOS(h1.clipNeg(t3[0],t3[1],a,m)==true);

  point2<double> t4(1.3, 4.2);
  assertreturnOS(h1.clipNeg(t4[0],t4[1],a,m)==true);

  point2<double> t5(-5.3, -2.3);
  assertreturnOS(h1.clipNeg(t5[0],t5[1],a,m)==false);

  return 0;
}





