简体   繁体   中英

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. 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. They need to intersect with the perpendicular line at points x and y. I then need to then tell if point z appears between those two lines or not.

Any help is appreciated. Thanks for your time!

点图。

Compute the angle xyz and yxz. If either is > 90 then it is outside.

Since you tagged your question with java , here you go:

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. Define vectors v = Y - X and w = Z - X . Projection lies in XY segment if parameter b falls in range 0..1. Very simple formula:

b = DotProduct( w, v ) / DotProduct( v, v )

在此输入图像描述

Example 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 misnomers, as they are actually the distance squared of each.

So a line can be described by y = mx + b . Lets say your first line is represented by y = 2x-1 . Then for any point (u,v), you can plug x=u so that y = 2u-1 . That allows you to determine y for the x position on your line. Hence, 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'll end up with the three cases obvious from your picture:

  1. y is greater than the corresponding points on both lines
  2. y is greater than the corresponding point on one of your lines
  3. y is less than the corresponding point on both lines

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:

  1. Work out the 2 perpendicular lines passing through x and y. If z is above the x perpendicular line and below the y perpendicular line.

  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM