[英]Flood fill algorithm analysis
我有一些使用洪水填充算法的方法。 很簡單
轉到頂部的第一個障礙。
將像素顏色更改為底部
在更改時檢查左/右像素是否具有不同的顏色
如果是,則也為此列上色(stack.push())
環。
Stack<Point> st = new Stack<Point>(); bool spLeft, spRight; Bitmap b = canvas.buffer; st.Push(start); spLeft = spRight = false; Point p = new Point(); while (st.Count > 0) { //going as far top as possible (finding first obstacle) p = st.Pop(); while (pY >= 0 && b.GetPixel(pX, pY) == oldColor) pY--; p.Y++; spLeft = spRight = false; //looping on every oldColored pixel in column while (pY < b.Height && b.GetPixel(pX, pY) == oldColor) { b.SetPixel(pX, pY, state.currentColor); //setting new color //checking if left pixel is oldColored and if it doesn't belong to span if (!spLeft && pX > 0 && b.GetPixel(pX - 1, pY) == oldColor) { st.Push(new Point(pX - 1, pY)); spLeft = true; } //checking if left pixel isn't oldColored and if it belongs to span else if (spLeft && pX > 0 && b.GetPixel(pX - 1, pY) != oldColor) { spLeft = false; } if (!spRight && pX < b.Width - 1 && b.GetPixel(pX + 1, pY) == oldColor) { st.Push(new Point(pX + 1, pY)); spRight = true; } else if (spRight && pX < b.Width - 1 && b.GetPixel(pX + 1, pY) != oldColor) { spRight = false; } p.Y++; } }
關鍵是我只是不了解這些部分
//checking if left pixel isn't oldColored and if it belongs to span
else if (spLeft && p.X > 0 && b.GetPixel(p.X - 1, p.Y) != oldColor) {
spLeft = false;
和
else if (spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) != oldColor) {
spRight = false;
}
沒有這些,代碼就可以正常工作,並且似乎具有相同的迭代次數。 您能幫我弄清楚這些行是否真的沒用,或者我只是不理解它們? (我不能相信我的朋友沒有目的就把它們放了)
它們允許填充多個區域。 開頭的if語句檢查它們是否為假,並向堆棧中添加一個像素。 當該區域結束時,那些重置。
如果不重置,則不會遇到spLeft區域2,因為在遇到第一個區域時,spLeft區域2會被設置為true(這避免了不必要地增加堆棧很多)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.