I am trying to write code which generates ASCII art of this shape:
*
***
*****
******* *
********* ***
*********** *****
********************
**********************
************************
**************************
****************************
******************************
The code needs to be able to generate this shape based upon an input height. As you can see from the example shape, my code correctly generates the ASCII art using a line height of 12. However, for line heights of 3, 5, 6, 7, 10, 11, 15,... it doesn't correctly generate the ASCII shape. I have tried to debug this myself, but I can't find a commonality between the failing line heights, which is preventing me from nailing down the problem with my algorithm.
This is the Java code which I am using to generate the ASCII art shape (though without a hard-coded line height of 12 of course):
int h = 12;
for (int i = 0; i < h; i++) {
for (int j = 0; j < h - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = i; j >= 0; j--) {
System.out.print("*");
}
for (int j = 0; j < h / 2 - i; j++) {
System.out.print(" ");
}
for (int j = h / 2 - i; j > 0; j--) {
System.out.print(" ");
}
for (int j = 0; j <= h / 2; j++) {
if (i >= h / 2)
System.out.print("*");
}
for (int j = 0; j < i - h / 5; j++) {
if (i >= h / 4 && i < h / 2)
System.out.print("*");
}
for (int j = i - h / 5 - 1; j > 0; j--) {
if (i >= h / 4 && i < h / 2)
System.out.print("*");
}
System.out.println();
}
What exactly is causing my ASCII art generator code to fail for certain line heights, and how do I fix that problem with the code so that it correctly generates ASCII art for any positive integer?