开发者

Circle selection in an Integer[][] matrix - Java [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have a matrix with n columns, n rows and every element is initialized with 0.

I want to select the largest circle in that matr开发者_C百科ix, and set the values to 1.

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

Can you help me with the java algorithm ?

Language: Java

Thank you, Horatiu


The most efficient way to find proper pixels of a circle boundary is the Bresenhamn's algorithm or Mid Point Circle Algorithm; http://en.wikipedia.org/wiki/Midpoint_circle_algorithm#Optimization

this is how I change that to fit yours:

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;
    }
}


Here's a naive algorithm. It fills in each point to a 0 or 1 depending upon whether it lies inside or outside the circle.

public static void main(String[] args)
{
    int[][] matrix = new int[9][];
    double midPoint = (matrix.length-1)/2.0;
    for (int col = 0; col < matrix.length; col++)
    {
        int[] row = new int[matrix.length];
        double yy = col-midPoint;
        for (int x=0; x<row.length; x++)
        {
           double xx = x-midPoint;
           if (Math.sqrt(xx*xx+yy*yy)<=midPoint)
             row[x] = 1;
           System.out.print(row[x]);
        }
        matrix[col] = row;
        System.out.println();
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜