2013-04-15 30 views
1

我有點兒熟悉MySQL,但考慮實現這個功能會讓我的大腦受傷。我需要一個系統,您可以設置一個區域的「成本」,例如,在座標X = 20 Y = 20 WIDTH = 20 HEIGHT = 20之間,以每像素15個爲代價,現在如果您在此區域內放置另一個區域, X = 25,Y = 25,WIDTH = 10,HEIGHT = 10,每個像素5個成本,先前的區域被分解成4個部分,中間被擦掉以利於該區域。高級MySQL區域成本

我還希望能夠計算某個像素之間區域的「成本」。希望我已經以大多數人會理解的方式解釋了這一點。我不知道從哪裏開始。

+0

爲什麼前面的區域分成4部分? – dbf

+0

更多的是一個數學問題,而不是一個編程問題 – 2013-04-15 20:54:10

+0

+1。 。 。我碰巧認爲這是一個非常有趣的數據庫問題。 –

回答

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 
+0

這是真的生活。這些都是兩個單獨的表是否正確?如何將它們的結構看? – user2279927

+0

有人嗎?這看起來像中國人給我 – user2279927

-1

考慮實施此功能使我的大腦受到傷害

是的,我的!嗯...這聽起來很熟悉,但 - 因爲它已經做過的milliondollarhomepage

也許你也可以使用或改編預製的解決方案,如Gpix否則谷歌「像素廣告腳本」。

+0

也就是說沒有像我在找什麼。 – user2279927