Is a point between two points?
After Googling and looking at the Stack for a long time, I was able to find methods to determine if a point falls on a line that connects two points. Unfortunately, this is not what I need.
Please see the image at the end of this question. I apologize in advance for the horrible picture, but she gets the point (get her?) Through.
I need to create two perpendicular lines to the one that connects points x and y. They must intersect with a perpendicular line at points x and y. Then I need to indicate if the z point exists between these two lines or not.
Any help is appreciated. Thank you for your time!
source to share
To find if point Z falls within the desired lane, you can determine if the projection of the Z line to XY is between these points. Define the vectors v = Y - X and w = Z - X . The projection lies in the XY segment if the b parameter is in the range 0..1. A very simple formula:
b = DotProduct ( w, v ) / DotProduct ( v, v )
source to share
Sample code in 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??
are incorrect as they are actually the square of the distance of each one.
source to share
Thus, the string can be described y = mx + b
. Let's say that your first line is presented y = 2x-1
. Then for any point (u, v) you can connect x=u
so that y = 2u-1
. This allows you to define y
for the x position on your line. Therefore, if v>y
, then your point is above the line. Otherwise, your point is below the line.
By doing this with two parallel lines, you get three cases, obvious from your image:
- y is greater than the corresponding points on both lines
- y is greater than the corresponding point on one of your lines
- y is less than the corresponding point on both lines
EDIT:
Reading some of the comments on your post, it looks like there might be better ways than this.
source to share
Basically, this is a math problem, not a programming problem. (Or, to put it another way, when you understand math, programming is trivial.)
I can think of two ways to do this:
-
Work out 2 perpendicular lines going through x and y. If z is above the perpendicular line x and below the perpendicular line y.
-
Work out the angle between yxz and the angle between xyz. If both angles are less than 90 degrees, then z is between the lines.
source to share