2d-density 中文文档教程

发布于 7年前 浏览 25 项目主页 更新于 3年前

2d density

一种算法,可用于查找可装入具有预定义间距的网格的最大单元格数。 换句话说,二维密度。

Build Status

CircleCI

Install

npm install 2d-density

Usage

const twoDDensity = require('2d-density');

const gridWidth = 100;
const gridHeight = 100;
const spacing = 3;

const result = twoDDensity.findPercentFull(gridWidth, gridHeight, spacing);

宽度、高度和间距都应该是整数。

间距不计算对角线。 因此,对角相邻的单元格实际上间隔 2 个单元格。

Sample output

{
  count: 2000,
  percent: 0.2
}

Visual example

假设您有一个宽度为 5 且高度为 4 的网格,并且您想要填充彼此相距 2 的单元格。 您最终可能会得到这样

xxx
xx
xxx
xx

的结果:计数为 10,已满百分比为 0.5。 对于 2 的间距,这将非常容易计算,但是当使用奇数间距时,事情会变得更加复杂。

例如,间距为 3

xx
x
xx

的 5 x 5 网格。计数仅为 5,百分比为 0.2。

2d density

An algorithm than can be used to find the maximum number of cells that can fit into a grid with a predefined spacing. In other words two-dimensional density.

Build Status

CircleCI

Install

npm install 2d-density

Usage

const twoDDensity = require('2d-density');

const gridWidth = 100;
const gridHeight = 100;
const spacing = 3;

const result = twoDDensity.findPercentFull(gridWidth, gridHeight, spacing);

Width, height and spacing should all be whole numbers.

Spacing does not count diagonals. Thus a cell that is diagonally adjacent is really spaced 2 cells away.

Sample output

{
  count: 2000,
  percent: 0.2
}

Visual example

Say you have a grid with a width of 5 and a height of 4 and you want to fill cells that are 2 away from each other. You might end up with something like this:

xxx
xx
xxx
xx

The count would be 10 and percentage full would be 0.5. This would be pretty easy to calculate for spacing of 2 but things get more complex when using spacings with odd numbers.

For example a 5 by 5 grid with spacing of 3.

xx
x
xx

The count is only 5 and the percentage would be 0.2.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文