#include "AlgUtils.h"
#define ROUND(a) floor(a + 0.5)
#include "math.h"

#define TOLX 3

bool lineEquation(RacPixel &p1, RacPixel &p2, RacLine &line)
{
  if(abs(p2.x - p1.x) > TOLX)
  {
    line.a = (p2.y - p1.y)/(float)(p2.x - p1.x);
    line.b = -1;
    line.c = p1.y - line.a*p1.x;
  }
  else
  {
    line.a = 0;
    line.b = 0;
    line.c = -p1.x;
  }
  return true;
  /*
  if((abs(p2.x - p1.x) > tolx) && (abs(p2.y - p1.y) > toly))
  {
    line.a = (p2.y - p1.y)/(float)(p2.x - p1.x);
    line.b = p1.y - line.a*p1.x;
    return true;
  }
  return false;
  */
}

bool linesIntersection(RacLine &l1, RacLine &l2, RacPixel &pixel)
{
  float t2 = l1.a * l2.b - l2.a * l1.b;
  
  if( t2 && l2.a )
  {
    float tmp = (l2.a*l1.c - l1.a * l2.c) / t2;
    pixel.y = ROUND(tmp);
    tmp = (-l2.b*tmp - l2.c ) / l2.a ;
    pixel.x = ROUND(tmp);
    return true;
  }
  return false;
  /*
  if(l1.a != l2.a)
  {
    float tmp = (l2.b - l1.b)/(l1.a - l2.a);
    pixel.x = ROUND(tmp);

    tmp = l2.a*pixel.x + l2.b;
    pixel.y = ROUND(tmp);
    return true;
  }
  return false;
  */
}
