// circle.java
//
// This file contains code to draw a circle on the text screen using
// a two-dimensional array.

import java.io.*;

class circle {

static final int WIDTH = 79;
static final int HEIGHT = 24;

/* clear the screen buffer by putting blanks in each array element */

static void clear (char screen[][]) {
	int	i, j;

	for (i=0; i<WIDTH; i++) for (j=0; j<HEIGHT; j++) screen[i][j] = ' ';
}

/* output the screen buffer to the standard output */

static void print (char screen[][]) {
	int	i, j;

	for (j=0; j<HEIGHT; j++) {
		for (i=0; i<WIDTH; i++) System.out.print (screen[i][j]);
		System.out.println ();
	}
}

/* draw a circle by placing stars in the array using the formula
 * for a unit circle y = Math.sqrt (1 - x*x) 
 */
static void circle (char screen[][], int i, int j, int radius) {
	double	x, y;
	int	di, dj;

	/* x will go from 0 through 1 in steps of 0.01 */

	for (x=0.0; x<=1.0; x+=0.01) {

		/* y is the y coordinate of the point (x,y)
		 * on the unit circle
		 */
		y = Math.sqrt (1.0 - x*x);

		/* multiply by the radius (factor of 1.7 makes it look more
		 * circular on some monitors
		 */
		di = (int) (x * radius * 1.7);
		dj = (int) (y * radius);

		/* put a star in each quadrant for this point,
		 * offset from the center of the circle
		 */

		screen[i + di][j + dj] = '*';
		screen[i + di][j - dj] = '*';
		screen[i - di][j + dj] = '*';
		screen[i - di][j - dj] = '*';
	}
}

public static void main (String args[]) {
	char	screen[][] = new char[WIDTH][HEIGHT];

	/* clear (initialize) the screen buffer */

	clear (screen);

	/* put a circle of radius 10 at (x,y)=(40,12) */

	circle (screen, 40, 12, 10);

	/* print the screen buffer to standard output */

	print (screen);
}

}
