Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:45:35+00:00 2026-06-02T07:45:35+00:00

I searched but I could not find a complete answer. In C# if at

  • 0

I searched but I could not find a complete answer.
In C# if at all possible.
I need the shortest distance between a WGS point and a WGS point defined line segment on a sphere (Earth exactly).

float DistanceInKilometres(PointF LineStartA, PointF LineEndB, PointF ThePoint)

EDIT: Perhaps an illustration would help

enter image description here

Please note that this is an ideal example. ‘The point’ could be anywhere on the surface of the sphere, the segment start-end, too. Obviously, I’m not looking for the distance through the sphere. Math isn’t my stronger side, so I don’t understand normalize or to cartesian. Maybe I should also note that path AB, is the shortest possible, and Distance?, is the shortest possible too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:45:36+00:00Added an answer on June 2, 2026 at 7:45 am

    You can use the spherical law of cosines:

    • http://en.wikipedia.org/wiki/Spherical_law_of_cosines
    • http://mathworld.wolfram.com/SphericalSegment.html
    • http://mathworld.wolfram.com/SphericalTrigonometry.html

    You will have to use the earth’s radius for calculations:

    EARTH_RADIUS_KM = 6371;

    Here, from my contributions to OsmMercator.java, from openstreetmap.org:

    /**
     * Gets the distance using Spherical law of cosines.
     *
     * @param la1 the Latitude in degrees
     * @param lo1 the Longitude in degrees
     * @param la2 the Latitude from 2nd coordinate in degrees
     * @param lo2 the Longitude from 2nd coordinate in degrees
     * @return the distance
     */
    public static double getDistance(double la1, double lo1, double la2, double lo2) {
        double aStartLat = Math.toRadians(la1);
        double aStartLong = Math.toRadians(lo1);
        double aEndLat =Math.toRadians(la2);
        double aEndLong = Math.toRadians(lo2);
    
        double distance = Math.acos(Math.sin(aStartLat) * Math.sin(aEndLat)
                + Math.cos(aStartLat) * Math.cos(aEndLat)
                * Math.cos(aEndLong - aStartLong));
    
        return (EARTH_RADIUS_KM * distance);
    }
    

    All you need to do is find the closest point with dot product and use that with the distance equation.

    Here’s the closest point example:

    double[] nearestPointSegment (double[] a, double[] b, double[] c)
    {
       double[] t= nearestPointGreatCircle(a,b,c);
       if (onSegment(a,b,t))
         return t;
       return (distance(a,c) < distance(b,c)) ? a : c;
    }
    
    • How to calculate distance from a point to a line segment, on a sphere?
    • http://en.wikipedia.org/wiki/Great-circle_distance

    Keep in mind the units haven’t been explicitly declared. When dealing with points in space there’re are a variety of ways to determine position. The main thing is you have to nail down your units to a consistent type.

    When working with position on the earth, I mainly use lat/long coordinates and vectors for magnitude/direction. There’re are several known types to use for vectors and earth’s position. Among them are the following:

    • Earth-centered earth-fixed (ECEF) coordinate system
    • North-East-Down (NED)
    • Geodetic coordinate system

    For your example, I might consider sticking to Geodetic.

    Now, bringing this together, you might have some pseudo code which looks like this:

    Where a Vector is made up of Geodetic coordinates:
    class Vector {
     double x=0.0; //latitude
     double y=0.0; //longitude
     double h=0.0; //height
    ...
    }
    
    public Vector closestPoint(Vector lineStartA, Vector lineEndB, final Vector thePoint ) {
        Vector w = thePoint.subtract(lineStartA);
        double proj = w.dot(lineEndB);
        // endpoint 0 is closest point
        if ( proj <= 0.0f )
            return lineStartA;
        else
        {
            //Vector square 
            double vsq = lineEndB.dot(lineEndB);
            // endpoint 1 is closest point
            if ( proj >= vsq )
                return lineStartA.add(lineEndB);
            else
                return lineStartA.add(lineEndB.multiply(proj/vsq));
        }
    }      
    
    double DistanceInKilometres(Vector lineStartA, Vector lineEndB, Vector thePoint) {
      Vector cp=closestPoint(lineStartA, lineEndB, thePoint);
      return getDistance(cp.x, cp.y, thePoint.x, thePoint.y);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched google but could not find an answer to what I think
I have searched the web but could not find an answer. how do I
I just searched the web but could not find a good answer to this:
I searched for a solution but could not find an answer for my specific
I've searched and searched google for an answer but could not find one. I
I have searched but could not find the reason for this behavior. I have
I searched a lot but could not find a way to dump table relations
I've searched for an answer and found some c#-examples, but could not get this
I searched the site but I could not find any solution to this problem.
I searched around a bit, but could not find anything useful. Could someone help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.