由于作业需要用到这样的稀奇玩意,就得研究研究。
最基本的种子算法,比如当前开始填充的点位A,那么就沿着A的上下左右四个方向开始遍历进行判断,如果像素符合要求那么就进行填充否者就不用管了。最简单的办法就是递归了,但对于一张好多万像素的图片来说,这一递归估计就拜拜了。因此必须进行改进再改进,具体的有什么扫描线算法之类的优化。
当然解决办法还是只有一个,建立一个堆栈就搞定了,所以很容易的。还是种子算法,没那么难的东西。研究效率什么的真没心情,快点搞定上交才是王道。重图形上看,这一点都不完美。会慢慢改善的。
QT算法,贴代码我还是很乐意的,不过头文件什么的就算了。
#include "fillshape.h"
FillShape::FillShape(QImage *img)
{
w = img->width();
h = img->height();
this->img = img;
color = qRgba(50,50,50,50);
}
bool FillShape::isBrim(int x,int y){
if(x<=0 || y<=0 || x>=w || y>=h){
return true;
}
QRgb rgb =img->pixel(QPoint(x,y));
return rgb>0;
}
void FillShape::fillStack(XyPoint p){
XyPoint tmp;
img->setPixel(p.x,p.y,color);
if(!isBrim(p.x,p.y+1)){
tmp.x = p.x;
tmp.y = p.y+1;
stack.push(tmp);
}
if(!isBrim(p.x,p.y-1)){
tmp.x = p.x;
tmp.y = p.y-1;
stack.push(tmp);
}
if(!isBrim(p.x-1,p.y)){
tmp.x = p.x-1;
tmp.y = p.y;
stack.push(tmp);
}
if(!isBrim(p.x+1,p.y)){
tmp.x = p.x+1;
tmp.y = p.y;
stack.push(tmp);
}
}
void FillShape::fill(int x,int y){
if(isBrim(x,y)){
return;
}
stack.clear();
XyPoint p;
p.x = x;
p.y = y;
fillStack(p);
while(!stack.isEmpty()){
fillStack(stack.pop());
}
}
FillShape::FillShape(QImage *img)
{
w = img->width();
h = img->height();
this->img = img;
color = qRgba(50,50,50,50);
}
bool FillShape::isBrim(int x,int y){
if(x<=0 || y<=0 || x>=w || y>=h){
return true;
}
QRgb rgb =img->pixel(QPoint(x,y));
return rgb>0;
}
void FillShape::fillStack(XyPoint p){
XyPoint tmp;
img->setPixel(p.x,p.y,color);
if(!isBrim(p.x,p.y+1)){
tmp.x = p.x;
tmp.y = p.y+1;
stack.push(tmp);
}
if(!isBrim(p.x,p.y-1)){
tmp.x = p.x;
tmp.y = p.y-1;
stack.push(tmp);
}
if(!isBrim(p.x-1,p.y)){
tmp.x = p.x-1;
tmp.y = p.y;
stack.push(tmp);
}
if(!isBrim(p.x+1,p.y)){
tmp.x = p.x+1;
tmp.y = p.y;
stack.push(tmp);
}
}
void FillShape::fill(int x,int y){
if(isBrim(x,y)){
return;
}
stack.clear();
XyPoint p;
p.x = x;
p.y = y;
fillStack(p);
while(!stack.isEmpty()){
fillStack(stack.pop());
}
}
从来不写头文件……直接#include “xxx.c”……
还有#include “xxx.cpp”……
虽然不知道你是谁,但欢迎

你的作业总跟我的差不多!现在在我眼里看来,你是大神级的,我连渣渣都不如啊!
对于你,我只能说,努力吧
膜拜啊,QT大神
错了,错了,上个星期才开始的新手
老夫可能了此余生也无法理解。。。
不了解意味着,吃得好、睡的香,多享受的日子
感觉QT效率挺高的
也还好,毕竟是C++的东西
强人
没什么强不强的,搞定就行
博主学的不错啊!
将就会用,和别人差距还是太大
你的作业好醉人。
估计你们是看到代码迷糊了吧,这个算法很简单的
代码什么的,不懂不懂。
你们的作业都好高大上,我还真的看不懂。
算不上高大上,就是多而已。