using System;
using System.ComponentModel;
namespace CoordinateSharp {
///
/// Cartesian (X, Y, Z) Coordinate
///
[Serializable]
public class Cartesian : INotifyPropertyChanged {
///
/// Create a Cartesian Object
///
///
public Cartesian(Coordinate c) {
//formulas:
this.x = Math.Cos(c.Latitude.ToRadians()) * Math.Cos(c.Longitude.ToRadians());
this.y = Math.Cos(c.Latitude.ToRadians()) * Math.Sin(c.Longitude.ToRadians());
this.z = Math.Sin(c.Latitude.ToRadians());
}
///
/// Create a Cartesian Object
///
/// X
/// Y
/// Z
public Cartesian(Double xc, Double yc, Double zc) {
//formulas:
this.x = xc;
this.y = yc;
this.z = zc;
}
///
/// Updates Cartesian Values
///
///
public void ToCartesian(Coordinate c) {
this.x = Math.Cos(c.Latitude.ToRadians()) * Math.Cos(c.Longitude.ToRadians());
this.y = Math.Cos(c.Latitude.ToRadians()) * Math.Sin(c.Longitude.ToRadians());
this.z = Math.Sin(c.Latitude.ToRadians());
}
private Double x;
private Double y;
private Double z;
///
/// X Coordinate
///
public Double X {
get => this.x;
set {
if (this.x != value) {
this.x = value;
this.NotifyPropertyChanged("X");
}
}
}
///
/// y Coordinate
///
public Double Y {
get => this.y;
set {
if (this.y != value) {
this.y = value;
this.NotifyPropertyChanged("Y");
}
}
}
///
/// Z Coordinate
///
public Double Z {
get => this.z;
set {
if (this.z != value) {
this.z = value;
this.NotifyPropertyChanged("Z");
}
}
}
///
/// Returns a Lat Long Coordinate object based on the provided Cartesian Coordinate
///
/// X
/// Y
/// Z
///
public static Coordinate CartesianToLatLong(Double x, Double y, Double z) {
Double lon = Math.Atan2(y, x);
Double hyp = Math.Sqrt(x * x + y * y);
Double lat = Math.Atan2(z, hyp);
Double Lat = lat * (180 / Math.PI);
Double Lon = lon * (180 / Math.PI);
return new Coordinate(Lat, Lon);
}
///
/// Returns a Lat Long Coordinate object based on the provided Cartesian Coordinate
///
/// Cartesian Coordinate
///
public static Coordinate CartesianToLatLong(Cartesian cart) {
Double x = cart.X;
Double y = cart.Y;
Double z = cart.Z;
Double lon = Math.Atan2(y, x);
Double hyp = Math.Sqrt(x * x + y * y);
Double lat = Math.Atan2(z, hyp);
Double Lat = lat * (180 / Math.PI);
Double Lon = lon * (180 / Math.PI);
return new Coordinate(Lat, Lon);
}
///
/// Cartesian Default String Format
///
/// Cartesian Formatted Coordinate String
/// Values rounded to the 8th place
public override String ToString() => Math.Round(this.x, 8).ToString() + " " + Math.Round(this.y, 8).ToString() + " " + Math.Round(this.z, 8).ToString();
///
/// Property changed event
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// Notify property changed
///
/// Property name
public void NotifyPropertyChanged(String propName) {
if (this.PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
}