#pragma prototyped
#include "geometry.h"
#include <math.h>
Point origin = {0,0};
double xmin, xmax, ymin, ymax;
double deltax,
deltay;
int nsites;
int sqrt_nsites;
void
geominit()
{
double sn;
sn = nsites+4;
sqrt_nsites = (int)sqrt(sn);
}
double
dist_2 ( Point* pp, Point* qp)
{
double dx = pp->x - qp->x;
double dy = pp->y - qp->y;
return (dx*dx + dy*dy);
}
void
subPt (Point* a, Point b, Point c)
{
a->x = b.x - c.x;
a->y = b.y - c.y;
}
void
addPt (Point* c, Point a, Point b)
{
c->x = a.x + b.x;
c->y = a.y + b.y;
}
double
area_2(Point a, Point b, Point c )
{
return (a.x * b.y - a.y * b.x +
a.y * c.x - a.x * c.y +
b.x * c.y - c.x * b.y);
}
int
leftOf(Point a, Point b, Point c)
{
return (area_2( a, b, c ) > 0);
}
int
intersection( Point a, Point b, Point c, Point d, Point* p )
{
double s, t;
double denom;
denom =
a.x * ( d.y - c.y ) +
b.x * ( c.y - d.y ) +
d.x * ( b.y - a.y ) +
c.x * ( a.y - b.y );
if (denom == 0.0) return 0;
s = ( a.x * ( d.y - c.y ) +
c.x * ( a.y - d.y ) +
d.x * ( c.y - a.y )
) / denom;
t = -( a.x * ( c.y - b.y ) +
b.x * ( a.y - c.y ) +
c.x * ( b.y - a.y )
) / denom;
p->x = a.x + s * ( b.x - a.x );
p->y = a.y + s * ( b.y - a.y );
if ((0.0 <= s) && (s <= 1.0) &&
(0.0 <= t) && (t <= 1.0))
return 1;
else return 0;
}