Check if one rectangle intersects or is within another rectangle - java

I have the following situation, for illustrative purposes split into 2 situations.
The green rectangle in the middle is the rectangle that I am referring to as my base, since it is the rectangle that is constant.
Now I'd like to know if a yellow rectangle is intersecting, is inside or fully encapsulates the green rectangle.
I've seen this post and understand it, but (unless I forget something) it ignores the case of a yellow rectangle being inside the green rectangle. The simplest solution I can think of is to double check the 4 points in either order, but is that the only("best") solution?

Two rectangles A, B have a non-empty intersection iif
A.l < B.r and A.r > B.l and A.t < B.b and A.b > B.t
(left, right, top, bottom coordinates, y downward).
A wholly contains B iif
A.l <= B.l and A.r >= B.r and A.t <= B.t and A.b >= B.b

Related

How to find if two rectangles intersect with the top left and bottom right points?

Two rectangles intersect if they are over lapping. Edges and corners count as an intersection. If only given the points for the top left and bottom right of both rectangles. How does one find out if they are intersecting.
This is a Python solution but it is simple enough.
Let (ax1,ay1), (bx1,by1) and (ax2,ay2),(bx2,by2) be the top left and bottom right points of first and second rectangle respectively. To check whether they intersect or not you can simply compare the x and y coordinates for left-right and top-bottom respectively. For example, if ax1>=bx2 it means the first rectangle is to the left of the second rectangle and so on.
def rec_intersect(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
if ax1 >= bx2 or bx1 >= ax2 or ay1 >= by2 or by1 >= ay2:
return True
return False

How to optimize this solution to avoid exceeding the time limit?

This is my solution to this question:
Given a circle represented as (radius, x_center, y_center) and an
axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1)
are the coordinates of the bottom-left corner, and (x2, y2) are the
coordinates of the top-right corner of the rectangle.
Return True if the circle and rectangle are overlapped otherwise
return False.
In other words, check if there are any point (xi, yi) such that
belongs to the circle and the rectangle at the same time.
class Solution {
public boolean checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
for(int i=x1; i<=x2; ){
for(int j=y1; j<=y2; ){
System.out.println((Math.pow(i-x_center, 2) +" "+ Math.pow(j-y_center, 2))+" "+Math.pow(radius, 2));
System.out.println(i+" "+j);
if((Math.pow(i-x_center, 2)+Math.pow(j-y_center, 2))<=Math.pow(radius, 2)){
return true;
}
j += 1;
}
i += 1;
}
return false;
}
}
I am pretty confident that the logic is correct. Ranging from the bottom left corner of the rectangle to the top right point, for every point, I am checking if it lies inside the circle.
If I increase the increment step to anything beyond '1', I see that the code fails for the test cases where the rectangle and circle just 'touch' each other. But having it this way leads to exceeding the time limit in some cases. How do I optimize the code for this logic?
Thanks in advance.
This problem can be simplified. I've found a solution of time complexity O(1) and memory complexity O(1). Instead of checking for every single pixel from the rectangle, you can even only take into consideration the bounds themselves.
As I see it, there are 3 cases:
Circle is fully inside rectangle.
Rectangle is fully inside circle
Circle and rectangle outlines intersect in at least one point. This is the tougher one.
I'll call center of the circle coordinates x0 and y0.
You can simply check the bounding box for the circle, as in, which is the northern most point of the circle(x0,y0-radius), which is the southern-most point of the circle(x0,y0+radius), eastern-most(x0-radius,y0) and western-most(x0+radius,y0). If they all fall within the rectangle, then problem solved.
If a rectangle is fully within a circle, that definitely means that its corners are at a smaller distance from the center of the circle than the radius. Simply check this distance for each corner.
Now comes the hard part.
So, as you have figured out, being in a circle(or intersecting one) means that some point must be at a smaller or equal distance from the center than the radius.
However, we can do an optimization for the rectangle checking part, too. Intersection with a rectangle likely means intersection with at least one of the segments that make the outline of the rectangle. So, in the case of an intersection between a rectangle and a circle, you need to check if there is any segment that would be at a smaller or equal distance from the center of the circle than the radius.
Edit: Also, the reason that your code fails on tests where they barely touch is likely due to floating-point errors. Do not use == (or in this case <=, which is similar) for checking if two floating point(or even a floating-point and integer) values are the same. Math.pow() in Java returns double. Just use normal multiplication for squaring.
In fact, you might want to stay as far from floating-point as possible, unless you can't figure out how to get rid of them and the problem says "0.001 error is acceptable" or something around the lines. They are both slow and prone to errors.
Edit 2: Also, I've written the code to help you aid in understanding this explanation. I've tested it on the site, it works for every test with runtime 1ms and memory usage 37.7Mb.
class Solution {
public boolean checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
//case 1: circle is fully inside rectangle
//check the bounding box of the circle against the rectangle
if(x_center-radius>=x1&&y_center-radius>=y1
&&x_center+radius<=x2&&y_center+radius<=y2
)
return true;
//case 2: checking closest corner against circle bounds
int minX=(Math.abs(x_center-x1)>Math.abs(x_center-x2))?(x_center-x2):(x_center-x1);
int minY=(Math.abs(y_center-y1)>Math.abs(y_center-y2))?(y_center-y2):(y_center-y1);
if(minX*minX+minY*minY<=radius*radius)
return true;
//case 3: checking distances to segments against circle bounds
//Checking distance from a segment to a point is alike checking
//the distance from the line the segment is part of to a point,
//except you have to check if the closest point from the segment
//is actually on the segment. If it isn't, the distance from a
//segment to a point is the minimum distance from one of its
//corners to the point.
if(x1<=x_center&&x_center<=x2)
if(minY*minY<=radius*radius)
return true;
if(y1<=y_center&&y_center<=y2)
if(minX*minX<=radius*radius)
return true;
return false;
}
}
This code could be possibly shortened. However, time complexity-wise, it can't get better than O(1).

Reflecting pixels on a line

The aim is to 'move' (some of) the pixels in a square sub-area (square(x,y)), of an image.
I do this by going through each horizontal line in that square and working out which will be moved. So moving them in an inverse way.
Given I have a horizontal line of pixels in my square (x,y)
A..............B.........C
After applying a transformation, the pixels after B till C will turn black. The pixel at C 'moves' to A, and the pixels in between B and P are moved and scaled linearly from A to B.
This means that A to B pixels are a stretched reflection of B to C. But the pixel at B stays the same.
My problem is working out the positions of A and C, which would be the right hand side and left hand side of the square (x,y).
So far, I have this: size is half of length or width of square
// check if square(x,y) lies within image
if ( (x<image.getWidth()) && (y<image.getHeight()) ){
if (( y<=0) && (y >= image.getHeight())){ //not sure whether these if's are right
for (int i=0; i<x-size; i++) {// visiting each pixel in square(x,y) at coordinate (i,j)
for(int j=0; j< y-size; j++){
// set values for A(left hand side), C(right hand side), B(x +n)
int A=
int B=
int B= x+n;
From what I can understand, you have a small square area inside a bigger image, but you're running a "for loop" through the entire image instead of the square (which I assume is moving across the screen from some keyboard commands or mouse movement).
In the case of mouse movement, your mouse would be centered in the square, at a position (x,y). In each horizontal line of pixels in the square, your A would be (x - (square.width/2)), your B would be (x + n), and your C would be (x + (square.width/2).

libGDX, detect side touching between rectangles (side collision)

I use libGDX library for my game. I user overlap method for detecting collision detection between two rectangles.
...
if (r1.overlaps(r2)) collisionTest();
...
I want to detect touching side on rectangle (top, bottom, left or right):
r1 overlap r2 on the left side
Can anyone give me code for this, but this need to be fast method.
Thanks
You can use the method intersectRectangles provided in the Intersector class to determine if two rectangles are overlapping, and if so, where they overlap. You could use this info to determine if they overlap with the left, right, top, and/or bottom.
Rectangle r1 = /*Initialize*/;
Rectangle r2 = /*Initialize*/;
Rectangle intersection = new Rectangle();
Intersector.intersectRectangles(r1, r2, intersection);
if(intersection.x > r1.x)
//Intersects with right side
if(intersection.y > r1.y)
//Intersects with top side
if(intersection.x + intersection.width < r1.x + r1.width)
//Intersects with left side
if(intersection.y + intersection.height < r1.y + r1.height)
//Intersects with bottom side

Rectangle intersection shared edge

I'm trying to determine whether two rectangles border each other. If they share an edge or part of an edge, then I want to include them, if they only share a vertice then I don't.
I've tried using android android.graphics.Rect, I was hoping that the intersect method would return true giving me a rectangle, with 0 width but the points of the intersecting edge. I'm using andEngine and also tried the collideswith method of org.andengine.entity.primitive.Rectangle however that returns true, even if the rectangle only share one corner vertice.
Is there a nice way of doing this? The only other way I can think of is to try and create a collection of all the edges then see if they're equal or are in someway partly equal.
Here's an image to demonstrate what I want. If I click on rect 1 then I want to return rects 2,3 and 4, but not 5.
"Map":
It sounds like you need a new class to do this. I would take the coordinates of each corner of the rectangles. Then, when you are selecting a rectangle, you can get those adjacent to it by finding them one side at a time. Starting with the top for an example, you check which other rectangles have corners at the same height. From that list, you check to see which ones exist on at least one point between the two top corners. So, if top left is 0,3 and top right is 4,3 then you would look for the list of corners at y=3. From that list you find all corners where 0<=x<=4 and anything that fits will be adjacent. You then do the same thing for each additional side. It should be an easy class to make, but I am not going to write any code as I do not know anything about how you stored your data or how you would reference this in your code. If you need help with that, write a comment.
Write a function to find which rectangles share edges with rectangles within all considered rectangles.
Then, map these rectangles which share edges to one another. An Adjacency List is just a way of representing a graph in code.
Sometimes code is easier to understand, so here's code. I have not tested this, but it should get you most the way there.
Also, I'm not sure what you're end goal is here but here's a question I answered that deals with rectangular compression.
List<Rectangle> allRectangles;
public boolean shareAnEdge(Rectangle r1, Rectangle r2){
int y1 = r1.y + r1.height;
int y2 = r2.y+r2.height;
int x1 = r1.x+r1.width;
int x2 = r2.x+r2.width;
boolean topShared = (y1 == r2.y && r2.x == r1.x);
boolean bottomShared = (y2 == r2.y && r2.x==r1.x);
boolean rightShared = (x1 == r2.x && r2.y==r1.y);
boolean leftShared = (x2 == r1.x && r2.y==r1.y);
if (topShared || bottomShared || rightShared || leftShared) {
return true;
}
return false;
}
public List<Rectangle> findSharedEdgesFor(Rectangle input){
List<Rectangle> output = new List<Rectangle>();
for(Rectangle r : allRectangles){
if(r!=input && shareAnEdge(r, input)){
output.add(r);
}
}
}
public AdjacencyList createGraph(List<Rectangle> rectangles){
AdjacencyList graph = new AdjacencyList();
for(Rectangle r : rectangles){
List<Rectangle> sharedEdges = findSharedEdgesFor(r);
for(Rectangle shared : sharedEdges){
graph.createEdgeBetween(r, shared);
}
}
}

Categories