Repository navigation
Expand file tree
/
Copy pathcore.cpp
More file actions
186 lines (150 loc) · 4.96 KB
/
Copy pathcore.cpp
File metadata and controls
186 lines (150 loc) · 4.96 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
using namespace std;
// Code for Graph visualization with Flood Fill ALgorithm( $4 directions -
// top,right,bottom,left )
class dm {
public:
int no_of_lines;
int no_of_dta_inALine;
dm() {
no_of_lines = 0;
no_of_dta_inALine = 0;
getDms();
}
void getDms() {
std::vector<long double> dim;
std::ifstream file("dms.txt");
// read the input
copy(std::istream_iterator<long double>(file),
std::istream_iterator<long double>(), std::back_inserter(dim));
no_of_lines = dim[0];
no_of_dta_inALine = dim[1];
// cout << no_of_lines << " " << no_of_dta_inALine << endl;
}
};
vector<vector<long double>> getTwoD_RepresentationMatrix() {
dm *D = new dm();
// cout << D->no_of_lines << D->no_of_dta_inALine << endl;
int i = D->no_of_lines;
int j = D->no_of_dta_inALine;
vector<long double> values;
ifstream file("image-output.txt");
// read the input
copy(istream_iterator<long double>(file), istream_iterator<long double>(),
back_inserter(values));
// cout << "Size of the vector is : " << values.size() << endl;
vector<vector<long double>> image(i, vector<long double>(j, 0));
cout << "i : " << i << " j : " << j << endl;
cout << image[0].size() << endl;
cout << image.size() << endl;
int count = 0;
for (int m = 0; m < i; m++) {
for (int n = 0; n < j; n++) {
image[m][n] = values[count];
count++;
}
}
return image;
}
void writeOutput(vector<vector<long double > > updated_Image){
// PROCESSIING THE OUTPUT :
dm *D = new dm();
// cout << D->no_of_lines << D->no_of_dta_inALine << endl;
int M = D->no_of_lines;
int N = D->no_of_dta_inALine;
ofstream output_file("output.txt");
ostream_iterator<int> output_iterator(output_file, " ");
int count = 0;
for (int i = 0; i < updated_Image.size(); i++) {
copy(updated_Image.at(i).begin(), updated_Image.at(i).end(),
output_iterator);
output_file << '\n';
}
}
string getPx(long double dt, long double flag) {
if (flag == 1) {
return to_string(dt);
}
if (dt == 0)
return ".";
else if (dt == 2)
return "O";
else
return " ";
}
void printImage(vector<vector<long double>> img, long double flag) {
cout << endl;
for (auto list : img) {
for (auto px : list) {
cout << getPx(px, flag) << " ";
}
cout << endl;
}
cout << endl;
}
void color(long double row, long double col,
vector<vector<long double>> &updated_image,
vector<vector<long double>> image, long double newColor,
long double delRow[], long double delCol[], long double iniC) {
updated_image[row][col] = newColor;
long double n = image.size(); // height
long double m = image[0].size(); // width
for (int i = 0; i < 4; i++) {
long double new_row = row + delRow[i];
long double new_col = col + delCol[i];
if (new_row >= 0 && new_row < n && new_col >= 0 && new_col < m &&
image[new_row][new_col] ==
iniC && // if new polong double color is same as
// initial color then Re-Palong double it
updated_image[new_row][new_col] !=
newColor // if new polong double is not updated by any node
) {
color(new_row, new_col, updated_image, image, newColor, delRow, delCol,
iniC);
}
}
}
vector<vector<long double>> floodFill(vector<vector<long double>> image,
long double start_row,
long double start_col,
long double newColor) {
long double initial_color = image[start_row][start_col];
vector<vector<long double>> updated_image = image;
long double delRow[] = {-1, 0, +1, 0};
long double delCol[] = {0, +1, 0, -1};
color(start_row, start_col, updated_image, image, newColor, delRow, delCol,
initial_color);
return updated_image;
}
vector<vector<long double>> processWithOne(vector<vector<long double>> image,
long double newColor) {
// printImage(image, 0);
vector<vector<long double>> updated_Image = floodFill(image, 0, 0, newColor);
for (long double i = 0; i < image.size(); i++) {
for (long double j = 0; j < image[0].size(); j++) {
if (image[i][j] == 1) {
updated_Image = floodFill(updated_Image, i, j, newColor);
}
}
}
// updated_Image = floodFill(updated_Image, 0, 9, 2);
return updated_Image;
}
int main() {
// x : 9 , y : 10
vector<vector<long double>> image = getTwoD_RepresentationMatrix();
long double newColor = 100;
// call flood fill where 1 is the value
// vector<vector<long double>> updated_Image = processWithOne(image,
// newColor);
// // printImage(updated_Image, 0);
// printImage(updated_Image, 1);
vector<vector<long double>> updated_Image = floodFill(image, 202, 0, newColor);
writeOutput(updated_Image);
return 0;
}