All pastes #727745 Raw Edit

Someone

public text v1 · immutable
·published 2007-10-06 10:13 UTC
rendered paste body
/*--------------------------------------
	Intersection
--------------------------------------*/
#define GW_NO_CROSS	0
#define GW_CROSS	1
#define	GW_PARREL	2

int Intersect(const gwSegment& _first, const gwSegment& _second, gwVector& _result)
{
	gwVector _alpha(_first.ex - _first.sx, _first.ey - _first.sy);		// First Direction
	gwVector _beta(_second.sx - _second.ex, _second.sy - _second.ey);	// Second Direction Flipped
	_result = gwVector();

	//----------------------------------------
	//	X Bounding Check
	//----------------------------------------
	if(_alpha.x > 0)	// Ensure Work is Left to Right
	{
		if(_beta.x > 0)
		{
			if((_first.ex < _second.ex) || (_second.sx < _first.sx))		return GW_NO_CROSS;
		}
		else
		{
			if((_first.ex < _second.sx) || (_second.ex < _first.sx))		return GW_NO_CROSS;
		}
	}
	else
	{
		if(_beta.x > 0)
		{
			if((_first.sx < _second.ex) || (_second.sx < _first.ex))		return GW_NO_CROSS;
		}
		else
		{
			if((_first.sx < _second.sx) || (_second.ex < _first.ex))		return GW_NO_CROSS;
		}
	}

	//----------------------------------------
	//	Y Bounding Check
	//----------------------------------------
	if(_alpha.y > 0)	// Ensure Work is Left to Right
	{
		if(_beta.y > 0)
		{
			if((_first.ey < _second.ey) || (_second.sy < _first.sy))		return GW_NO_CROSS;
		}
		else
		{
			if((_first.ey < _second.sy) || (_second.ey < _first.sy))		return GW_NO_CROSS;
		}
	}
	else
	{
		if(_beta.y > 0)
		{
			if((_first.sy < _second.ey) || (_second.sy < _first.ey))		return GW_NO_CROSS;
		}
		else
		{
			if((_first.sy < _second.sy) || (_second.ey < _first.ey))		return GW_NO_CROSS;
		}
	}

	gwVector _charlie(_first.sx - _second.sx, _first.sy - _second.sy);
	
	//-----------------------------------
	//	Parrell Test
	//-----------------------------------
	float _dem = (_alpha.y * _beta.x) - (_alpha.x * _beta.y);
	if(fabsf(_dem) < 0.00000001f) return GW_PARREL;

	//-----------------------------------
	//	Alpha Test
	//-----------------------------------
	float _an = (_beta.y * _charlie.x) - (_beta.x * _charlie.y);

	if(_dem > 0)
	{
		if(_an < 0 || _an > _dem) return GW_NO_CROSS;
	}
	else 
	{
		if(_an > 0 || _an < _dem) return GW_NO_CROSS;
	}

	//-----------------------------------
	//	Beta Test
	//-----------------------------------
	float _bn = (_alpha.x * _charlie.y) - (_alpha.y * _charlie.x);

	if(_dem > 0)
	{
		if(_bn < 0 || _bn > _dem) return GW_NO_CROSS;
	}
	else 
	{
		if(_bn > 0 || _bn < _dem) return GW_NO_CROSS;
	}

	//-----------------------------------
	//	They Do Intersect
	//	Calc Point
	//-----------------------------------
	gwVector _num(_an*_alpha.x);
	gwVector _offset;
	_offset.x = (((_num.x > 0)&&(_dem > 0)) || ((_num.x < 0)&&(_dem < 0))) ? (_dem / 2.0f) : -(_dem/2.0f);
	_offset.y = (((_num.y > 0)&&(_dem > 0)) || ((_num.y < 0)&&(_dem < 0))) ? (_dem / 2.0f) : -(_dem/2.0f);

	_result = gwVector(_first.sx,_first.sy) + (_num + _offset) / _dem;
	return GW_CROSS;
}