Point Methods to test if a point lies between two other points

These arose while I was working on a web app for the Canal and River Trust.
On computers with location services (GPS etc.), I wanted to place a marker on a navigation diagram between two features (typically locks) so that the user could see his/her current location.
The diagram is schematic, not a scaled map, so I needed a function to test if the current location was between two known locations.
Navigations do not normally travel in a straight line, so a rough test was required to allow for deviations from the straight line.

Three methods are provided to test if a point (p0) lies between two other points p1 and p2.

The mathematical background can be viewed here.

The code can be viewed here.

Point.isBetween1(p1, p2)

The method tests whether this point (p0) lies within the circle centred midway between p1 and p2 with radius half the distance between p1 and p2.

The circle passes through p1 and p2. Points p1 and p2 are considered to be within the circle. Change the <= test to a < test to exclude points on the circle.

Point.isBetween2(p1, p2, e)

This method tests whether this point (p0) lies within the ellipse (of eccentricity e) centred midway between p1 and p2.

Points p1 and p2 are the end points of the major axis of the ellipse and are considered to be within the ellipse. Change the <= test to a < test to exclude points on the ellipse.

Point.isBetween3(p1, p2, e)

Point.isBetween3 is essentially the same as Point.isBetween2. The code has been modified to eliminate the Math.sqrt calls.

Choose point 1 (x,y)


Choose point 2 (x,y)


Choose point 0 (x,y)


Set eccentricity

by major-axis to minor-axis ratio:
or by direct entry of e in [0,1):

Diagram

The diagram displays the points and the circle and ellipse used in the calculation.
Note that the y-coordinate increases downwards on the diagram.



Log Output




Table of eccentricities for various major-axis to minor-axis ratios (a)

a	1/a		eccentricity
1	1.0000000	0.0000000
2	0.5000000	0.8660254
3	0.3333333	0.9428090
4	0.2500000	0.9682458
5	0.2000000	0.9797959
6	0.1666667	0.9860133
7	0.1428571	0.9897433
8	0.1250000	0.9921567
9	0.1111111	0.9938080
10	0.1000000	0.9949874
11	0.0909091	0.9958592
12	0.0833333	0.9965217
13	0.0769231	0.9970370
14	0.0714286	0.9974457
15	0.0666667	0.9977753
16	0.0625000	0.9980450
17	0.0588235	0.9982684
18	0.0555556	0.9984556
19	0.0526316	0.9986140
20	0.0500000	0.9987492

Enter a value for 1/a or for eccentricity

1/a: eccentricity:
                                 

Last updated Thursday, August 29, 2019.