0

There is an image container with height 5000px and width 5000px.

And there are also N images (in form of rectangle) randomly placed in this container. Each of these images has 4 properties, positionX, positionY (they are the coordinates of where the upper left corner of the image is in the container), height and width.

The question is, what is the most efficient way to check if there are any of these images overlapping with each other in the container? I am going to write the function with either C# or JavaScript.

I was thinking to use an 5000 x 5000 array (int) with starting value 0, then put these images one by one in the array (add 1 to all the elements in the array where image is). If there are any elements in the array turns to 2, return false.

It doesn't seem that efficient by this way.

Ferry
  • 583
  • 8
  • 18
  • Check out the explanation in the [overlap tag info](https://stackoverflow.com/tags/overlap/info) on the math you need to use for checking if two elements overlap. Then translate that one dimensional test to a two dimensional test. – Zohar Peled Aug 04 '21 at 06:51
  • Does this answer your question? [Overlapping rectangles](https://stackoverflow.com/questions/39519775/overlapping-rectangles) – Sinatr Aug 04 '21 at 07:52

3 Answers3

2

Checking if two rectangles overlap is fairly cheep. So if N is small (say 10k) you should just be able to check each combination. This would be O(N^2), but if you have a bound on 'N' that should not be a problem.

If you have more rectangles an R-Tree might be appropriate. This creates a tree of minimum bounding rectangles that should allow for O(n log n) complexity.

Marking an array as you propose is also possible, and should allow for O(n) complexity if the size and position of the rectangles is bounded. But the constant factor might be larger than the other methods.

JonasH
  • 28,608
  • 2
  • 10
  • 23
2

Use a line-sweep based algorithm.

https://www.hackerearth.com/practice/math/geometry/line-sweep-technique/tutorial/

You have to think about which state to associate with the sweep line.

One consideration for instance would to be that a rectangle that has the left side on the sweep line (which I assume is vertical and goes left to right) cannot intersect rectangles whose right side is left of the sweep line.

You would also track all rects that intersect the sweep line.

citykid
  • 9,916
  • 10
  • 55
  • 91
0

"It doesn't seem that efficient by this way." - haha! (Sorry, no offense though) You are going to run almost 5000 * 5000 checks (loops), which is definitely not worth the work.

Edit:

Sorry, the previous algorithm does not work.

I know this question is closed, but anyways:

This was a method I used in my 3D game to check for collision detection. I tried to change it to fit this situation. Hopefully, it works this time...

    public boolean intersects(Image other) { // This should be your image class where you store data like its width, height, startPos, endPos
    
    float thisSizeX = Math.abs(WIDTH) / 2.0f; //Math.abs returns absolute value.
    float thisSizeY = Math.abs(HEIGHT) / 2.0f;
    
    
    float otherSizeX = Math.abs(other.WIDTH) / 2.0f;
    float otherSizeY = Math.abs(other.HEIGHT) / 2.0f;
    
    float thisCenterX = endPositionX - thisSizeX;
    float thisCenterY = endPositionY - thisSizeY;
    
    float otherCenterX = other.endPositionX - otherSizeX;
    float otherCenterY = other.endPositionY - otherSizeY;
    
    //check the X axis
    if(Math.abs(thisCenterX - otherCenterX) < thisSizeX + otherSizeX)
       {
          //check the Y axis
          if(Math.abs(thisCenterY - otherCenterY) < thisSizeY + otherSizeY)
          {
              
              return true;
          }
       }

       return false;
       
       }

**This is written in Java but should be fairly easy to port to other languages.

Dstarred
  • 321
  • 2
  • 10
  • 2
    @SnipingPoodle Your algorithm is just wrong. Imagine `image[0]` has coordinates (startx/starty, endx, endy) `[10,11, 20, 21]` and `image[1]` has coordinates `[1,2,5,6]` . I hope we both recognize, these two are not overlapping. Yet your approach will return `true` because `10 > 5` and `11 > 6` – derpirscher Aug 04 '21 at 08:07
  • still wrong. imagine image[0] = [0,1, 10,11] and image1 = [5,6, 8,9], they are clearly overlapping, yet your algorithm won't return true, because all conditions 0 > 8, 5 > 0, 1 > 9, 8 > 10 are false ... – derpirscher Aug 04 '21 at 08:29
  • Ok... sorry again... clearly this algorithm is not working, wait edit it completely. – Dstarred Aug 04 '21 at 12:33