text: #include "MathPCH.h"
  
  using namespace DirectX;
  
  const float SlitherMathLibrary::Infinity = FLT_MAX;
  
  float SlitherMathLibrary::AngleFromXY(float x, float y)
  {
  	float theta = 0.0f;
  
  	if (x > 0.0f)
  	{
  		theta = atanf(y / x);
  
  		if (theta < 0.0f)
  			theta += 2.0f * PI;
  	}
  
  	else
  		theta = atanf(y / x) + PI;
  
  	return theta;
  }
  
  DirectX::XMVECTOR SlitherMathLibrary::RandUnitVec3()
  {
  	XMVECTOR One = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f);
  	XMVECTOR Zero = XMVectorZero();
  
  	// Keep trying until we get a point on/in the hemisphere.
  	while (true)
  	{
  		// Generate random point in the cube [-1,1]^3.
  		XMVECTOR v = XMVectorSet(RandF(-1.0f, 1.0f), RandF(-1.0f, 1.0f), RandF(-1.0f, 1.0f), 0.0f);
  
  		// Ignore points outside the unit sphere in order to get an even distribution 
  		// over the unit sphere.  Otherwise points will clump more on the sphere near 
  		// the corners of the cube.
  
  		if (XMVector3Greater(XMVector3LengthSq(v), One))
  			continue;
  
  		return XMVector3Normalize(v);
  	}
  }
  
  DirectX::XMVECTOR SlitherMathLibrary::RandHemisphereUnitVec3(DirectX::XMVECTOR n)
  {
  	XMVECTOR One = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f);
  	XMVECTOR Zero = XMVectorZero();
  
  	// Keep trying until we get a point on/in the hemisphere.
  	while (true)
  	{
  		// Generate random point in the cube [-1,1]^3.
  		XMVECTOR v = XMVectorSet(RandF(-1.0f, 1.0f), RandF(-1.0f, 1.0f), RandF(-1.0f, 1.0f), 0.0f);
  
  		// Ignore points outside the unit sphere in order to get an even distribution 
  		// over the unit sphere.  Otherwise points will clump more on the sphere near 
  		// the corners of the cube.
  
  		if (XMVector3Greater(XMVector3LengthSq(v), One))
  			continue;
  
  		// Ignore points in the bottom hemisphere.
  		if (XMVector3Less(XMVector3Dot(n, v), Zero))
  			continue;
  
  		return XMVector3Normalize(v);
  	}
  }
  