How to determine if a vector is between two other vectors?

How to determine if a vector is between two other vectors?

It’s not clear what you actually want. Do you want to know if the X coordinate of the blue circle is greater than A and less than B? Because that’s what I get from your extremely vague question.

boolean isWhatYouWant = (blue.x > a.x && blue.x < b.x) 
1 Like

@jayfella Or he might mean:

boolean isWhatYouWant = (blue.x > a.x && blue.x < b.x) && (blue.y > a.y && blue.y < b.y)

Assuming the camera is looking down the Z axis. Or he can just be asking “How long is a piece of string?” … I wish people explained their intended use case clearly.

@yn97 can you please answer this important question first… What are you trying to do?

1 Like

not sure what you exactly mean, but i see square there so i think you mean if its within it. if not, then your question is not clear, because between points can mean you compare X or Y or both or point need to be on line between this points.

its all about comparing line functions, i have some old code for polygons.

trully you just compare 4 line function here. x=0, y=0, x=10, y=10 for example. if point is above x=0 and lower than x=10 and above y=0 and below y=10 then point is inside your square with coords(0,10,0,10)

if you want to check if point is on line between points, then you just check if it belong to function fro mthis 2 points.

its just mathematics

something from below might help you

 * @author oxplay
 * calculate function for x or y and based on decision point, will verify next points if on same line function side. 
 * In short, decide if point is on up/down/left/right side of line and decide if this is correct.(used to verify if point belong to polygon)
 * Attention! It will NOT work properly for concave polygon
public class PlotLineFunction {

    public Float ySlope;
    public Float yOffset;
    public Float xOffset;
    public boolean usingY = true;
    public boolean shouldBeOnAboveSide = false;
    public boolean skip = false;
    protected Vector2f polygonCenterPoint;
    private static final Logger LOGGER = Logger.getLogger(PlotLineFunction.class.getName());

    public PlotLineFunction(Vector2f previousPoint, Vector2f point, Vector2f polygonCenterPoint) {
        this.polygonCenterPoint = polygonCenterPoint;
        Vector2f pointA = previousPoint.clone().subtract(polygonCenterPoint);
        Vector2f pointB = point.clone().subtract(polygonCenterPoint);
        float diffX = Math.abs(pointA.x - pointB.x);
        float diffY = Math.abs(pointA.y - pointB.y);
        if(diffX == 0 && diffY == 0){
            LOGGER.log(Level.SEVERE, "PlotLineFunction: Points have same position!");
            skip = true;
        } else {
            if (diffX > diffY) {
                usingY = true;
                ySlope = diffY / diffX;
                yOffset = pointA.y;
                xOffset = pointA.x;
                shouldBeOnAboveSide = 0 > calculateValue(0);
            } else {
                usingY = false;
                ySlope = diffX / diffY;
                yOffset = pointA.x;
                xOffset = pointA.y;
                shouldBeOnAboveSide = 0 > calculateValue(0);
    public boolean isPointOnGoodSide(Vector2f vect){
        Vector2f newVect = vect.clone().subtract(polygonCenterPoint);
        //System.out.println("new Vect " + newVect);
            return true;
            float value = calculateValue(newVect.x);
            return (newVect.y > value) == shouldBeOnAboveSide || newVect.y == value;
        } else {
            float value = calculateValue(newVect.y);
            return (newVect.x > value) == shouldBeOnAboveSide || newVect.x == value;
    private float calculateValue(float val){
        return yOffset + (ySlope * (val - xOffset));


public boolean isPointWithinPlot(Vector2f pointToCheck){
    for (int i = 0; i < lineFunctions.size(); i++) {
        PlotLineFunction lineFunction = lineFunctions.get(i);
            return false;
    return true;

its not all code but i provide just 4+ vectors and i get plot(polygon) to check if points are within

1 Like

What are you ACTUALLY trying to do? Like, why do you need this?

1 Like

I want to check is there a sand bag between position of two soldier that are firing to each other or not. then soldier must move to position of sand bag and shelter behind it.

if you want check if its in line of fire, you can just use Raycast.

if you want to check in 2D if its in cone of view, then you compare 2 line functions(you can use some code i provided) and you check 4 points (all sandbag)

1 Like

A dot product with the relative corners would tell you also. Way less math.

To me I see a few issues anyway:

  1. the concept of “between” is a bit fuzzy.
  2. from an AI perspective, it would not matter if the sandbags are between or not… if the sandbags are close (even if behind) then running for cover is still a good option.
    …which eliminates the entire question.

imo it should base on multiple conditions.

  1. sandbag very close
  2. sandbag in cone view of NPC1 and closer to NPC1 than NPC2(or any other enemy) (avoid both of them go to sandbag same time)
  3. sandbag on enemy flank(tactical move)

etc etc its up to developer :slight_smile:

the best would be to determine all positive tactical points (not just sandbags) to hide and shoot. working with many enemies and many allies. calculate profitability for this points (like could be shot by enemy while move, etc , etc) so npc choose best options