Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
rect-scaler
Advanced tools
Find largest rectangle and square sizes when fitting them into a container
A set of javascript functions for calculating how large a set of equally sized squares or rectangles can be to fit within an arbitrary rectangular container, to cover it as fully as possible.
Useful for graphical layouts where you need to space items in a nice way. This algorithm does not allow for rotations, and is not generic bin packing.
Install from npm:
npm install rect-scaler
Pass the size of the container and the number of squares that need to be placed to largestSquare()
,
resulting in an object containing the optimal solution.
const { largestSquare } = require('rect-scaler');
const containerWidth = 100;
const containerHeight = 100;
const numSquares = 8;
const { rows, cols, width, height, area } = largestSquare(containerWidth, containerHeight, numSquares);
Pass the size of the container and the number of rectangles that need to be placed, along with the size of an (unscaled) rectangle that needs to be placed, resulting in an object containing the optimal solution.
const { largestRect } = require('rect-scaler');
const containerWidth = 100;
const containerHeight = 100;
const numRects = 8;
const rectWidth = 10;
const rectHeight = 2;
const result = largestRect(containerWidth, containerHeight, numRects, rectWidth, rectHeight);
mocha test
Inspired by this question on Math StackExchange.
Erich's Packing Center is also pretty interesting, for mathematical thought behind more complex versions of this.
MIT, see LICENSE.md for details.
FAQs
Find largest rectangle and square sizes when fitting them into a container
The npm package rect-scaler receives a total of 967 weekly downloads. As such, rect-scaler popularity was classified as not popular.
We found that rect-scaler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.