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

#include "logic.h"
#include <iostream>
/*
COORDINATE SYSTEM:
Example layout of 4x4 pixel file:
(0, 0)  (1, 0)  (2, 0)  (3, 0)
(0, 1)  (1, 1)  (2, 1)  (3, 1)
(0, 2)  (1, 2)  (2, 2)  (3, 2)
(0, 3)  (1, 3)  (2, 3)  (3, 3)
*/
void clearOutliersV2(unsigned char imageData[], unsigned int landMapArray[], const unsigned int res){
std::vector<unsigned int> colorIndices;
unsigned int curIter = 0;
unsigned int maxIter = res*res*3;
// Get colorIndices relating to those pixels 41 away from the edge
// that are not ocean.
// Don't need to clear edge since that's already ocean due to
// makeBeach passes.
while (curIter < maxIter){
unsigned int pixNumber = (curIter/3);// + 1;
unsigned int Y = int(floor(float(pixNumber)/float(res)));
unsigned int X = pixNumber  res*Y;//  1;
if ((X == 41)  (X == (res42))  (Y == 41)  (Y == (res42))){
if (landMapArray[getIndex(X, Y, res)] != 0){
colorIndices.push_back(curIter);
}
}
curIter+= 3;
}
// Now, colorIndices contains color indices to all pixels on
// the edge of the checking boundary that aren't ocean (and need to be changed)
curIter = 0;
// Unspecified amount of passes, as many as we need:
while (true){
// If we've gone through every pixel in the list,
// return:
if (curIter == colorIndices.size()){
return;
}
unsigned int Y = getY(res, colorIndices[curIter]);
unsigned int X = getX(Y, res, colorIndices[curIter]);
// Set pixel to ocean:
imageData[colorIndices[curIter]] = char(0);
imageData[colorIndices[curIter]+1] = char(0);
imageData[colorIndices[curIter]+2] = char(191);
landMapArray[(colorIndices[curIter])/3] = 0;
// Unclear which direction code scans in.
// We need to check on all sides of a pixel.
// Checking up and down:
unsigned int checkUpIndex = getIndex(X, Y1, res);
unsigned int checkDownIndex = getIndex(X, Y+1, res);
unsigned int checkRightIndex = getIndex(X+1, Y, res);
unsigned int checkLeftIndex = getIndex(X1, Y, res);
// Pixel above is land (and so connected to the one we just cleared):
if (landMapArray[checkUpIndex] != 0){
// Check if pixel above is already in the vector,
// and only add it if it isn't already there:
if (std::find(colorIndices.begin(), colorIndices.end(), (checkUpIndex*3)) == colorIndices.end()){
// Add it to the vector to be set to ocean eventually:
colorIndices.push_back(checkUpIndex*3);
std::cout << "\nPixel above added.";
}
}
// Pixel below is land (and so connected to the one we just cleared):
if (landMapArray[checkDownIndex] != 0){
// Check if pixel below is already in the vector,
// and only add it if it isn't already there:
if (std::find(colorIndices.begin(), colorIndices.end(), (checkDownIndex*3)) == colorIndices.end()){
// Add it to the vector to be set to ocean eventually:
colorIndices.push_back(checkDownIndex*3);
std::cout << "\nPixel below added.";
}
}
// Pixel to the right is land (and so connected to the one we just cleared):
if (landMapArray[checkRightIndex] != 0){
// Check if pixel to the right is already in the vector,
// and only add it if it isn't already there:
if (std::find(colorIndices.begin(), colorIndices.end(), (checkRightIndex*3)) == colorIndices.end()){
// Add it to the vector to be set to ocean eventually:
colorIndices.push_back(checkRightIndex*3);
std::cout << "\nPixel to the right added.";
}
}
// Pixel to the left is land (and so connected to the one we just cleared):
if (landMapArray[checkLeftIndex] != 0){
// Check if pixel to the left is already in the vector,
// and only add it if it isn't already there:
if (std::find(colorIndices.begin(), colorIndices.end(), (checkLeftIndex*3)) == colorIndices.end()){
// Add it to the vector to be set to ocean eventually:
colorIndices.push_back(checkLeftIndex*3);
std::cout << "\nPixel to the left added.";
}
}
curIter++;
std::cout << "\nPass " << curIter << " done.";
if (curIter >= 1000000){
return;
}
}
return;
}
// Convert X and Y values into array index
// NOT color array index
int getIndex(unsigned int X, unsigned int Y, const unsigned int res){
return (res*Y + X);
}
unsigned int getX(unsigned int Y, const unsigned int res, unsigned int colorIndex){
unsigned int pixNumber = (colorIndex/3);// + 1;
unsigned int X = pixNumber  res*Y;//  1;
return X;
}
unsigned int getY(const unsigned int res, unsigned int colorIndex){
unsigned int pixNumber = (colorIndex/3);
unsigned int Y = int(floor(float(pixNumber)/float(res)));
return Y;
}
// isPixelInCircle: returns true if pixel in character array at index
// pixIndex (to be converted to (X, Y) values) lies within or on a circle
// centered at (res/2, res/2) with the given radius
bool isPixelInCircle(unsigned int pixIndex, unsigned int res, unsigned int radius){
unsigned int X = res/2;
unsigned int Y = res/2;
unsigned int center[2] = {X, Y};
unsigned int pixNumber = pixIndex + 1;
Y = int(floor(float(pixNumber)/float(res)));
X = pixNumber  res*Y  1;
unsigned int pixelCoord[2] = {X, Y};
if ((pixelCoord[0]center[0])*(pixelCoord[0]center[0]) + (pixelCoord[1]center[1])*(pixelCoord[1]center[1]) <= radius*radius){
return true;
}
else return false;
}
