# Intersection of two rectangles

**URL:** <https://discourse.processing.org/t/intersection-of-two-rectangles/39212>\
**Category:** Beginners\
**Created:** [October 11, 2022, 10:29am UTC](https://discourse.processing.org/t/intersection-of-two-rectangles/39212 "2022-10-11T10:29:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shachar](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Shachar](https://discourse.processing.org/u/Shachar)\
**Post date:** [October 11, 2022, 10:29am UTC](https://discourse.processing.org/t/intersection-of-two-rectangles/39212/1 "2022-10-11T10:29:57Z")

</div>

Hi everyone!  
I’ve tried to create a rectangle class and check if two rectangles intersect. Their x and y values represent the centers. My intersection condition looks like this:

> r1.x+r1.w/2 \< r2.x-r2.w/2 || r2.x+r2.w/2 \< r1.x-r1.w/2 ||  
> r1.y-r1.h/2 \< r2.y+r2.h/2 || r2.y-r2.h/2 \< r1.y+r1.h/2

but for some reason, it always returns true. I would highly appreciate it if you can help me find the mistake.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 11, 2022, 10:35am UTC](https://discourse.processing.org/t/intersection-of-two-rectangles/39212/2 "2022-10-11T10:35:59Z")

</div>

Processing Java Mode Rect-Rect: [Collision Detection](http://JeffreyThompson.org/collision-detection/rect-rect.php)  
Flavor p5js Rect-Rect: [Collision Detection](http://CrHallberg.com/CollisionDetection/Website/rect-rect.html)

---

<div class="post-metadata">

**Author:** ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)\
**Post date:** [October 11, 2022, 12:06pm UTC](https://discourse.processing.org/t/intersection-of-two-rectangles/39212/3 "2022-10-11T12:06:31Z")

</div>

In case you want to dive deeper into intersections this article by gorillasun is a fantastic resource:

> **[An Algorithm for Polygon Intersections](https://gorillasun.de/blog/an-algorithm-for-polygon-intersections)**
>
> In this post we’ll work our way towards an algorithm that can not only compute the intersection between two rotated rectangles and convex polygons. We’ll also have a look at the case where the rectangles are axis-aligned, a function that can...

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/9/f973cca8c2f36e6fc97ca7c51c37c1abdf0ef8da.png)
