-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotBoundaryFinder.cpp
68 lines (60 loc) · 1.69 KB
/
dotBoundaryFinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "dotBoundaryFinder.h"
#include <QDebug>
#include <bits/stdc++.h>
using namespace std;
DotBoundaryFinder::DotBoundaryFinder(QImage &image)
{
this->image =ℑ
row = image.height();
col = image.width();
stackList = new QList<QPoint>();
}
QList<int> DotBoundaryFinder::findBoundary(int x, int y)
{
if(!isValidAndBlack(x,y)){
qDebug()<<"Err from ULBRofDotV4 (x,y): "<<x<<","<<y<<endl;
return QList<int>()<<0<<0<<0<<0;
}
area = 1;
u = b = y;
l = r = x;
image->setPixel(x,y,qRgb(1,1,1));
stackList->append(QPoint(x,y));
QPoint point;
while(!stackList->empty()){
area++;
point = stackList->takeFirst();
u = std::min(point.y(),u);
l = std::min(point.x(),l);
b = std::max(point.y(),b);
r = std::max(point.x(),r);
fillUpAdjList(point.x(),point.y());
}
//cout<<area<<endl;
return QList<int>()<<u<<l<<b<<r;
}
bool DotBoundaryFinder::isValidAndBlack(int x, int y)
{
if(x>=0 && x<col)
if(y>=0 && y<row)
if(image->pixel(x,y) == qRgb(0,0,0))
return true;
return false;
}
void DotBoundaryFinder::fillUpAdjList(int x, int y)
{
QList<QPoint> tempList;
tempList.append(QPoint(x-1,y));
tempList.append(QPoint(x+1,y));
tempList.append(QPoint(x,y-1));
tempList.append(QPoint(x,y+1));
for(int i=0;i<=3;i++)
if(isValidAndBlack(tempList[i].x(),tempList[i].y())){
image->setPixel(tempList[i].x(),tempList[i].y(),qRgb(1,1,1));
stackList->append(tempList[i]);
}
}
double DotBoundaryFinder::getArea()
{
return area;
}