Home » Java » Finding if the x and y coordinate is within a view is enclosed by two intersecting rectangles, in java-Exceptionshub

Finding if the x and y coordinate is within a view is enclosed by two intersecting rectangles, in java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Say I have this view with two subviews A and B, A and B has a color property mColor,

enter image description here

I want a function in the parent view that has this signature: int getColor(int x, int y), which for any given x and y coordinate, return the color at that position, if the x and y coordinates land and the two shapes are overlapped, return the average color of A and B

The problem I am running into is i see myself doing a lot of conditional checks, checking if A is left of B or if A is right of B etc. I feel like I am missing some key intuition

I have a Point class that handles the coordinates”

public class Point {
    private final int mX;
    private final int mY;

    public Point(int mX, int mY) {
        this.mX = mX;
        this.mY = mY;
    }

    public int getX() {
        return mX;
    }

    public int getY() {
        return mY;
    }
}

Here is my subview class:

public class Chart  {

    private int mColor;
    private Point mTopLeft;
    private Point mBottomRight;

    public Point getTopLeft() {
        return mTopLeft;
    }

    public Point getBottomRight() {
        return mBottomRight;
    }

    public Chart(int mColor, Point topLeft, Point bottomRight) {
        this.mColor = mColor;
        this.mTopLeft = topLeft;
        this.mBottomRight = bottomRight;
    }

    public int getColor() {
        return mColor;
    }

    public Point getTopRightCorner() {
        return new Point(getBottomRight().getX(), getTopLeft().getY());
    }

    public Point getBottomLeftCorner() {
        return new Point(getTopLeft().getX(), getBottomRight().getY());
    }
}

My parent view class:

public class View {

    private Chart mChartA;
    private Chart mChartB;

    public View(Chart chartA,
                Chart chartB) {
        mChartA = chartA;
        mChartB = chartB;
    }

    public boolean doChartsOverlap() {
        boolean isOverlapped = true;

        if(isChartALeftOfChartBInX() || isChartARightOfChartBInX()) {
            isOverlapped = false;
        }

        if(isChartABottomOfChartBInY() || isChartATopOfChartBInY()) {
            isOverlapped = false;
        }
        return isOverlapped;
    }

    public final boolean isChartALeftOfChartBInX() {
        return mChartA.getBottomRight().getX() <= mChartB.getTopLeft().getX();
    }

    public final boolean isChartARightOfChartBInX() {
        return mChartA.getTopLeft().getX() >= mChartB.getBottomRight().getX();
    }

    public final boolean isChartATopOfChartBInY() {
        return mChartA.getBottomRight().getY() <= mChartB.getTopLeft().getY();
    }

    public final boolean isChartABottomOfChartBInY() {
        return mChartA.getTopLeft().getY() >= mChartB.getBottomRight().getY();
    }

    public void setChartA(Chart mChartA) {
        this.mChartA = mChartA;
    }

    public void setChartB(Chart mChartB) {
        this.mChartB = mChartB;
    }
}
How to&Answers:

Logically, you want to test if the point is in both rectangles; so test if it is in one rectangle and also in the other rectangle. Everything else about whether one rectangle is above, below, left or right of the other is a red herring.

Add this method to the Chart class (use < instead of <= if you only want to paint the interiors):

public boolean contains(Point p) {
    return mTopLeft.getX() <= p.getX() && p.getX() <= mBottomRight.getX()
        && mTopLeft.getY() <= p.getY() && p.getY() <= mBottomRight.getY();
}

And this method to the View class:

public int getColor(Point p) {
    boolean inA = mChartA.contains(p), inB = mChartB.contains(p);
    if(inA && inB) {
        // ...
    } else if(inA) {
        // ...
    } else if(inB) {
        // ...
    } else {
        // ...
    }
}

You can then overload public int getColor(int x, int y) to return getColor(new Point(x, y)).

Answer:

Rectangle r1 = new Rectangle(x1, y1, width1, height1 );
Rectangle r2 = new Rectangle(x2, y2, width2, height2);
if ( r1.contains( x3, y3 ) && r2.contains( x3, y3 ) ) {
   // (x3,y3) is in both rectangles
}