[英]Is a point between two points?
After Googling and looking on Stack for a long time, I've only managed to find methods for determining whether or not a point falls on the line that connects two points. 经过谷歌搜索并在Stack上查看了很长时间后,我只是设法找到一个方法来确定一个点是否落在连接两个点的线上。 This, unfortunately, isn't what I need. 不幸的是,这不是我需要的。
Please see the image at the end of this question. 请参阅此问题末尾的图像。 I apologize in advance for the terrible picture, but it gets the point (get it?) across. 我提前为这张可怕的照片道歉,但它得到了重点(得到它?)。
I need to create two perpendicular lines to the one that joins the points x and y. 我需要创建两条垂直线,连接点x和y。 They need to intersect with the perpendicular line at points x and y. 它们需要在点x和y处与垂直线相交。 I then need to then tell if point z appears between those two lines or not. 然后,我需要判断点z是否出现在这两行之间。
Any help is appreciated. 任何帮助表示赞赏。 Thanks for your time! 谢谢你的时间!
Compute the angle xyz and yxz. 计算角度xyz和yxz。 If either is > 90 then it is outside. 如果其中一个> 90,则它在外面。
Since you tagged your question with java
, here you go: 既然你用java
标记了你的问题,那你就去:
import javafx.geometry.Point2D;
....
// is z between parallel lines
boolean betweenLines(Point2D x, Point2D y, Point2D z) {
return x.angle(y,z) < 90 && y.angle(x,z) < 90;
}
To find if Z point falls in needed stripe, you can determine if projection of Z to XY line falls between these points. 要查找Z点是否落在所需条带中,您可以确定Z到XY线的投影是否落在这些点之间。 Define vectors v = Y - X and w = Z - X . 定义向量v = Y - X和w = Z - X. Projection lies in XY segment if parameter b falls in range 0..1. 如果参数b落在0..1范围内,则投影位于XY段中。 Very simple formula: 非常简单的公式:
b = DotProduct( w, v ) / DotProduct( v, v ) b = DotProduct( w,v )/ DotProduct( v,v )
Example code in JavaScript: JavaScript中的示例代码:
// JavaScript function to determine if infinite strip generated
// by x and y contains the point z
// point structure is:
// {
// double x;
// double y;
// }
// returns true or false
function stripContainsPoint(x, y, z) {
var distXZ = (x.x - z.x) * (x.x - z.x) + (x.y - z.y) * (x.y - z.y),
distXY = (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y),
distYZ = (y.x - z.x) * (y.x - z.x) + (y.y - z.y) * (y.y - z.y);
// if triangle is right or acute, or obtuse with hypotenuse XY, returns true
return (distXZ + distXY >= distYZ) && (distYZ + distXY >= distXZ);
}
The variables dist??
变量dist??
are misnomers, as they are actually the distance squared of each. 是误称,因为它们实际上是每个的距离平方。
So a line can be described by y = mx + b
. 因此,可以通过y = mx + b
来描述线。 Lets say your first line is represented by y = 2x-1
. 让我们说你的第一行用y = 2x-1
。 Then for any point (u,v), you can plug x=u
so that y = 2u-1
. 然后对于任何点(u,v),你可以插入x=u
,使得y = 2u-1
。 That allows you to determine y
for the x position on your line. 这允许您确定线上x位置的y
。 Hence, if v>y
, then your point is above the line. 因此,如果v>y
,那么你的点就在线之上。 Otherwise, your point is below the line. 否则,你的观点就在线下。
By doing this with two parallel lines, you'll end up with the three cases obvious from your picture: 通过使用两条平行线进行此操作,您最终将从图片中看出三个案例:
EDIT: 编辑:
Reading some of the comments on your post, it sounds like there may be better ways than this :) 阅读你的帖子上的一些评论,听起来可能有比这更好的方法:)
Basically, this is a maths problem not a programming problem. 基本上,这是一个数学问题而不是编程问题。 (Or to put it another way, once you understand the maths, the programming is trivial.) (换句话说,一旦你理解了数学,编程就变得微不足道了。)
I can think of two ways to do this: 我可以想到两种方法:
Work out the 2 perpendicular lines passing through x and y. 计算出穿过x和y的2条垂直线。 If z is above the x perpendicular line and below the y perpendicular line. 如果z高于x垂直线并低于y垂直线。
Work out the angle between yxz and the angle between xyz. 计算出yxz与xyz之间的角度之间的角度。 If both angles are less than 90 degrees, then z is between the lines. 如果两个角度都小于90度,则z在线之间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.