Facebook Pixel

Complex Boundary Conditions in Pattern Printing

Combine geometric logic with pattern rules to create advanced shapes like hollow triangles and crossed boxes.

Advanced Coordinate Logic

Once you understand how to print a hollow square using coordinate boundaries, you can apply this logic to much more complex shapes, like a hollow right-angled triangle.

The Hollow Triangle Problem

If N = 5:

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

The Boundary Analysis

First, we set up the loop for a standard solid triangle (so the inner loop runs to col <= row). Now, what coordinates get a star?

  1. The left vertical wall: col == 1
  2. The bottom horizontal wall: row == n
  3. The diagonal hypotenuse: This is where the column equals the row! col == row

The Implementation

for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        // The three boundaries
        if (col == 1 || row == n || col == row) {
            System.out.print("*");
        } else {
            System.out.print(" ");
        }
    }
    System.out.println();
}

The Crossed Box (Square with Diagonals)

What if you need a square with an 'X' inside it? Set up an N x N solid square loop. Boundaries:

  • Box edges: row == 1 || row == n || col == 1 || col == n
  • Main diagonal: row == col
  • Anti-diagonal: row + col == n + 1

If any of those are true, print a star. Else, print a space.

The Takeaway

You can draw almost any geometric shape in a terminal by combining boundary constraints and diagonal coordinate logic inside a standard nested loop structure.

The boundaries are the first column (col == 1), the last row (row == N), and the diagonal edge where the column index equals the row index (col == row).

Assuming 1-based indexing, the anti-diagonal occurs where the sum of the row and column coordinates equals N + 1.

Because we only want to evaluate the coordinate space that actually exists inside the triangle boundary. Evaluating the full N x N grid would require unnecessary blank space calculations.

You use the logical OR (||) operator in your if-statement. If any one of the geometric boundary conditions is met, a star is printed.

Yes, identifying diagonals and boundaries using coordinates is required for solving matrix problems like the N-Queens problem or Tic-Tac-Toe validation.

Please Login.
Please Login.
Please Login.
Please Login.