/// <summary> /// Calculates the closest Coordinate to a line created by two other Coordinates. The resulting Coordinate is not garunteed /// to fall between the two endpoint Coordinates. /// </summary> /// <param name="endpoint1">A Coordinate on the line.</param> /// <param name="endpoint2">Another Coordinate on the line.</param> /// <returns>The closest Coordinate on the line to this Coordinate.</returns> public Coordinate ClosestPoint(Coordinate endpoint1, Coordinate endpoint2) { double R = 3956; Point3D ep1cart = new Point3D( R * Math.Cos(DegToRad(endpoint1.latitude)) * Math.Cos(DegToRad(endpoint1.longitude)), R * Math.Cos(DegToRad(endpoint1.latitude)) * Math.Sin(DegToRad(endpoint1.longitude)), R * Math.Sin(DegToRad(endpoint1.latitude))); Point3D ep2cart = new Point3D( R * Math.Cos(DegToRad(endpoint2.latitude)) * Math.Cos(DegToRad(endpoint2.longitude)), R * Math.Cos(DegToRad(endpoint2.latitude)) * Math.Sin(DegToRad(endpoint2.longitude)), R * Math.Sin(DegToRad(endpoint2.latitude))); Point3D ptcart = new Point3D( R * Math.Cos(DegToRad(this.latitude)) * Math.Cos(DegToRad(this.longitude)), R * Math.Cos(DegToRad(this.latitude)) * Math.Sin(DegToRad(this.longitude)), R * Math.Sin(DegToRad(this.latitude))); Point3D origin = new Point3D(0, 0, 0); double d = ((ptcart - ep1cart).CrossProduct(ptcart - ep2cart)).Magnitude() / (ep2cart - ep1cart).Magnitude(); double hypotenuse = ptcart.DistanceTo(ep1cart); double theta = Math.Asin(d/hypotenuse); double adjacent = d / Math.Tan(theta); Point3D closestcart = ep1cart.MoveTowards(ep2cart, adjacent); Point3D surfacecart = origin.MoveTowards(closestcart, R); return new Coordinate( (int)(RadToDeg(Math.Asin(surfacecart.Z / R)) * 1E6), (int)(RadToDeg(Math.Atan2(surfacecart.Y, surfacecart.X)) * 1E6)); }
/// <summary> /// Calculates the Euclidian distance between this point and another point. /// </summary> /// <param name="p">The point to measure the distance to.</param> /// <returns>The distance between this point and point p.</returns> public double DistanceTo(Point3D p) { return Math.Sqrt(Math.Pow(p.X - this.X, 2) + Math.Pow(p.Y - this.Y, 2) + Math.Pow(p.Z - this.Z, 2)); }
/// <summary> /// Calculate the result of moving this point a given distance towards another point. /// </summary> /// <param name="p">The point to move towards.</param> /// <param name="distance">The distance to move. It is possible to move past either endpoint.</param> /// <returns>A point representing the result of moving this point towards the other.</returns> public Point3D MoveTowards(Point3D p, double distance) { Point3D directionVector = p - this; directionVector = directionVector / directionVector.Magnitude(); return (directionVector * distance) + this; }
/// <summary> /// Calculates the cross product of two points as vectors positioned on the origin. /// </summary> /// <param name="p">The point to cross with.</param> /// <returns>The cross product of the points.</returns> public Point3D CrossProduct(Point3D p) { return new Point3D( this.Y * p.Z - this.Z * p.Y, this.Z * p.X - this.X * p.Z, this.X * p.Y - this.Y * p.X); }