在笛卡尔平面上给出一堆随机小尺寸的矩形(一般为每边3-8个),每个矩形的左上角给定为x,y坐标,在-1和1之间随机分配,怎么能我把它们分散到最低限度,以便保持它们相对x,y定位的方式没有重叠?
我会喜欢javascript中的答案,但任何可读代码都可以
这里有一些快速简单的javascript来实现它:
for(some_number_of_rectangles)
squares.push({
x:random(-1,1),
y:random:(-1,1),
width:random(3,8),
height:random(3,8)
})
示例输出:
[
{x:0.5,y:0,width:2,height:2}, //intersects 3rd
{x:0,y:1,width:2,height:2}, // intersects 4th
{x:-1,y:0,width:2,height:2},
{x:0,y:-0.5,width:2,height:2}, //intersects 5th
{x:0,y:-1.5,width:2,height:2}
] // to simplify the problem, the sizes are all the same, but that won't be the case usually
及其解决方案:
[ // no intersections now
{x:1,y:0,width:2,height:2}, // movement: 0.5
{x:0,y:2,width:2,height:2}, // movement: 1
{x:-2,y:0,width:2,height:2}, // movement: 1
{x:0,y:-1,width:2,height:2} // movement: 0.5
{x:0,y:-3,width:2,height:2} // movement: 1.5
]
答案 0 :(得分:1)
伪代码:
SELECT COUNT(*), camper_id, MIN(reg_date) as reg_date
FROM tbl_registration
WHERE reg_date >= CURDATE()
GROUP BY camper_id;
推理: 然后是每对矩形,
factor = 0
for a in rectangles:
for b in rectangles:
factor = max(
factor,
min(
max(
a.width / (b.x - a.x),
b.width / (a.x - b.x)
),
max(
a.height / (b.y - a.y),
b.height / (a.y - b.y)
)
)
)
// now multiply all coordinates with factor
或
factor >= a.width / (b.x - a.x) and factor >= b.width / (a.x - b.x)
现在假设例如factor >= a.height / (b.y - a.y) and factor >= b.height / (a.y - b.y)
和a.x <= b.x
。然后是第一行
a.y <= b.y
或第二行
factor*b.x >= factor*a.x + a.width
因此a和b不能在x和y中重叠,因此它们在2d中不重叠。 其他案件的处理类似。
根据factor*b.y >= factor*a.y + a.height
的定义,这些不等式中的至少一个将保持相等,因此得到的因子是最小的可能解。