Sources/CryGame C++/Solution1/CryGame/XArea.cpp
//////////////////////////////////////////////////////////////////////
// calculates min distance from point within area to the border of area
// returns fade coefficient: Distance/m_Proximity
// [0 - on the very border of area,	1 - inside area, distance to border is more than m_Proximity]

calculates min distance from point within area to the border of area
returns fade coefficient: Distance/m_Proximity
[0 - on the very border of area,       1 - inside area, distance to border is more than m_Proximity]
calculates min distance from point within area to the border of area returns fade coefficient : Distance / m_Proximity [ 0 - on the very border of area 1 - inside area distance to border is more than m_Proximity ]
float	CXArea::CalcDistToPoint(const a2DPoint& point) const
{
	if (m_Proximity==0.0f)
		return 1.0f;
	float	distMin = m_Proximity*m_Proximity;
	float	curDist;
	a2DBBox	proximityBox;
	proximityBox.max.x = point.x + m_Proximity;
	proximityBox.max.y = point.y + m_Proximity;
	proximityBox.min.x = point.x - m_Proximity;
	proximityBox.min.y = point.y - m_Proximity;
	for(unsigned int sIdx=0; sIdx<m_vpSegments.size(); sIdx++)
	{
		a2DSegment *curSg = m_vpSegments[sIdx];
		if(!m_vpSegments[sIdx]->bbox.BBoxOutBBox2D( proximityBox ))
		{
			if(m_vpSegments[sIdx]->isHorizontal)
			{
				if( point.x<m_vpSegments[sIdx]->bbox.min.x )
					curDist = m_vpSegments[sIdx]->bbox.min.DistSqr( point );
				else if( point.x>m_vpSegments[sIdx]->bbox.max.x )
					curDist = m_vpSegments[sIdx]->bbox.max.DistSqr( point );
				else
					curDist = fabsf( point.y-m_vpSegments[sIdx]->bbox.max.y );
				curDist *= curDist;
			}
			else
			if(m_vpSegments[sIdx]->k==0.0f)
			{
				if( point.y<m_vpSegments[sIdx]->bbox.min.y )
					curDist = m_vpSegments[sIdx]->bbox.min.DistSqr( point );
				else if( point.y>m_vpSegments[sIdx]->bbox.max.y )
					curDist = m_vpSegments[sIdx]->bbox.max.DistSqr( point );
				else
					curDist = fabsf( point.x-m_vpSegments[sIdx]->b );
				curDist *= curDist;
			}
			else
			{
				a2DPoint	intersection;
				float	b2, k2;
				k2 = -1.0f/m_vpSegments[sIdx]->k;
				b2 = point.y - k2*point.x;
				intersection.x = (b2 - m_vpSegments[sIdx]->b)/(m_vpSegments[sIdx]->k - k2);
				intersection.y = k2*intersection.x + b2;
				if( intersection.x<m_vpSegments[sIdx]->bbox.min.x)
					if( m_vpSegments[sIdx]->k<0 )
						curDist = point.DistSqr( m_vpSegments[sIdx]->bbox.min.x, m_vpSegments[sIdx]->bbox.max.y );
					else
						curDist = point.DistSqr( m_vpSegments[sIdx]->bbox.min );
				else if( intersection.x>m_vpSegments[sIdx]->bbox.max.x)
					if( m_vpSegments[sIdx]->k<0 )
						curDist = point.DistSqr( m_vpSegments[sIdx]->bbox.max.x, m_vpSegments[sIdx]->bbox.min.y );
					else
						curDist = point.DistSqr( m_vpSegments[sIdx]->bbox.max );
				else
					curDist = intersection.DistSqr( point );
			}
			if(curDist<distMin)
				distMin = curDist;	
		}
	}
	return cry_sqrtf(distMin)/m_Proximity;
}
float CXArea :: CalcDistToPoint ( const a2DPoint & point ) const { if ( m_Proximity == 0.0f ) return 1.0f ; float distMin = m_Proximity * m_Proximity ; float curDist ; a2DBBox proximityBox ; proximityBox . max . x = point . x + m_Proximity ; proximityBox . max . y = point . y + m_Proximity ; proximityBox . min . x = point . x - m_Proximity ; proximityBox . min . y = point . y - m_Proximity ; for ( unsigned int sIdx = 0 ; sIdx < m_vpSegments . size ( ) ; sIdx ++ ) { a2DSegment * curSg = m_vpSegments [ sIdx ] ; if ( ! m_vpSegments [ sIdx ] -> bbox . BBoxOutBBox2D ( proximityBox ) ) { if ( m_vpSegments [ sIdx ] -> isHorizontal ) { if ( point . x < m_vpSegments [ sIdx ] -> bbox . min . x ) curDist = m_vpSegments [ sIdx ] -> bbox . min . DistSqr ( point ) ; else if ( point . x > m_vpSegments [ sIdx ] -> bbox . max . x ) curDist = m_vpSegments [ sIdx ] -> bbox . max . DistSqr ( point ) ; else curDist = fabsf ( point . y - m_vpSegments [ sIdx ] -> bbox . max . y ) ; curDist *= curDist ; } else if ( m_vpSegments [ sIdx ] -> k == 0.0f ) { if ( point . y < m_vpSegments [ sIdx ] -> bbox . min . y ) curDist = m_vpSegments [ sIdx ] -> bbox . min . DistSqr ( point ) ; else if ( point . y > m_vpSegments [ sIdx ] -> bbox . max . y ) curDist = m_vpSegments [ sIdx ] -> bbox . max . DistSqr ( point ) ; else curDist = fabsf ( point . x - m_vpSegments [ sIdx ] -> b ) ; curDist *= curDist ; } else { a2DPoint intersection ; float b2 , k2 ; k2 = -1.0f / m_vpSegments [ sIdx ] -> k ; b2 = point . y - k2 * point . x ; intersection . x = ( b2 - m_vpSegments [ sIdx ] -> b ) / ( m_vpSegments [ sIdx ] -> k - k2 ) ; intersection . y = k2 * intersection . x + b2 ; if ( intersection . x < m_vpSegments [ sIdx ] -> bbox . min . x ) if ( m_vpSegments [ sIdx ] -> k < 0 ) curDist = point . DistSqr ( m_vpSegments [ sIdx ] -> bbox . min . x , m_vpSegments [ sIdx ] -> bbox . max . y ) ; else curDist = point . DistSqr ( m_vpSegments [ sIdx ] -> bbox . min ) ; else if ( intersection . x > m_vpSegments [ sIdx ] -> bbox . max . x ) if ( m_vpSegments [ sIdx ] -> k < 0 ) curDist = point . DistSqr ( m_vpSegments [ sIdx ] -> bbox . max . x , m_vpSegments [ sIdx ] -> bbox . min . y ) ; else curDist = point . DistSqr ( m_vpSegments [ sIdx ] -> bbox . max ) ; else curDist = intersection . DistSqr ( point ) ; } if ( curDist < distMin ) distMin = curDist ; } } return cry_sqrtf ( distMin ) / m_Proximity ; }
calculates min distance from point within area to the border of area
returns fade coefficient: Distance/m_Proximity
[0 - on the very border of area,       1 - inside area, distance to border is more than m_Proximity]
calculates min distance from point within area to the border of area returns fade coefficient : Distance / m_Proximity [ 0 - on the very border of area 1 - inside area distance to border is more than m_Proximity ]