円を描く、Javaで
概要
コマンドラインで指定するサイズの円をテキスト画面に描画するプログラム
public class CircleDrawer { static int size; static int height, width; static double theta; static int radius; public static void main(String[] args) { int i, j; radius = Integer.parseInt(args[0]); size = radius * 2 + 5; height = size; width = size;// 描画サイズ char drawable[][];// 領域名設定 drawable = new char[height][width];// 領域確保 for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { drawable[j][i] = '・'; } } for (theta = 0; theta < 360; theta += (90 / size)) { i = (int) (radius * Math.cos(theta * Math.PI / 180) + radius + 2); j = (int) (radius * Math.sin(theta * Math.PI / 180) + radius + 2); // System.out // .println((int) (radius * Math.cos(theta * Math.PI / 180) + radius) // + " " // + (int) (radius * Math.sin(theta * Math.PI / 180) + radius)); // System.out.println(); drawable[j][i] = '0'; } for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { System.out.print(drawable[j][i]); } System.out.println(); } } }
実行結果(コマンドラインに10を入力した場合)
・・・・・・・・・・・・・・・・・・・・・・・・・
・・・・・・・・・・・・・・・・・・・・・・・・・
・・・・・・・0000000000・・・・・・・・
・・・・・・00・・・・・・・・00・・・・・・・
・・・・00・・・・・・・・・・・・00・・・・・
・・・・0・・・・・・・・・・・・・・0・・・・・
・・・0・・・・・・・・・・・・・・・・0・・・・
・・00・・・・・・・・・・・・・・・・00・・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・0・・・・・・・・・・・・・・・・・・00・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・0・・・・・・・・・・・・・・・・・・0・・・
・・00・・・・・・・・・・・・・・・・00・・・
・・・0・・・・・・・・・・・・・・・・0・・・・
・・・・0・・・・・・・・・・・・・・0・・・・・
・・・・00・・・・・・・・・・・・00・・・・・
・・・・・・00・・・・・・・・00・・・・・・・
・・・・・・・0000000000・・・・・・・・
・・・・・・・・・・・・0・・・・・・・・・・・・
・・・・・・・・・・・・・・・・・・・・・・・・・
・・・・・・・・・・・・・・・・・・・・・・・・・
感想
ちょっとイビツ