rectangle-overlap
Advanced tools
Comparing version 1.0.0 to 2.0.0
32
index.js
@@ -1,7 +0,27 @@ | ||
module.exports = function(x1, y1, w1, h1, x2, y2, w2, h2) { | ||
var dx = (x1 < x2) ? x1 + w1 - x2 : x2 + w2 - x1; | ||
if (dx < 0) return 0; | ||
var dy = (y1 < y2) ? y1 + h1 - y2 : y2 + h2 - y1; | ||
if (dy < 0) return 0; | ||
return dx * dy; | ||
"use strict"; | ||
function getOverlap(rectangle1, rectangle2) { | ||
const intersectionX1 = Math.max(rectangle1.x, rectangle2.x); | ||
const intersectionX2 = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width); | ||
if (intersectionX2 < intersectionX1) { | ||
return null; | ||
} | ||
const intersectionY1 = Math.max(rectangle1.y, rectangle2.y); | ||
const intersectionY2 = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle2.height); | ||
if (intersectionY2 < intersectionY1) { | ||
return null; | ||
} | ||
return new Rectangle(intersectionX1, intersectionY1, intersectionX2 - intersectionX1, intersectionY2 - intersectionY1); | ||
} | ||
class Rectangle { | ||
constructor(x, y, width, height) { | ||
this.x = x; | ||
this.y = y; | ||
this.width = width; | ||
this.height = height; | ||
} | ||
get area() { | ||
return this.width * this.height; | ||
} | ||
} | ||
module.exports = getOverlap; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsU0FBUyxVQUFVLENBQUMsVUFBc0IsRUFBRSxVQUFzQjtJQUNoRSxNQUFNLGNBQWMsR0FBRyxJQUFJLENBQUMsR0FBRyxDQUFDLFVBQVUsQ0FBQyxDQUFDLEVBQUUsVUFBVSxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQzVELE1BQU0sY0FBYyxHQUFHLElBQUksQ0FBQyxHQUFHLENB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biAgcmVhZG9ubHkgeTogbnVtYmVyO1xuICByZWFkb25seSB3aWR0aDogbnVtYmVyO1xuICByZWFkb25seSBoZWlnaHQ6IG51bWJlcjtcbn1cblxuY2xhc3MgUmVjdGFuZ2xlIGltcGxlbWVudHMgSVJlY3RhbmdsZSB7XG4gIGNvbnN0cnVjdG9yKFxuICAgIHB1YmxpYyByZWFkb25seSB4OiBudW1iZXIsXG4gICAgcHVibGljIHJlYWRvbmx5IHk6IG51bWJlcixcbiAgICBwdWJsaWMgcmVhZG9ubHkgd2lkdGg6IG51bWJlcixcbiAgICBwdWJsaWMgcmVhZG9ubHkgaGVpZ2h0OiBudW1iZXIsXG4gICkge1xuICB9XG5cbiAgZ2V0IGFyZWEoKTogbnVtYmVyIHtcbiAgICByZXR1cm4gdGhpcy53aWR0aCAqIHRoaXMuaGVpZ2h0O1xuICB9XG59XG4iXX0= |
{ | ||
"name": "rectangle-overlap", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Computes the area of the intersection of two rectangles.", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"check-workspace": "git diff --exit-code >/dev/null && git diff --cached --exit-code >/dev/null", | ||
"build": "rm -rf dist/ && npm run compile && cp LICENSE README.md package.json dist/", | ||
"compile": "tsc --project .", | ||
"test": "npx tslint --project . && npx jest && npx stryker run", | ||
"prepublishOnly": "echo 'Use npm run publish-package' && exit 1", | ||
"publish-package": "npm run check-workspace && npm test && npm run build && git push && git push --tags && npm publish --access=public --ignore-scripts dist" | ||
}, | ||
@@ -18,10 +24,25 @@ "repository": { | ||
"difference", | ||
"geometry" | ||
"geometry", | ||
"intersection" | ||
], | ||
"author": "Ophir LOJKINE", | ||
"license": "GPLv2", | ||
"license": "LGPL-3.0-or-later", | ||
"bugs": { | ||
"url": "https://github.com/lovasoa/rectangle-overlap/issues" | ||
}, | ||
"homepage": "https://github.com/lovasoa/rectangle-overlap" | ||
"homepage": "https://github.com/lovasoa/rectangle-overlap", | ||
"devDependencies": { | ||
"@stryker-mutator/core": "^2.0.0", | ||
"@stryker-mutator/html-reporter": "^2.0.0", | ||
"@stryker-mutator/jest-runner": "^2.0.0", | ||
"@stryker-mutator/typescript": "^2.0.0", | ||
"@types/jest": "^24.0.15", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.0.2", | ||
"tslint": "^5.18.0", | ||
"typescript": "^3.5.2" | ||
}, | ||
"jest": { | ||
"preset": "ts-jest" | ||
} | ||
} |
# rectangle-overlap | ||
Fastly compute the area of the intersection of two rectangles. | ||
[![TypeScript support](https://img.shields.io/npm/types/rectangle-overlap.svg)](https://github.com/lovasoa/rectangle-overlap/blob/master/index.ts) | ||
[![License](https://img.shields.io/npm/l/rectangle-overlap.svg)](https://github.com/lovasoa/rectangle-overlap/blob/master/LICENSE) | ||
Fastly compute the intersection of two rectangles. | ||
## Usage | ||
```js | ||
var area = require("rectangle-overlap"); | ||
const intersection = require("rectangle-overlap"); | ||
area(rect1.x, rect1.y, rect1.w, rect1.h, rect2.x, rect2.y, rect2.w, rect2.h); | ||
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"); | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
5
42
0
27
0
14456
9