高级MySQL区域成本

时间:2013-04-15 20:48:16

标签: php mysql

我对MySQL很熟悉但是考虑实现这个功能会让我的大脑受到伤害。我需要一个系统,你可以在坐标X = 20 Y = 20 WIDTH = 20 HEIGHT = 20之间设置一个区域的“成本”,每个像素15个成本,现在如果你在这个区域内放置另一个区域,比如说X = 25,Y = 25,WIDTH = 10,HEIGHT = 10,每个像素5个成本,前一个区域被分解为4个部分,中间被擦除,有利于这个区域。

我还希望能够计算某些像素之间区域的“成本”。希望我已经解释过,大部分人都会理解。我不知道从哪里开始。

2 个答案:

答案 0 :(得分:2)

我会通过存储所有具有优先级的区域然后逐个像素地获得成本来实现此目的。

因此,单个像素的成本为:

select c.*
from costs c
where PIXELX between c.x and c.x + c.deltax and PIXELY between c.y + c.deltay
order by priority desc
limit 1

要将其扩展到像素区域,您可以将区域扩展为一组像素。我建议使用numbers表来帮助解决这个问题:

select x.num as x, y.num as y
from numbers x cross join
     numbers y
where x.num between PIXELX and PIXELX and DELTAX and
      y.num between PIXELY and PIXELY and DELTAY

现在,结合这些想法来获得给定像素的所有可能成本:

select x.num as x, y.num as y, max(priority) as maxpriority
from numbers x cross join
     numbers y join
     costs c
     on x.num between c.x and c.x + c.deltax and y.num between c.y + c.deltay
where x.value between PIXELX and PIXELX and DELTAX and
      y.value between PIXELY and PIXELY and DELTAY
group by x.num, y.num

最后,加入给定优先级的费用:

select sum(c.cost)
from (select x.num as x, y.num as y, max(priority) as maxpriority
      from numbers x cross join
           numbers y join
           costs c
           on x.num between c.x and c.x + c.deltax and y.num between c.y + c.deltay
      where x.value between PIXELX and PIXELX and DELTAX and
            y.value between PIXELY and PIXELY and DELTAY
      group by x.num, y.num
    ) xyp join
    costs c
    on  xyp.x between c.x and c.x + c.deltax and xyp.y between c.y + c.deltay and
        xyp.maxpriority = c.priority

答案 1 :(得分:-1)

  

考虑实施此功能会让我的大脑受伤

是的,我也是!嗯......这听起来非常熟悉 - 因为它已经在milliondollarhomepage

之前完成了

也许您可以使用或改编预先制定的解决方案,例如Gpix或google“像素广告脚本”。