new-horizons/NewHorizons/Utility/CoordinateUtilities.cs
2022-01-25 00:33:04 -05:00

24 lines
715 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace NewHorizons.Utility
{
public static class CoordinateUtilities
{
public static Vector3 CartesianToSpherical(Vector3 v)
{
float dist = Mathf.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
float latitude = (Mathf.Rad2Deg * Mathf.Acos(v.z / dist));
float longitude = 180f;
if (v.x > 0) longitude = Mathf.Rad2Deg * Mathf.Atan(v.y / v.x);
if (v.x < 0) longitude = Mathf.Rad2Deg * (Mathf.Atan(v.y / v.x) + Mathf.PI);
return new Vector3(longitude, latitude, dist);
}
}
}