我會通過存儲具有優先級的所有區域,然後逐個像素地獲取成本來解決這個問題。
因此,對於單個像素的成本將是:
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
爲什麼前面的區域分成4部分? – dbf
更多的是一個數學問題,而不是一個編程問題 – 2013-04-15 20:54:10
+1。 。 。我碰巧認爲這是一個非常有趣的數據庫問題。 –