+ 2

Help me draw a circle pattern using * symbol in java or python

Here is the code but the circle is not as good as it should be. public static void main(String[] args) { int P; // For Creating A Square int x, y;// Coordinates Of Square int r;//Radius Of Circle int c;//Every Single Point Of Circle (Equation If Circle) Scanner sc = new Scanner(System.in); System.out.print("Enter Radius Of Circle : "); r = sc.nextInt(); // Crate A Circle Of P*P P = 2*r; // Draw A Square Of Size P*P. for (int i = 0; i <= P; i++){ for (int j = 0; j <= P; j++){ // Start From The Left Corner Of Circle x = r-i; y = r-j; c = x*x + y*y ; //If Point (x,y) Is Inside The Circle if (c <= r*r+1 ) { System.out.print("* "); } else { // If (x,y) Is Outside Of Circle System.out.print(" "); } } System.out.println();//Jump In A New Line } }

3rd Oct 2021, 3:02 PM
Coder
Coder - avatar
1 Antwort
+ 1
What do you want to improve? Is it looking elliptical? Is it not dark enough? I made a few adjustments below to make it look more circular(same height/width) as tested in Sololearn's Code Playground and added the fontAspectRatio final variable/constant. It also looks darker because '*' is used for every character instead of having spaces between each. public static void main(String[] args) { int P; // For Creating A Square int x, y;// Coordinates Of Square int r;//Radius Of Circle double c;//Every Single Point Of Circle (Equation If Circle) Scanner sc = new Scanner(System.in); System.out.print("Enter Radius Of Circle : "); r = sc.nextInt(); System.out.println(); // make sure the circle starts on a new line. // Crate A Circle Of P*P P = 2*r; final double fontAspectRatio = 1.62868; // Adjust the fontAspectRatio if you see your circle looking a bit elliptical. // Draw A Square Of Size P*P. for (int i = 0; i <= P; i++){ for (int j = 0; j <= P; j++){ // Start From The Left Corner Of Circle x = r-i; y = r-j; c = x*x*fontAspectRatio*fontAspectRatio + y*y ; char ch; //If Point (x,y) Is Inside The Circle if (c <= r*r ) { ch='*'; } else { // If (x,y) Is Outside Of Circle ch = ' '; } System.out.print(ch); } System.out.println();//Jump In A New Line } }
4th Oct 2021, 1:15 PM
Josh Greig
Josh Greig - avatar