Over a million developers have joined DZone.

How to Find a Point Coordinates Between Other Two Points

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Theory: Given a segment from point A to point B, our goal is to find a C point coordinates at a specified distance on that segment. 

Let’s say that DT is the distance of the AB segment and D is the distance from point A to point C.
Using the distance formula, we can find the distance from point A to point B:
Now, we can easily find the C point coordinates like this:

That’s it. From Java perspective, the mechanism looks like this:
 package calculatelinepoints;

public class CalculateLinePoints {

    public static void main(String[] args) {
        // point A coordinates
        double a = 20.0;
        double b = 10.0;

        // point B coordinates
        double c = 50.0;
        double d = 40.0;

        // calculate distance between the two points
        double DT = Math.sqrt(Math.pow((c - a), 2) + Math.pow((d - b), 2));

        double D = 20.0; // distance to point C
        double x;
        double y;
        double T = D / DT;
        // finding point C coordinate
        x = (1 - T) * a + T * c;
        y = (1 - T) * b + T * d;
        System.out.println("Point C coordinates:\n  x: " + x + "\n  y: " + y);


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Published at DZone with permission of A. Programmer. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}