Scanline Flood fill Algorithm

16 Sep 2016,
Share: 

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

Access raspberry pi ผ่าน internet โดยไม่ต้อง forward port

สวัสดีครับ หลายๆคนอาจเจอปัญหานี้ อยากจะ ssh ไปยัง raspberry pi ผ่าน internet จากที่ใดๆก็ได้บนโลก แต่ติดปัญหาคือ คุณอาจจะอยู่ หอพัก อพาร์ทเม้นท์ ฯลฯ ที่คุณไม่สามารถเข้าไปยุ่งกับ rounter ได้ ไม่สามารถ forward port ได้ ทําให้ไม่สามารถ Access raspberry pi ได้ ในบทความนี้มีคําตอบครับ พระเอกของงานนี้คือ weaved ซึ่งจะช่วยจักการการเชื่อมต่อให้เราเองเพียงแค่ติดตั้ง service บน raspberry pi ของเราเท่านั้น

WebP Image คืออะไร

สวัสดีครับ ในบทความนี้เราจะมาทําความรู้จักกับ WebP Image นะครับ ซึ่งจะมาช่วยให้เราโหลดรูปบนหน้าเว็บได้เร็วขึ้นกว่าเดิม และเป็นผลดีกับ SEO ด้วย

ไฟฟ้าดูด (Electric shock)

โดยปกติกระแสไฟฟ้าจะไหลผ่านตัวนําไฟฟ้าเช่น สายไฟฟ้า สายทองแดง โดยจะมีอิเล็กตรอนไหลจากขั้วลบไปยังขั้วบวกจนครบวงจร ถ้าหากส่วนใดส่วนหนึ่งของรางกายของเราไปสัมผัสกับตัวนําไฟฟ้าที่มีกระแสไฟฟ้าอยู่ แล้วทําให้กระแสไฟฟ้าไหลผ่านร่างกายของเรา จะทำให้เกิดอันตรายต่อร่างกายหรือบาดเจ็บถึงชีวิตได้


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

Build with ❤️ and Astro.

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