边界填充模拟(C ++)

时间:2015-12-01 01:54:36

标签: c++ recursion matrix divide-and-conquer boundary

我正在尝试使用整数矩阵模拟边界填充方法,在每个位置都有一个0-255的数字,用于标识“像素颜色”,我问一个位置,一个颜色要改变颜色替换它。 我实现的代码适用于方形矩阵,但如果它不是正方形我有两个错误:

1-如果行数大于列数,算法会忽略最后一行并在最后一行上完成所有操作。

2 - 如果列数大于行数,我会在第一次迭代时得到分段错误。

我想向能帮助的人知道,我做错了什么。我试图调试(有印刷品,如果有人可以告诉我一个更好的方式,感谢)。逻辑似乎正确,这种分割错误是奇怪的。

以下是该函数的代码:

void BoundaryFill(int*** img, int x, int y, int newColor, int oldColor, int WIDTH, int HEIGTH){
 if(x >= 0 && x < WIDTH && y >= 0 && y < HEIGTH && (*img)[x][y] == oldColor && (*img)[x][y] != newColor){

     (*img)[x][y] = newColor; //set color before starting recursion
     BoundaryFill(img, x + 1, y, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img, x - 1, y, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img, x, y + 1, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img, x, y - 1, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img,x + 1, y + 1, newColor, oldColor,WIDTH,HEIGHT);
     BoundaryFill(img,x - 1, y - 1, newColor, oldColor,WIDTH,HEIGHT);
     BoundaryFill(img,x - 1, y + 1, newColor, oldColor,WIDTH,HEIGHT);
     BoundaryFill(img,x + 1, y - 1, newColor, oldColor,WIDTH,HEIGHT);
 } }

这是主要代码:

int main(){
int x, y, new_color,old_color;

//Reads the size of the matrix
int HEIGHT; cin >> HEIGHT;
int WIDTH; cin >> WIDTH;

int** img = new int* [HEIGHT];
for (int i=0;i<HEIGHT;i++)
    img[i] = new int [WIDTH];

//Reads the matrix
for (int i=0;i<HEIGHT;i++){
    for (int j=0;j<WIDTH;j++){
        cin >> img[i][j];
    }
}

cin >> x >> y;
old_color = 1; //assuming the old color always gonna be 1
cin >> new_color;

BoundaryFill(&img,x,y,new_color,old_color ,WIDTH,HEIGHT);

//Shows the matrix
for (int i=0;i<HEIGHT;i++){
    for (int j=0;j<WIDTH;j++){
        cout << img[i][j] << " ";
    }
    cout << endl;
}

//Free the HEAP
for(int i = 0;i < WIDTH; i++)
    delete []img[i];
delete []img;}

以下是我用来测试的一些带矩阵的输入文件(无法将输入的数字放在矩阵格式上,但这些数字的含义是:首先定义矩阵的大小,行数和列数。最后三个定义起始位置和将替换的颜色。其余的是矩阵值的输入:

更多行:

  

8 7 2 1 1 1 1 1 2 1 2 1 1 1 2 1 1 1 2 1 1 1 1 1 1 2 1 1 1 1 1 2 1 2   1 1 1 2 1 1 1 2 1 2 1 1 1 1 1 2 2 2 1 1 1 2 2 2 1

更多专栏:

  

7 8   2 1 1 1 1 1 2 2   1 2 1 1 1 2 1 1   1 1 2 1 2 1 1 1   1 1 1 2 1 1 1 1   1 1 2 1 2 1 1 1   1 2 1 1 1 2 1 1   2 1 1 1 1 1 2 2   2 1   5

1 个答案:

答案 0 :(得分:0)

from StringIO import StringIO import matplotlib.pyplot as plt import numpy as np import requests import shapely.geometry as shp # Read the points AFURL = 'http://m-selig.ae.illinois.edu/ads/coord_seligFmt/ah79100c.dat' afpts = np.loadtxt(StringIO(requests.get(AFURL).content), skiprows=1) # Create a Polygon from the nx2 array in `afpts` afpoly = shp.Polygon(afpts) # Create offset airfoils, both inward and outward poffafpoly = afpoly.buffer(0.03) # Outward offset noffafpoly = afpoly.buffer(-0.03) # Inward offset # Turn polygon points into numpy arrays for plotting afpolypts = np.array(afpoly.exterior) poffafpolypts = np.array(poffafpoly.exterior) noffafpolypts = np.array(noffafpoly.exterior) # Plot points plt.plot(*afpolypts.T, color='black') plt.plot(*poffafpolypts.T, color='red') plt.plot(*noffafpolypts.T, color='green') plt.axis('equal') plt.show() 中设置数组时,main的第一个下标是垂直(HEIGHT),第二个是水平(WIDTH)。在img中,在第一个使用水平索引而第二个使用垂直索引的情况下,您可以使用此方法。

您应该在BoundaryFill中使用(*img)[y][x]

相关问题