Q
安排n個對象的
1
A
回答
6
採取平方根的小區?
import math
ceil(14**(.5))
它返回:
>>> from math import ceil
>>> ceil(14**(.5))
4.0
2
獲取項目數的平方根:
n = 14^0.5 ~ 3.7417
四捨五入爲最接近的整數:
在Pythonn = ceil(14^0.5) = 4
,直到你用完現在只需安排的n個項目行的項目。
1
鑑於n
對象,所得到的「廣場」的大小是ceil(sqrt(n))
,有專門的快捷案件,其中n = 0(不畫任何東西)或n = 1(只是一個1x1平方)。
而且,問題的標題是有點誤導。你永遠不能在一個NxN方安排N個對象,除非N爲1
2
那要看情況。假定列的數目應該大於行數等於或大於以下計算的列數(在僞代碼):
Ceiling(Sqrt(n))
其中n
是項數。
1
如果您希望將網格居中,您需要知道您將提前多少行,該函數(它是javascript,但我認爲它應該可以輕鬆移植到另一種語言)將生成一個網格的位置和支持居中和間距。
在僞代碼
columns = Ceiling(Sqrt(n))
rows = (columns - 1) if (Round(Sqrt(n)) < columns) else columns
如果center
是false
,該功能會產生這樣的事情(o
爲原點):
+———————+
|oxx |
|xxx |
|xx |
| |
| |
+———————+
如果center
是true
+———————+
| |
| xxx |
| xox |
| x |
| |
+———————+
代碼
/**
* Creates a grid of positions for the given items count and size.
* It assumes items anchor are centered.
*
* @param {number} x - The grid x position
* @param {number} y - The grid y position
* @param {number} count - The number of positions you wish to create
* @param {number} size - The item size
* @param {boolean} [center=true] - If true wil center the grid according to `x`, `y`
* @param {number} [spacing=0] - Item spacing
* @return {Array.<{x: number, y: number}>} The generated positions
*/
export const makeGrid = (x, y, count, size, { center = true, spacing = 0 } = {}) => {
// avoid computing trivial cases
if (count === 0) return []
if (count === 1) return [{ x, y }]
const sqrt = Math.sqrt(count)
const columns = Math.ceil(sqrt)
const rows = Math.round(sqrt) < columns ? columns - 1 : columns
const sizeAndSpacing = size + spacing
let offsetX = x
let offsetY = y
// if grid is centered apply offset according to `columns` and `rows`
if (center === true) {
offsetX -= (columns - 1) * sizeAndSpacing * .5
offsetY -= (rows - 1) * sizeAndSpacing * .5
}
let column = 0
let row = 0
return _.range(count).map(() => {
// if we're on last row's first column and grid is centered
if (row === rows - 1 && column === 0 && center === true) {
// if last row's items doesn't completely fill the line
// we apply an extra offset to center them
const modulus = count % columns
if (modulus > 0) {
offsetX += (columns - modulus) * sizeAndSpacing * .5
}
}
const pos = {
x: offsetX + column * sizeAndSpacing,
y: offsetY + row * sizeAndSpacing,
}
column++
if (column === columns) {
column = 0
row++
}
return pos
})
}
相關問題
- 1. n個對象的排列(帶有重複項的排列)
- 2. 具有n個線程和n個相應對象的ExecutorService
- 3. n個對象的等價測試
- 4. 獲取N個對象的值
- 5. 對兩個對象的數組排序
- 6. 是否有一個R函數來獲取n個對象的排列數k p(n,k)?
- 7. 如何在(n)hibernate中排序子對象的屬性?
- 8. 如何枚舉R中k = 1的n個二元對象的所有排序?
- 9. 刪除N個隨機對象Django orm
- 10. Limit!dumpheap(windbg)輸出到n個對象
- 11. 創建N個動畫對象
- 12. 使用合併排序對n個字符串進行排序
- 13. 對象排序與對象
- 14. 如何將「有N個對象/對象」複數化?
- 15. 排序的對象
- 16. 一次呈現n個對象,插入代碼,然後重複接下來的n個對象 - Rails
- 17. 排序對象
- 18. 排列對象
- 19. 排序對象
- 20. 排序對象
- 21. 對象排名
- 22. 排序對象
- 23. JS - 排序對象中的子對象
- 24. 可排序的對象鏈表對象
- 25. JSON.stringify循環對象n^2
- 26. N層對象參數
- 27. 在O(n)時間和O(1)額外內存中排序1000萬個對象
- 28. C#對象的排名,多個條件
- 29. 比較共同值的n個對象的多個字段
- 30. Javascript排序的對象的子對象的對象數組
除非我完全誤解了問題,這有沒有關係組合或置換... – Guffa 2010-03-17 14:26:34