# Detecting if a rectangle is fully in a 2D plain

I am creating a placement system, the ‘canvas’ is an odd shape, as shown below. How, mathematically, can I detect if the entire part is within the canvas?

Currently all of my potential solutions are not very elegant, this includes adding ‘walls’ to the edges of the canvas or casting a ray from the corners of the part and of the canvas to ensure it is in the canvas.

Note: I am not able to use AABB collision detecting because the parts may be rotated. I can also not check to see if the corners of the parts are within the canvas because it may be over a corner as shown below.

The black outline is the canvas and the red and green objects are the parts.

1 Like

I’d suggest using Region3. The documentation can be found here. I found a post similar to yours here. The only difference is that the post is talking about one block, but it shouldn’t be an issue if you can check if the part is fully in either of your parts.

If you want your life to be a bit easier, be sure to check out the RotatedRegion3 module. I also found post that solves the same problem as yours using the module.

1 Like

An R3 would not work because the part could be over multiple R3 therefore checking to see if the part is completely in the region would not work if you wanted to put it on the border, between 2 regions.

EDIT: This may help visualise the problem better.

The dotted line would be between 2 regions and the yellow box should be allowed, but wouldn’t because it is not fully in a region.

1 Like

Imo Region3 is a good idea. In the example you provided you could construct two region3 that do overlap. Then check if the part/object is inside one of these. However, as @OnlyJaycbee observed, this can be annoying for more complex canvas.

A more expensive (in terms of performance) way to do this would be using GetTouchingParts (canvas/walls are made of parts, right?) If the part intersects with the walls, then it would be incorrectly placed.

1 Like

Currently I have no walls, because the canvas is dynamic (think Lumber Tycoon 2 Style), while it would be possible I think that it could be annoying and not a great solution.

As far as overlapping Region3’s go, while it does sound like a good idea there is a case where someone may still have a part overlapping both regions as shown below.

1 Like

Since you have a concave bounding box, the problem becomes a little harder. The naive approach, and most direct way of handling this arbitrarily, is to do two things.

First, run a pass where you determine if the 4 vertices of your rectangle are bounded inside of the concave hull. You can just google-around for some algorithms for this. This initial pass just lets you know if your object is relatively “inside” of the bounding box.

If the rectangle’s vertices are inside of the bounding box, the second part is to then determine if there are any edge intersections. This takes care of the cases where the vertices can all be inside, but the rectangle still clips. Like above, lots of algorithms exist for this.

By dividing your initial problem into two smaller and generalized sub-problems, we can then use pre-existing algorithms and techniques to solve this problem.

2 Likes

Compare the positions of the vertices and raycast between all edges. If all the corners are within the bounds and the rays don’t detect any hits it should mean it’s fully inside I think.

1 Like