扫描线填充算法C++实现

扫描线填充算法C++实现

#include <iostream>
#include <stack>
#define newValue 1
#define boundaryValue 0

using namespace std;

class Point {
public:
    Point(int x, int y) {

    }

    int x{}, y{};
};

void setPixel(int x, int y) {
    cout << x << ends << y<< endl;
}

int getPixel(int x, int y) {   
    cout << x << ends << y<< endl;
    return 0;
}

void scanLindSeedFill (int x, int y) {
    int x0, xl, xr, y0, xid = 0;
    stack<Point> s;
    int flag = 0;
    Point p(0, 0);
    s.push(Point(x, y));
    while (!s.empty()) {
        p = s.top();
        s.pop();
        x = p.x;
        y = p.y;
        setPixel(x, y);
        //填充右侧像素
        x0 = x + 1;
        while (getPixel(x0, y) != boundaryValue && getPixel(x0, y) != newValue) {
            setPixel(x0, y);
            x0++;
        }
        //填充左侧像素
        xr = x0 - 1;
        x0 = x - 1;
        while (getPixel(x0, y) != boundaryValue && getPixel(x0, y) != newValue) {
            setPixel(x0, y);
            x0--;
        }
        xl = x0 + 1;
        y0 = y;
        for (int i = 1; i >= -1; i -= 2) {
            x0 = xr;
            y = y0 + i;
            while (x0 >= xl) {
                while (getPixel(x0, y) != boundaryValue && getPixel(x0, y) != newValue && x0 >= xl) {
                    if (!flag) {
                        flag = 1;
                        xid = x0;
                    }
                    x0--;
                }
                if (flag) {
                    s.push(Point(xid, y));
                    flag = 0;
                }
                while (getPixel(x0, y) == boundaryValue || getPixel(x0, y) == newValue) {
                    x0 --;
                }
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值