2009-06-20 20 views
8

私はすべての動きがグリッドベースのゲームを作っています。私はまた、サークル内を周りを回ることができるエンティティを作ることを望んでいません。だから、誰かが正方形のグリッドから円を作成するための提案があります(MS Paintがサークルツールを使用して正方形のピクセルから円を描くように)。グリッド上で円を描くにはどうすればいいですか?

答えて

3

ここは私のJava実装のBressenham's Midpoint Circleアルゴリズムです。

private void drawCircle(final int centerX, final int centerY, final int radius) { 
    int d = 3 - (2 * radius); 
    int x = 0; 
    int y = radius; 
    Color circleColor = Color.white; 

    do { 
     image.setPixel(centerX + x, centerY + y, circleColor); 
     image.setPixel(centerX + x, centerY - y, circleColor); 
     image.setPixel(centerX - x, centerY + y, circleColor); 
     image.setPixel(centerX - x, centerY - y, circleColor); 
     image.setPixel(centerX + y, centerY + x, circleColor); 
     image.setPixel(centerX + y, centerY - x, circleColor); 
     image.setPixel(centerX - y, centerY + x, circleColor); 
     image.setPixel(centerX - y, centerY - x, circleColor); 
     if (d < 0) { 
      d = d + (4 * x) + 6; 
     } else { 
      d = d + 4 * (x - y) + 10; 
      y--; 
     } 
     x++; 
    } while (x <= y); 
} 

ロゼッタサイトでフルクラスの実装とその他の多くの言語の例が見つかります。 http://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm

関連する問題