glm::vec3 VirtualTrackball::getClosestPointOnUnitSphere(int x, int y) {
	glm::vec2 normalized_coords;
	glm::vec3 point_on_sphere;
	float k;

	normalized_coords = getNormalizedWindowCoordinates(x, y);
	
	
	// Finding the point on the sphere

	k = sqrtf(normalized_coords.x*normalized_coords.x + normalized_coords.y*normalized_coords.y);

	if (k <= 0.5)
	{
		point_on_sphere.x = 2 * normalized_coords.x;
		point_on_sphere.y = 2 * normalized_coords.y;
		point_on_sphere.z = sqrtf(1 - 4 * (k * k));
	}
	else
	{
		point_on_sphere.x = normalized_coords.x / k;
		point_on_sphere.y = normalized_coords.y / k;
		point_on_sphere.z = 0;
	}

	

	//std::cout << "Point on sphere: " << point_on_sphere.x << ", " << point_on_sphere.y << ", " << point_on_sphere.z << std::endl;

	return point_on_sphere;
}
glm::vec3 VirtualTrackball::getClosestPointOnUnitSphere(int x, int y) {
	glm::vec2 n_coord = getNormalizedWindowCoordinates(x, y);
	glm::vec3 point_on_sphere;
	float r, k, z_outside, z_inside;

	r = 0.5f;														//Radius
	k = sqrt( pow2(n_coord.x) + pow2(n_coord.y) );					//square root of x^2 + y^2
	z_outside = (pow2(r) / 2.0f) / k;								// Z-position outside of circle
	z_inside = sqrt( 1.0f - 4.0f * pow2(k));						// Z-position inside of circle

	//Mouse is outside of circle
	if(k > pow2(r) / 2.0f){
		point_on_sphere = glm::vec3( n_coord.x * 2.0f, n_coord.y * 2.0f, z_outside);
	}
	//Mouse is inside of circle
	else{
		point_on_sphere = glm::vec3( 2.0f * n_coord.x, 2.0f * n_coord.y, z_inside);
	}

	return point_on_sphere;
}