/*<html><pre>*/
#include <../source/TAL/objbase.h>

#ifdef	PLANE_NO_MACRO

void	V_GLOBAL
poly2AddPoint(poly2_p w,vec2_t pt)
{
	V_ASSERT(w->count!=w->numpoints);
	vec2Copy(w->rays[w->count++].origin,pt);
}

#endif	/* PLANE_NO_MACRO */

void	V_GLOBAL
mat2Rotation(matrix2_t mat,vec_t radians)
{
	vec_t s,c;
	v_fsincos(&s,&c,radians);
	mat[0][1]=-s;
	mat[1][0]=s;
	mat[0][0]=mat[1][1]=c;
}

void	V_GLOBAL
line2Cross(vec2_t point,line2_p l1,line2_p l2)
{
	matrix2_t m;
	m[0][0]=l1->normal[0];
	m[0][1]=l1->normal[1];
	m[1][0]=l2->normal[0];
	m[1][1]=l2->normal[1];
	vec2Set(point,l1->dist,l2->dist);
	mat2Solve(m,point);
}

void	V_GLOBAL
line2FromPoints(line2_p line,vec2_t p1,vec2_t p2)
{
	line->normal[0]=p2[1]-p1[1];
	line->normal[1]=p1[0]-p2[0];
	vec2Unit(line->normal,line->normal);
	line->dist=-vec2Dot(p1,line->normal);
}

vec_t	V_GLOBAL
line2DistFromPoint(line2_p line,vec2_t p)
{
	return vec2Dot(p,line->normal)+line->dist;
}

poly2_p	V_GLOBAL
poly2Alloc(int points)
{
	poly2_t *ret;
	int size;
	V_ASSERT(points);
	size = sizeof(poly2_t)+(points-1)*sizeof(ray2_t);
	ret=(poly2_t*)memAlloc(size);
	ret->numpoints=points;
	ret->count=0;
	ret->size=size;
	return ret;
}

poly2_p	V_GLOBAL
poly2Duplicate(poly2_t *ref)
{
	poly2_p ret;
	V_ASSERT(ref);
	ret=(poly2_p)memAlloc(ref->size);
	memCopy32(ret,ref,(ref->size)>>2);
	return ret;
}

void	V_GLOBAL
poly2ComputeEdges(poly2_p w)
{
	int i;
	for(i=0;i<w->count-1;i++)
		line2FromPoints(&w->rays[i].line,w->rays[i].origin,w->rays[i+1].origin);
	line2FromPoints(&w->rays[w->count-1].line,w->rays[w->count-1].origin,
		w->rays[0].origin);
}

vboolean	V_GLOBAL
poly2PointInside(poly2_p w,vec2_t p)
{
	int i;
	for(i=0;i<w->count;i++)
		if(line2DistFromPoint(&w->rays[i].line,p)<0.0)
			return false;
	return true;
}

/*</pre></html>*/
/*EOF*/
