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

copy โฟลเดอร์ที่ภายในมีข้อมูลอยู่ ubuntu

โดยปกติแล้วถ้าเราต้องการจะ Copy อะไรซักอย่างเราจะใช้คําสั่ง cp แต่จะไม่สามารถใช้กับโฟลเดอร์ที่มีข้อมูลอยู่ภายในได้ เราจะต้องเพื่ม option -avr เข้าไปดังนี้

BMS (Battery Management System) คืออะไร

BMS ย่อมาจาก Battery Management System หรือถ้าแปลเป็นไทยก็คือ ระบบจัดการแบตเตอรี่ ซึ่งจะทําหน้าที่ในการ Maintain balance cell battery เพื่ออัดไฟให้เต็มมากที่สุด และยืดอายุการใช้งานแบตเตอรี่ หากยังนึกภาพไม่ออก ผมจะยกตัวอย่างดังนี้ครับ เมื่อเรานําแบตเตอรี่หลายๆก้อนมาต่อกันแบบอนุกรมเพื่อให้ได้แรงดันไฟฟ้าตามที่เราต้องการแบบในรูป

Auto login on raspberry pi

Auto login on raspberry pi ถ้าเราใช้ debian บน raspberry pi ในโหมด gui ระบบจะ login user: pi ให้เราโดยอัตโนมัติ แต่ถ้าเราต้องการให้ login ด้วย user อื่นๆ ก็สามารถทำได้โดย แก้ไข ไฟล์ "/etc/lightdm/lightdm.conf" ด้วยคำสั่งนี้


Copyright © 2019 - 2025 thiti.dev |  v1.57.0 |  Privacy policy | 

Build with ❤️ and Astro.

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