Integer [] []矩阵中的圆选择-Java

Hor*_*lea 1 java algorithm math image-processing

我有一个n列n行的矩阵,每个元素都用0初始化。

我想选择该矩阵中的最大圆,并将值设置为1。

000010000
000111000
000111000
001111100
011111110     The drawing isnt't very artistic (or correct)...  
001111100        matrix: 9*9
000111000        largest circle
000111000
000010000
Run Code Online (Sandbox Code Playgroud)

您能帮我Java算法吗?

语言:Java

谢谢你,Horatiu

Rez*_*eza 5

查找圆边界合适像素的最有效方法是布雷森汉姆算法或中点圆算法;http://en.wikipedia.org/wiki/Midpoint_circle_algorithm#Optimization

这就是我将其更改为适合您的方式的方法:

public class Circle {
    private char[][] px;
    private char cEmpty='.';
    private char cFilled='#';
    public static void main(String[] args) {
        new Circle(15);
    }
    public Circle(int size)
    {
        px=new char[size][size];
        for(int i=0;i<size;i++)
            for(int j=0;j<size;j++)
                px[i][j]=cEmpty;
        calc(size/2,size/2,size/2-1);
        for(int i=0;i<size;i++){
            for(int j=0;j<size;j++)
                System.out.print(px[i][j]);
            System.out.println();
        }
    }

    public void calc(int cx, int cy, int radius)
    {
      int error = -radius;
      int x = radius;
      int y = 0;
      while (x >= y)
      {
        plot8points(cx, cy, x, y);   
        error += y;
        ++y;
        error += y;
        if (error >= 0)
        {
          --x;
          error -= x;
          error -= x;
        }
      }
    }

    void plot8points(int cx, int cy, int x, int y)
    {
      plot4points(cx, cy, x, y);
      if (x != y) plot4points(cx, cy, y, x);
    }
    void plot4points(int cx, int cy, int x, int y)
    {
      setPixel(cx + x, cy + y);
      if (x != 0) setPixel(cx - x, cy + y);
      if (y != 0) setPixel(cx + x, cy - y);
      if (x != 0 && y != 0) setPixel(cx - x, cy - y);
    }
    void setPixel(int x, int y){
        px[x][y]=cFilled;
    }
}
Run Code Online (Sandbox Code Playgroud)