扫描线填充算法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 --;
}
}
}
}
}