0

How to draw a double empty rhombus with asterisks? I have implemented the rhombus itself, but I don't understand how to make it double.

     *          *
    * *        * *
   *   *      *   *
  *     *    *     *
 *       *  *       *
*         **         *
 *       *  *       *
  *     *    *     *
   *   *      *   *
    * *        * *
     *          *

My code:

int i, j, n = 6;
String str = "";
for (i = 1; i <= n; i++) {
    for (j = n; j > i; j--)
        str += " ";
    str += "*";
    for (j = 1; j < (i - 1) * 2; j++)
        str += " ";
    if (i == 1)
        str += ("\n");
    else
        str += ("*\n");
}
for (i = 2; i <= n; i++) {
    for (j = 1; j < i; j++)
        str += " ";
    for (j = 1; j <= n * 2 - (2 * i - 1); j++)
        if (i == 0 || j == 1 || j == n * 2 - (2 * i - 1))
            str += "*";
        else
            str += " ";
    str += "\n";
}
System.out.println(str);

4 Answers4

0

Separate your code into two functions that produce a line with one or two asterisks. This function should take a string, the rhombus width and the asterisk position as arguments and return a string with the created line appended to the string given as the argument. Also the created line should be always the same width, that means padded with blanks also on the right side. And it must not append a line-feed. So your code can look like:

private final static int WIDTH = 11;
...
StringBuilder rhombusBuilder = new StringBuilder();
String line = "";

line = singleAsteriskLine( line, WIDTH ); // outputs "     *     "
line = singleAsteriskLine( line, WIDTH ); // outputs "     *          *     "
rhombusBuilder.append( line + "\n" );
for (int pos = 0; pos < (WIDTH - 1) / 2; ++pos) {
    line = doubleAsteriskLine( "", WIDTH, pos ) // pos = 0 "   * *    "
    line = doubleAsteriskLine( line, WIDTH, pos ) // pos = 5 "*         **         *"
// append to stringbuilder
} 
// same backwards....
// append a single asterisk line again...

`

You can add more rhombuses by simply repeating the line-builder functions one more time in each line.

The Calif
  • 11
  • 3
0

I would compute a bitmask for the desired output, calculate the appropriate integer values and then use that to drive everything. Like,

int[] bitmask = { 65568, 163920, 278664, 532740, 1053186, 2100225,
        1053186, 532740, 278664, 163920, 65568 };
for (int v : bitmask) {
    for (int j = 0; j < 22; j++) {
        System.out.print((v & (1 << j)) == 0 ? ' ' : '*');
    }
    System.out.println();
}

Outputs (as requested)

     *          *     
    * *        * *    
   *   *      *   *   
  *     *    *     *  
 *       *  *       * 
*         **         *
 *       *  *       * 
  *     *    *     *  
   *   *      *   *   
    * *        * *    
     *          *     
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I would refactor your code into a method that returns a single line of the rhombus, including the spaces at the end as well as the beginning:

String rhombLine(int i, int n)
{
    String side = repeat(" ", n-i);
    String mid = "*";
    if (i > 1)
    {
        mid += repeat(" ", (i - 1) * 2 - 1);
        mid += "*";
    }
    return side + mid + side;
}

We simplify the code by making use of a method that repeats n copies of a String:

static String repeat(String s, int n)
{
    String r = "";
    for(int i=0; i<n; i++) r += s;
    return r;
}

If you're using Java 11 you can use the String.repeat() method instead.

Now you can just print two copies of the rhombus line on each row:

void rhombus(int n)
{
    for(int i=1; i<=n; i++)
        System.out.println(repeat(rhombLine(i, n), 2));         

    for(int i=n-1; i>0; i--)
        System.out.println(repeat(rhombLine(i, n), 2));                 
}

As an alternative there's a nice trick using recursion that means you only need to generate each line once:

void rhombus(int i, int n)
{       
    String line = repeat(rhombLine(i, n), 2);       
    
    System.out.println(line);
    
    if(i == n) return;
    
    rhombus(i+1, n);
    
    System.out.println(line);
}

This would be called initially via rhombus(1, 6)

RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
0

Imagine a coordinate plane and draw a graph of the function in a rhombus form:

Math.abs(y - n) + Math.abs(x - n) == n

Try it online!

The zero point is in the upper left corner, and axes are directed downward and to the right:

// size of the rhombus
int n = 6;
// number of rhombuses
int m = 4;
// vertical axis
for (int y = 0; y <= 2 * n; y++) {
    // horizontal axis
    for (int x = 0; x < 2 * n * m + m; x++) {
        // current rhombus
        int k = x / (2 * n + 1);
        // center of the rhombus
        int c = (2 * k + 1) * n + k;
        // check if this point is on the rhombus
        if (Math.abs(y - n) + Math.abs(x - c) == n)
            System.out.print("*");
        else
            System.out.print(" ");
    }
    System.out.println();
}

Output:

      *            *            *            *      
     * *          * *          * *          * *     
    *   *        *   *        *   *        *   *    
   *     *      *     *      *     *      *     *   
  *       *    *       *    *       *    *       *  
 *         *  *         *  *         *  *         * 
*           **           **           **           *
 *         *  *         *  *         *  *         * 
  *       *    *       *    *       *    *       *  
   *     *      *     *      *     *      *     *   
    *   *        *   *        *   *        *   *    
     * *          * *          * *          * *     
      *            *            *            *      

See also: Output an ASCII diamond shape using loops