我有一些洪水填充算法的方法。這是非常簡單的洪水填充算法分析
轉到第一個障礙在上面。同時改變檢查如果左
變化像素顏色的底部
/右像素是在不同顏色
若是:顏色此列太(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 (p.Y >= 0 && b.GetPixel(p.X, p.Y) == oldColor) p.Y--; p.Y++; spLeft = spRight = false; //looping on every oldColored pixel in column while (p.Y < b.Height && b.GetPixel(p.X, p.Y) == oldColor) { b.SetPixel(p.X, p.Y, state.currentColor); //setting new color //checking if left pixel is oldColored and if it doesn't belong to span if (!spLeft && p.X > 0 && b.GetPixel(p.X - 1, p.Y) == oldColor) { st.Push(new Point(p.X - 1, p.Y)); spLeft = true; } //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; } if (!spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) == oldColor) { st.Push(new Point(p.X + 1, p.Y)); spRight = true; } else if (spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) != 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;
}
如果沒有這些,代碼工作正常,並且現在看來似乎有等量的遊覽。你能幫我弄清楚這些線條是否真的無用或者我不理解它們? (我無法相信我的朋友把他們沒有目的地)
爲什麼不問你的朋友他爲什麼加了他們? – Kevin 2012-08-16 15:49:23
沒有聯繫atm。假日:/ – user1321706 2012-08-16 15:54:45