I have a 2d char array, of which I am trying to retrieve the value of an opposite side while on the edge. Instead of retrieving that value, it keeps returning with null. Can't make heads or tails of what the problem is. Here is the code I traced the problem to.
int xO = x; int yO = y;
//reassigns to opposite side of array
if ((x == 0) && (xM == - 1)) { xM = mapSize - 1; }
if ((x == mapSize - 1) && (xM == 1)) { xM = 0 - (mapSize - 1); }
if ((y == 0) && (yM == - 1)) { yM = mapSize - 1; }
if ((y == mapSize - 1) && (yM == 1)) { yM = 0 - (mapSize - 1);
}
//Checks 9 chars around and assigns any found characters
if (mapPlates[x + xM][y + yM] == 0) {
std::cout << "//" + std::to_string(x + xM) + ", " + std::to_string(yM + y) + "; " + std::to_string(mapPlates[x + xM][y + yM]) + "//";
}
if (mapPlates[x + xM][y + yM] =! "%") {
remaining--;
mapPlates[xO][yO] = mapPlates[x + xM][y + yM];
std::cout << "{assigned " + std::to_string(mapPlates[xO][yO]) + " }";
}
return remaining;
}
Snippet of output(only returns values == null)(should be returning '%'):
```//14, 49; 0////14, 0; 0////14, 1; 0////19, 49; 0////20, 49; 0////19, 0;
Related
Given an arbitrary matrix, how do I find the co-ordinates that surrounds each city accurately?
E.g. City 1 has surrounding matrix of (0, 0), (0, 3), (1, 0), (1, 3), (2,0), (2, 1), (2, 2), (2, 3).
I have tried using a hardcoded method. Which is loop through each city's co-ordinate, however there are still inaccuracy in this method.
E.g. (0, 1) and from there check all 8 directions, up, down, left, right, upper left, upper right, bottom left, bottom right.
And if the char value is ' ', it is not a city which means it is part of a surrounding.
Is there any way which is much more efficient and more accurate in finding the surrounding?
void surroundings(int x, int y) {
// Initiate the first city struct information
citySummInfo.cityId = cityLocList[0].cityId;
citySummInfo.cityName = cityLocList[0].cityName;
citySummInfoList.push_back(citySummInfo);
// Add unique cityID & cityName into vector
for (size_t i = 0; i < cityLocList.size(); i++) {
for (size_t j = 0; j < citySummInfoList.size(); j++) {
if (cityLocList[i].cityId == citySummInfoList[j].cityId && cityLocList[i].cityName == citySummInfoList[j].cityName) {
break;
}
else if (j == citySummInfoList.size() - 1) {
citySummInfo.cityId = cityLocList[i].cityId;
citySummInfo.cityName = cityLocList[i].cityName;
citySummInfoList.push_back(citySummInfo);
}
}
}
// To populate the entire matrix with city ID
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (size_t k = 0; k < cityLocList.size(); k++) {
if (cityLocList[k].xGrid == i && cityLocList[k].yGrid == j)
mapPtr[j][i] = cityLocList[k].cityId + '0';
}
}
}
// Main process of getting the surrounding
for (size_t i = 0; i < citySummInfoList.size(); i++) {
for (size_t j = 0; j < cityLocList.size(); j++) {
if (citySummInfoList[i].cityId == cityLocList[j].cityId)
citySummInfoList[i].coOrdinates.push_back(to_string(cityLocList[j].xGrid) + "." + to_string(cityLocList[j].yGrid));
}
}
for (size_t i = 0; i < citySummInfoList.size(); i++) {
vector<string> temp;
for (size_t j = 0; j < citySummInfoList[i].coOrdinates.size(); j++) {
char cityId = citySummInfoList[i].cityId + '0';
char delim[] = { '.' };
vector<string> tempAxis = tokenizer(citySummInfoList[i].coOrdinates[j], delim, 1);
int xAxis = stoi(tempAxis[0]);
int yAxis = stoi(tempAxis[1]);
if (xAxis - 1 < 0 || yAxis - 1 < 0) {
continue;
}
if (mapPtr[xAxis - 1][yAxis + 1] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis - 1) + "." + to_string(yAxis + 1);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis - 1][yAxis] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis - 1) + "." + to_string(yAxis);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis - 1][yAxis - 1] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis - 1) + "." + to_string(yAxis - 1);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis][yAxis + 1] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis) + "." + to_string(yAxis + 1);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis][yAxis - 1] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis) + "." + to_string(yAxis - 1);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis + 1][yAxis + 1] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis + 1) + "." + to_string(yAxis + 1);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis + 1][yAxis] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis + 1) + "." + to_string(yAxis);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
if (mapPtr[xAxis + 1][yAxis - 1] != cityId) {
if (xAxis + 1 == x || yAxis + 1 == y || xAxis - 1 < 0 || yAxis - 1 < 0)
continue;
string coOrd = to_string(xAxis + 1) + "." + to_string(yAxis - 1);
if (find(temp.begin(), temp.end(), coOrd) == temp.end()) {
temp.push_back(coOrd);
}
}
}
citySummInfoList[i].coOrdinates.reserve(temp.size());
citySummInfoList[i].coOrdinates.insert(citySummInfoList[i].coOrdinates.end(), temp.begin(), temp.end());
}
}
Also, is there a possibility that my print function may cause such unreliability?
void print(int x, int y) {
for (int i = 0; i <= x + 2; i++) {
if (i == 0 || i >= x + 1) // Indentation for 1st and last row of non city locations
cout << setw(4) << " ";
for (int j = 0; j <= y + 2; j++) {
if ((i == 0 || i == x + 1) && j <= y + 1) { // Layout for first and last row
cout << "# ";
}
else if ((j == 0 && (i != 0 || i <= x))) { // Numbering for each row
if (x - i >= 0) {
cout << setw(3) << left << x - i << " ";
}
else {
cout << " "; // Indentation for last row of #s
}
}
else if (j == 1 && i < x + 2) { // Layout for first column
cout << "#";
}
else if (j == y + 2 && i != 0 && i < x + 1) { // Layout for last column
cout << " #";
}
else if (i == x + 2 && j < y + 1) { // Numbering for each column
cout << j - 1 << " ";
}
else if ((i != 0 && i != x + 2 && j != y + 2)) {
cout << " " << mapPtr[x - i][j - 2]; // Plot map value
}
}
cout << endl;
}
cout << endl;
}
This is an O(n) answer for your problem. The idea behind it is to find all points that are edges (a point is an edge if it is adjacent to anything which is not its own city).
After finding all edge points, loop through each of them and add all points adjacent to them which are whitespace characters.
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> point;
string m[] = {
" ",
" 555 ",
" 555 ",
" 222 555 ",
" 222 ",
" 222 ",
" 222 ",
" ",
"11 33 ",
"11 44",
" 44"
};
//hash function for pairs
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
bool insideBounds(int x, int y, point &size){
if(x < 0 || x >= size.x || y < 0 || y >= size.y) return false;
return true;
}
bool isEdge(int x, int y, point &size){
for(int i = -1; i < 2; ++i){
for(int j = -1; j < 2; ++j){
if(!insideBounds(x + j, y + i, size)) return true;
if(m[y + i][x + j] == ' ') return true;
}
}
return false;
}
void FindSurrounding(int x, int y){
//size of map
point size = make_pair(11, 11);
//current city id
char city = m[y][x];
//finding a point in edge of the city
point edge = make_pair(x, y);
for(int i = x - 1; i >= 0; --i)
if(m[y][i] == city) edge = make_pair(i, y);
//find all edge points
unordered_set<point, hash_pair> visited;
stack<point> toVisit;
toVisit.push(edge);
while(toVisit.size()){
point current = toVisit.top();
visited.insert(current);
toVisit.pop();
for(int i = -1; i < 2; ++i){
for(int j = -1; j < 2; ++j){
point p = make_pair(current.x + j, current.y + i);
if(!insideBounds(p.x, p.y, size) || m[p.y][p.x] != city) continue;
if(visited.find(p) == visited.end() && isEdge(p.x, p.y, size)){
toVisit.push(p);
}
}
}
}
//find surrounding slots
unordered_set<point, hash_pair> surrounding;
for (const auto& p: visited) {
for(int i = -1; i < 2; ++i){
for(int j = -1; j < 2; ++j){
point curr = make_pair(p.x + j, p.y + i);
if(insideBounds(curr.x, curr.y, size) && m[curr.y][curr.x] == ' '){
surrounding.insert(curr);
}
}
}
}
//print answer
for (const auto& p: surrounding) {
cout<<"("<<p.x<<", "<<p.y<<"), ";
}
}
int main()
{
FindSurrounding(0, 8);
return 0;
}
OUTPUT: (2, 10), (1, 10), (2, 9), (0, 10), (2, 8), (2, 7), (1, 7), (0, 7),
I am coding a solution program for leetcode. I have encountered memory read errors and segmentation fault errors which I am unable to resolve. I have tried tinkering with the code to try and remove the errors.
For example: in the numIslands function's last lines, I had tried declaring coordinates as an array rather than a pointer but the results did not change.
*(last + 1) = *(coordinates + 1); does not seem to work at all in the numIslands function. I am totally blank about what to do next.
#include <iostream>
#include <string>
#include <vector>
#include <map>
namespace p200 {
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::map;
}
class Solution {
private:
int count_islands = 0;
public:
int *last;
int numIslands(vector<vector<char>>& grid) {
if (grid.size() == 0) return count_islands;
if (grid.size() == 1 && grid[0].size() == 1) return grid[0][0] - 48;
vector<vector<int>> grid_copy;
vector<int> in;
for (size_t index = 0; index < grid.size(); ++index) {
for (size_t index0 = 0; index0 < grid[index].size(); ++index0) {
(grid[index][index0] == '0')
? in.push_back(0)
: in.push_back(INT_MAX);
}
grid_copy.push_back(in);
in.clear();
}
int * coordinates = new int[2];
*coordinates = 0;
*(coordinates + 1) = 0;
last = new int[2];
*last = *coordinates;
*(last + 1) = *(coordinates + 1);
find_island(grid_copy, coordinates);
return count_islands;
}
void find_island(vector<vector<int>> & arg_grid, int* arg_coordinates) {
if (arg_grid[*arg_coordinates][*(arg_coordinates + 1)] == INT_MAX) {
*last = *arg_coordinates;
*(last + 1) = *(arg_coordinates + 1);
map_island(arg_grid, arg_coordinates, 1);
} else {
if (*(arg_coordinates + 1) < arg_grid[*arg_coordinates].size()) {
*(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
find_island(arg_grid, arg_coordinates);
}
if (*(arg_coordinates + 1) == arg_grid[*arg_coordinates].size()) {
if (*arg_coordinates == arg_grid.size()) return;
*arg_coordinates = *arg_coordinates + 1;
*(arg_coordinates + 1) = 0;
find_island(arg_grid, arg_coordinates);
}
}
}
void map_island(vector<vector<int>> & arg_grid, int* arg_coordinates, int arg_new) {
arg_grid[*arg_coordinates][*(arg_coordinates + 1)] = count_islands + (arg_new * 1);
if (arg_new) count_islands = count_islands + 1;
if (*arg_coordinates != 0 && *arg_coordinates - 1 == INT_MAX) {
*arg_coordinates = *arg_coordinates - 1;
map_island(arg_grid, arg_coordinates, 0);
*arg_coordinates = *arg_coordinates + 1;
}
if ((*arg_coordinates < arg_grid.size() - 1)
&& *arg_coordinates + 1 == INT_MAX) {
*arg_coordinates = *arg_coordinates + 1;
map_island(arg_grid, arg_coordinates, 0);
*arg_coordinates = *arg_coordinates - 1;
}
if (*(arg_coordinates + 1) != 0 && *(arg_coordinates + 1) - 1 == INT_MAX) {
*(arg_coordinates + 1) = *(arg_coordinates + 1) - 1;
map_island(arg_grid, arg_coordinates, 0);
*(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
}
if (*(arg_coordinates + 1) < arg_grid[*arg_coordinates].size() - 1
&& *(arg_coordinates + 1) + 1 == INT_MAX) {
*(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
map_island(arg_grid, arg_coordinates, 0);
*(arg_coordinates + 1) = *(arg_coordinates + 1) - 1;
}
find_island(arg_grid, last);
}
};
int main() {
vector<vector<char>> input;
input.push_back(vector<char>({'1','1','1','1','0'}));
input.push_back(vector<char>({'1','1','0','1','0'}));
input.push_back(vector<char>({'1','1','0','0','0'}));
input.push_back(vector<char>({'0','0','0','0','0'}));
Solution solver;
cout << "Number of Island: " << solver.numIslands(input) << endl;
return 0;
}
I have ran it through gdb. After a bit of digging, the error is this:
void find_island(vector<vector<int>> & arg_grid, int* arg_coordinates) {
if (arg_grid[*arg_coordinates][*(arg_coordinates + 1)] == INT_MAX) {
*last = *arg_coordinates;
*(last + 1) = *(arg_coordinates + 1);
map_island(arg_grid, arg_coordinates, 1);
} else {
if (*(arg_coordinates + 1) < arg_grid[*arg_coordinates].size()) {
*(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
find_island(arg_grid, arg_coordinates);
}
if (*(arg_coordinates + 1) == arg_grid[*arg_coordinates].size()) {
if (*arg_coordinates == arg_grid.size()) return;
*arg_coordinates = *arg_coordinates + 1;
*(arg_coordinates + 1) = 0;
find_island(arg_grid, arg_coordinates);
}
}
}
In the second if, you do a bounds check and then increment. You should first increment then to the bounds check, like this:
*arg_coordinates = *arg_coordinates + 1;
if (*arg_coordinates == arg_grid.size()) return;
Also please usevector.at() instead of operator[]. It produces more readable and easier to interpret debug messages. Also as other people pointed it out *(arg_coordinates + 1) = arg_coordinates[1], which would make for a more readable code.
For other similar issues I recommend a debugger.
Edit: I ran the code and realised that there is some other bug within it. One of the islands is counted twice, so it gives the wrong result, but that is not technically the issue at hand.
I am new to c++ and need help making my maze look better. I want to use unicode characters but my program crashes. Here is a picture of my random maze. image of my maze
You can check my code here
#include <iostream>
#include <ctime>
#include <cstring>
using std::cout;
using std::fill;
using std::endl;
using std::cin;
using std::cout;
int main()
{
srand(time(NULL));
int arr_x[1000] = { 0 }; fill(arr_x + 0, arr_x + 1000, -1);
int arr_y[1000] = { 0 }; fill(arr_y + 0, arr_y + 1000, -1);
//coordinates used to go back when the generation runs into blocked path
char arr[100][100]; memset(arr, '#', sizeof(char) * 100 * 100);
//creating 100x100 char array of #
int rows, cols = 0;
cout << "Rows "; cin >> rows;//choose
cout << "Columns"; cin >> cols;
int random_number = 0; int x = 0; int y = 0; int index = 0;
arr[0][0] = ' ';
while (1)
{
random_number = rand() % (4) + 1;
int available_paths = 0;
if (x + 1 >= 0 && x + 1 < rows && arr[x + 1][y] != ' ' && arr[x + 2][y] != ' ' && arr[x + 1][y + 1] != ' ' && arr[x + 1][y - 1] != ' ') { available_paths++; if (random_number == 1) { x = x + 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
if (x - 1 >= 0 && x - 1 < rows && arr[x - 1][y] != ' ' && arr[x - 2][y] != ' ' && arr[x - 1][y + 1] != ' ' && arr[x - 1][y - 1] != ' ') { available_paths++; if (random_number == 2) { x = x - 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
if (y + 1 >= 0 && y + 1 < cols && arr[x][y + 1] != ' ' && arr[x][y + 2] != ' ' && arr[x + 1][y + 1] != ' ' && arr[x - 1][y + 1] != ' ') { available_paths++; if (random_number == 3) { y = y + 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
if (y - 1 >= 0 && y - 1 < cols && arr[x][y - 1] != ' ' && arr[x][y - 2] != ' ' && arr[x + 1][y - 1] != ' ' && arr[x - 1][y - 1] != ' ') { available_paths++; if (random_number == 4) { y = y - 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
if (available_paths == 0) //if available_paths are 0 I need to go
{ //back to the last visited square
index = index - 1;
if (index < 0) { break; }
x = arr_x[index]; y = arr_y[index];
}
}
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
{
cout << "[" << arr[x][y] << "]";
if (y == (cols - 1)) { cout << endl; }
}
}
int end;
cin >> end;
}
I am thinking about using this character - ▮. Is there any simple way to improve my algorithm?
I've been struggling to understand how the function long long number here works. The bit that I can't fully grasp is the for cycles in the if's. Why when we have a number in dec do we have to raise it to that power? Shouldn't we just sum it up and leave it? Also why do we raise the other numbers to that power?
Here is the code:
int counter(long long n, int k) {
int counter = 0;
while (n != 0) {
counter++;
n /= k;
}
return counter;
}
int number2(long long n, int number) {
return (n / (long long) pow(10, number)) % 10;
}
int toDecimal(long long n, int k) {
long long decimal = 0;
for (int i = 0; i < counter(n, 10); i++) {
decimal += number2(n, i)*(int)pow(k, i);
}
return decimal;
}
long long number(char *arr, int start) {
int end = start;
long long number2 = 0;
while (*(arr + end) != ' ' && *(arr + end) != '\0') {
end++;
}
int numberSize = end - start;
if (*(arr + start) != '0') {
for (int i = 0; i < numberSize; i++) {
number2 += (*(arr + start + i) - '0')*pow(10, numberSize - i - 1);
}
return number2;
}
if (*(arr + start) == '0' && (*(arr + start + 1) != 'b' && *(arr + start + 1) != 'x')) {
for (int i = 1; i < numberSize; i++) {
number2 += (*(arr + start + i) - '0')*pow(10, numberSize - i - 1);
}
return toDecimal(number2, 8);
}
if (*(arr + start) == '0' && *(arr + start + 1) == 'b') {
for (int i = 2; i < numberSize; i++) {
number2 += (*(arr + start + i) - '0')*pow(10, numberSize - i - 1);
}
return toDecimal(number2, 2);
}
if (*(arr + start) == '0' && *(arr + start + 1) == 'x') {
int *hex = new int[numberSize - 2];
for (int i = 2; i < numberSize; i++) {
if (*(arr + start + i) >= '0'&&
*(arr + start + i) <= '9')
arr[i - 2] = (*(arr + start + i) - '0');
if (*(arr + start + i) >= 'A'&&
*(arr + start + i) <= 'F')
arr[i - 2] = (int)(*(arr + start + i) - '7');
number2 += arr[i - 2] * pow(16, numberSize - i - 1);
}
delete[] hex;
return number2;
}
}
int main() {
char first[1000];
cin.getline(first, 1000);
int size = strlen(first);
long numberr = number(&first[0], 0);
for (int counter = 0; counter < size; counter++) {
if (first[counter] == ' '&&first[counter + 1] == '+') {
numberr += number(&first[0], counter + 3);
}
}
cout << numberr << "\n";
return 0;
}
The number is a string and is a sequence of single characters representing digits. You have to convert the characters to numbers ("1" --> 1) and then multiply it by the right number of tens to move it to the right place. For example: "123" --> (1 * 10^2) + (2 * 10^1) + (3 * 10^0)
I'm kinda new to c++ and I have an assignment for my course to read a P6 ppm image and use some filters on it.Im stuck on the blurring one and I cannot find my logical error on my code.
My problem is that the output image is blurred but it is like there are 4 blurred same images there
EDIT- HERE IS THE LINK TO OUTPUT IMAGE(TESTFILTER.ppm) THE INPUT IMAGE (Original) AND A SIMPLE VIEWER TO VIEW THEM MY PROFFESSOR GAVE US (CPIViewer)
http://www.filedropper.com/blurfilterquestion
Here is the blur filter code.
typedef Vec3<float> buffer;
static void blur(Image* pic) { //BLUR FILTER
int h = pic->getHeight();
int w = pic->getWidth();
int count = 0;
buffer* temp = new buffer[h*w];
for (int x = 0; x < w; x++){
for (int y = 0; y < h; y++){
if (x == 0 && y == 0) {
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x + 1, y) + pic->getPixel(x, y + 1) + pic->getPixel(x + 1, y + 1)) / 4;
}
else if (x == 0 && y == h - 1) {
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x + 1, y) + pic->getPixel(x, y - 1) + pic->getPixel(x + 1, y - 1)) / 4;
}
else if (x == w - 1 && y == 0){
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x - 1, y) + pic->getPixel(x, y + 1) + pic->getPixel(x - 1, y + 1)) / 4;
}
else if (x == w - 1 && y == h - 1){
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x - 1, y) + pic->getPixel(x, y - 1) + pic->getPixel(x - 1, y - 1)) / 4;
}
else if (x == 0){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x + 1, y - 1)) + (pic->getPixel(x + 1, y)) + (pic->getPixel(x + 1, y + 1))) / 6;
}
else if (x == w - 1){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x - 1, y + 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x - 1, y - 1))) / 6;
}
else if (y == 0){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x - 1, y + 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x + 1, y + 1))) / 6;
}
else if (y == h - 1){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x + 1, y - 1)) + (pic->getPixel(x + 1, y)) + (pic->getPixel(x - 1, y - 1))) / 6;
}
else {
count++;
temp[y*w + x].r = ((pic->getPixel(x, y).r) + (pic->getPixel(x + 1, y).r) + (pic->getPixel(x, y + 1).r) + (pic->getPixel(x, y - 1).r) + (pic->getPixel(x - 1, y).r) + (pic->getPixel(x - 1, y - 1).r) + (pic->getPixel(x + 1, y + 1).r) + (pic->getPixel(x + 1, y - 1).r) + (pic->getPixel(x - 1, y + 1).r)) / 9;
temp[y*w + x].g = ((pic->getPixel(x, y).g) + (pic->getPixel(x + 1, y).g) + (pic->getPixel(x, y + 1).g) + (pic->getPixel(x, y - 1).g) + (pic->getPixel(x - 1, y).g) + (pic->getPixel(x - 1, y - 1).g) + (pic->getPixel(x + 1, y + 1).g) + (pic->getPixel(x + 1, y - 1).g) + (pic->getPixel(x - 1, y + 1).g)) / 9;
temp[y*w + x].b = ((pic->getPixel(x, y).b) + (pic->getPixel(x + 1, y).b) + (pic->getPixel(x, y + 1).b) + (pic->getPixel(x, y - 1).b) + (pic->getPixel(x - 1, y).b) + (pic->getPixel(x - 1, y - 1).b) + (pic->getPixel(x + 1, y + 1).b) + (pic->getPixel(x + 1, y - 1).b) + (pic->getPixel(x - 1, y + 1).b)) / 9;
}
}
}
const buffer* constTemp;
constTemp = temp;
pic->setData(constTemp);
std::cout << "TIMES: " << count << std::endl;
here is the getPixel method
typedef Vec3<float> color;
enter code here
color Image::getPixel(unsigned int x, unsigned int y) const {
if (x > getWidth() || y > getHeight()) { color col(0, 0, 0); return col; }
color col = buffer[y*width + x];
return col;
}
and here is my main() method
#include "Image.h"
#include "ppm_format.h"
#include "Filters.h"
#include <iostream>
using namespace imaging;
using namespace std;
using namespace filtering;
int main(int argc, char* argv[]) {
Image* image = ReadPPM(argv[argc - 1]); // Creating Image
float r, g, b;
bool filterFlag = false; //Flag used to check if -f command is given
string check;
Vec3<float> temp;
for (int i = 1; i < argc - 2; i++) {
string check(argv[i]);
if (!check.compare("-f")) { filterFlag = true; } // check.compare("-f") returns 0 if check = -f
if (filterFlag) {
i++;
check = argv[i];
if (!check.compare("gray")) {
filterFlag = false; //filter flag falsified to show that a filter is read
gray(image);
}
else if (!check.compare("color")) {
filterFlag = false;
if (argc-2 <= i + 3) {
cout << "INVALID COLOR FILTER CALL.EXITING...";
system("pause");
exit(0);
}
r = i + 1;
g = i + 2;
b = i + 3;
i += 4;
color(image, r, g, b);
}
else if (!check.compare("blur")) {
filterFlag = false;
blur(image);
}
else if (!check.compare("median")){
filterFlag = false;
medianORdiff(image,"median");
}
else if (!check.compare("diff")){
filterFlag = false;
medianORdiff(image,"diff");
}
else {
cout << "Invalid filter name or no filter name at all.\nFilter names are gray , color r g b , blur , median , diff";
system("pause");
exit(0);
}
*image >> "C://Users/M1TR0S0UL4S/Desktop/TESTFILTER.ppm";
}
}