Scanline Flood fill Algorithm

16 Sep 2016,
Share: 
Vacay banner

Flood fill Algorithm

Flood fill Algorithm คือ Algorithm การระบายสีโดยมีเงื่อนไขอยู่ว่ารูปทรงที่ต้องการระบายนั้นจะต้องเป็นรูปปิด ถ้าใครนึกภาพไม่ออกก็ลองนึกถึงโปรแกรม paint จะมีเครื่องมือเทสี(Fill) มีลักษณะเช่นเดียวกัน

Image

scanline-floodfill-algorithm Flood fill Algorithm จะแบ่งออกเป็นหลายประเภทแต่ในบทความนี้ จะอธิบายถึง Scanline Flood fill Algorithm

Scanline Flood fill Algorithm

หลักการทํางานของ Scanline Flood fill Algorithm คือ แสกนไปทีละ line ตามรูปตัวอย่าง

Image

มาดูตัวอย่าง source code กันดีกว่าครับ

void floodFillScanlineStack(int x, int y, int newColor, int oldColor)
{
    if(oldColor == newColor) return;
    emptyStack();

    int y1;
    bool spanLeft, spanRight;

    if(!push(x, y)) return;

    while(pop(x, y))
    {
        y1 = y;
        while(y1 >= 0 && screenBuffer[x][y1] == oldColor) y1--;
        y1++;
        spanLeft = spanRight = 0;
        while(y1 < h && screenBuffer[x][y1] == oldColor )
        {
            screenBuffer[x][y1] = newColor;
            if(!spanLeft && x > 0 && screenBuffer[x - 1][y1] == oldColor)
            {
                if(!push(x - 1, y1)) return;
                spanLeft = 1;
            }
            else if(spanLeft && x > 0 && screenBuffer[x - 1][y1] != oldColor)
            {
                spanLeft = 0;
            }
            if(!spanRight && x < w - 1 && screenBuffer[x + 1][y1] == oldColor)
            {
                if(!push(x + 1, y1)) return;
                spanRight = 1;
            }
            else if(spanRight && x < w - 1 && screenBuffer[x + 1][y1] != oldColor)
            {
                spanRight = 0;
            }
            y1++;
        }
    }
}

ตัวอย่างนี้จะเป็นการ scan ทางแนวตั้ง โดยจะใช้ Stack เข้ามาช่วย ลองทําความเข้าใจ source code ดูครับ

ข้อมูลจาก lodev.org

Suggestion blogs

[ภาษาซี] การดําเนินการทางคณิตศาสตร์

การดําเนินการทางคณิตศาสตร์ หมายถึงการนําโอเปอร์แรนด์ 2 ค่า มากระทํากันโดยใช้โอเปอเรเตอร์ หรือตัวดําเดินการทางคณิตศาสตร์ เช่น บวก, ลบ, คูณ, หาร

คํานวณความต้านทาน วงจรอนุกรม

การคํานวณค่าความต้านทานรวมเมื่อนํา R(ตัวต้านทาน) มาต่อกันแบบ อนุกรม ดังรูป

Go EP.8 Go Channel Select Multiple Communication Operations

สวัสดีครับ ในบทความนี้ก็เป็น EP.8 แล้วนะครับ โดยเนื้อหาจะเป็นเรื่องเกี่ยวกับ Channel Select Multiple Communication Operationsสําหรับท่านใดที่ยังไม่ได้อ่าน EP.7 ท่านสามารถกลับไปอ่านก่อนได้นะครับที่นี่ Go EP.7 Go Unit Testingในบทความก่อนหน้านี้เราได้เรียนรู้เรื่องการใช้งาน Go Channel กันไปแล้ว จะเห็นว่าถ้าเราต้องการส่งข้อมูลมากกว่า 1 Channel อาจจะทําเกิด blocking การทำงาน เมื่อ Channel ใด Channel หนึ่งไม่มีการรับส่งข้อมูล หรือไม่สามารถส่งข้อมูลได้อีก สิ่งที่จะมาช่วยให้โปรแกรมของเราทํางานต่อไปได้ก็คือ Select statement เรามาดูวิธีการใช้งานตามหัวข้อด้านล่างเลยครับ


Copyright © 2019 - 2026 thiti.dev | v1.61.0 | Privacy policy | 

Build with ❤️ and Astro.

Github profile Linkedin profile Instagram X profile Nostr Youtube channel Telegram Email contact วงแหวนเว็บ