1. Overview
1.概述
When working with 2D geometry, one common problem is to determine whether a point lies between two other points on a straight line.
在处理二维几何图形时,一个常见问题是确定一个点是否位于一条直线上其他两点之间。
In this quick tutorial, we’ll explore different approaches to making this determination in Java.
在本快速教程中,我们将探讨在 Java 中做出这种判断的不同方法。
2. Understanding the Problem Statement
2.了解问题陈述
Let’s say we’ve two points on a plane: the first point A has the coordinates (x1, y1), and the second point B has the coordinates (x2, y2). We want to check whether a given point C with (x3,y3) coordinates lies between A and B or not:
假设平面上有两点:第一点 A 的坐标为 (x1, y1),第二点 B 的坐标为 (x2, y2)。我们希望检查坐标为 (x3,y3) 的给定点 C 是否位于 A 和 B 之间:
In the above graph, point C lies between points A and B, whereas point D does not lie between points A and B.
在上图中,C 点位于 A 点和 B 点之间,而 D 点不在 A 点和 B 点之间。
3. Using the Distance Formula
3.使用距离公式
This approach involves calculating the distances: AC, CB, and AB from point A to C, point C to B, and point A to B, respectively. If C lies between points A and B, then the sum of AC and CB will be equal to AB:
这种方法涉及计算距离:AC、CB 和 AB 分别从点 A 到 C、点 C 到 B 以及点 A 到 B。如果 C 位于点 A 和点 B 之间,那么 AC 和 CB 的和将等于 AB:
distance (AC) + distance (CB) == distance (AB)
We can use the distance formula to calculate the distance between two different points. If point A has the coordinates (x1, y1), and point B has the coordinates (x2, y2), then we can calculate the distance by using the formula:
我们可以使用距离公式来计算两个不同点之间的距离。如果点 A 的坐标为 (x1,y1),点 B 的坐标为 (x2,y2),那么我们可以使用公式计算距离:
distance = sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1))
Let’s use the distance formula on the above diagram to verify the approach:
让我们用上图中的距离公式来验证一下这种方法:
Distance from point A (1,1) to point B (5,5) = 5.656
从点 A (1,1) 到点 B (5,5) 的距离 = 5.656
Distance from point A (1,1) to point C (3,3) = 2.828
从 A 点(1,1)到 C 点(3,3)的距离 = 2.828
Distance from point C (3,3) to point B (5,5) = 2.828
C 点(3,3)到 B 点(5,5)的距离 = 2.828
Here, distance (AC) + distance (CB) = 5.656, which is equal to distance (AB). It shows that point C lies between point A and point B.
这里,距离 (AC) + 距离 (CB) = 5.656,等于距离 (AB)。这表明 C 点位于 A 点和 B 点之间。
Let’s use the distance formula to check whether a point lies between two points or not:
让我们用距离公式来检验一个点是否位于两点之间:
boolean isPointBetweenTwoPoints(double x1, double y1, double x2, double y2, double x, double y) {
double distanceAC = Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2));
double distanceCB = Math.sqrt(Math.pow(x2 - x,2) + Math.pow(y2 - y, 2));
double distanceAB = Math.sqrt(Math.pow(x2 - x1,2) + Math.pow(y2 - y1, 2));
return Math.abs(distanceAC + distanceCB - distanceAB) < 1e-9;
}
Here, 1e-9 is a small eplison value used to account for rounding errors and imprecisions that can occur in floating-point calculations. If the absolute difference is very small (less than 1e-9), we’ll consider it as equality.
这里,1e-9 是一个很小的等差数列值,用于考虑浮点计算中可能出现的四舍五入误差和不精确度。如果绝对差值非常小(小于 1e-9),我们会将其视为相等。
Let’s test this approach using the above values:
让我们用上述数值来测试一下这种方法:
void givenAPoint_whenUsingDistanceFormula_thenCheckItLiesBetweenTwoPoints() {
double x1 = 1;<br /> double y1 = 1;<br />
double x2 = 5;<br /> double y2 = 5;<br />
double x = 3;<br /> double y = 3;
<br /> assertTrue(findUsingDistanceFormula(x1, y1, x2, y2, x, y));
}
4. Using the Slope Formula
4.使用斜率公式
In this approach, we’ll be calculating the slope of the lines AB and AC using the slope formula. We’ll compare these slopes to check the collinearity, i.e., the slope of AB and AC are equal. It will help us determine whether points A, B, and C are aligned or not.
在这种方法中,我们将使用斜率公式计算直线 AB 和 AC 的斜率。我们将比较这些斜率来检查共线性,即 AB 和 AC 的斜率相等。这将帮助我们确定 A、B 和 C 三点是否对齐。
If point A has the coordinates (x1, y1), and point B has the coordinates (x2, y2), then we can calculate the slope by using the formula:
如果点 A 的坐标为(x1, y1),点 B 的坐标为(x2, y2),那么我们可以用公式计算斜率:
slope = (y2 - y1) / (x2 - x1)
If the slopes of AB and AC are equal, and point C lies within the x and y coordinate range defined by A and B points, we can say that point C lies between point A and point B.
如果 AB 和 AC 的斜率相等,且点 C 位于 A、B 两点所定义的 x 和 y 坐标范围内,我们就可以说点 C 位于点 A 和点 B 之间。
Let’s calculate the slope of AB and AC on the above diagram to verify the approach:
让我们来计算上图中 AB 和 AC 的斜率,以验证这种方法:
Slope of AB = 1.0
AB 的斜率 = 1.0
Slope of AC = 1.0
AC 的斜率 = 1.0
Point C is (3,3)
C 点为 (3,3)
Here, AB = AC, and Point C’s x and y coordinates lie between the range defined by A (1,1) and B (5,5), which shows that point C lies between point A and point B.
这里,AB = AC,C 点的 x 坐标和 y 坐标位于 A(1,1)和 B(5,5)所定义的范围之间,这表明 C 点位于 A 点和 B 点之间。
Let’s use this approach to check whether a point lies between two points or not:
让我们用这种方法来检查一个点是否位于两点之间:
boolean findUsingSlopeFormula(double x1, double y1, double x2, double y2, double x, double y) {
double slopeAB = (y2 - y1) / (x2 - x1);
double slopeAC = (y - y1) / (x - x1);
return slopeAB == slopeAC && ((x1 <= x && x <= x2) || (x2 <= x && x <= x1)) && ((y1 <= y && y <= y2) || (y2 <= y && y <= y1));
}
Let’s test this approach using the above values:
让我们用上述数值来测试一下这种方法:
void givenAPoint_whenUsingSlopeFormula_thenCheckItLiesBetweenTwoPoints() {
double x1 = 1;<br /> double y1 = 1;<br />
double x2 = 5;<br /> double y2 = 5;<br />
double x = 3;<br /> double y = 3;
<br /> assertTrue(findUsingSlopeFormula(x1, y1, x2, y2, x, y));
}
5. Conclusion
5.结论
In this tutorial, we’ve discussed ways to determine whether a point lies between two other points on a straight line.
在本教程中,我们讨论了确定一个点是否位于直线上其他两点之间的方法。
As always, the code used in the examples is available over on GitHub.
一如既往,示例中使用的代码可在 GitHub 上获取。