rectangle-overlap

Computes the area of the intersection of two rectangles.

Usage no npm install needed!

<script type="module">
  import rectangleOverlap from 'https://cdn.skypack.dev/rectangle-overlap';
</script>

README

rectangle-overlap

TypeScript support License

Fastly compute the intersection of two rectangles.

Usage

const intersection = require("rectangle-overlap");

let rect1 = {x: 0, y: 0, width: 10, height: 10};
let rect2 = {x: 2, y: 3, width: 42, height: 42};

const overlap = intersection(rect1, rect2);

if (overlap) {
  console.log(`The rectangles overlap over an area of ${overlap.area}`);
  console.log(
    `Intersection coordinates: x=${overlap.x}, y=${overlap.y}, width=${overlap.width}, height=${overlap.height}`,
  )
} else {
  console.log("The rectangles do not overlap");
}