Visualize the Butterfly pattern with mirrored stars on both sides of each row, expanding toward the center then contracting.
About this algorithm
The butterfly prints two triangles that grow in from the left and right edges until they meet in the middle row, then shrink back, so the shape looks like a pair of wings. Each row has a left wing, a gap and a right wing, and the gap closes as the wings grow.
Take on the butterfly last. It combines the mirror trick from the diamond (a value that rises to the middle row and then falls) with a new idea: a star rule that tests two ranges at once, one measured from the left edge and one from the right. If you can explain why the middle row is solid here, every other pattern in this set will feel straightforward. The right triangle page is the left wing on its own, if you want to see one half first.
The page uses 2n - 1 rows and 2n - 1 columns. Rows 0 to n - 1 are the growing half, and the rest shrink.
level = i + 1 in the top half and level = (2n - 1) - i in the bottom half. Row 0 has wings 1 star wide; row n - 1 has wings n wide.
A cell is a star if j < level (inside the left wing) or j >= cols - level (inside the right wing). Everything between them is the gap and gets a space.
On the middle row the level is n, so the left wing covers columns 0 to n - 1 and the right wing covers n - 1 to 2n - 2. They share column n - 1, the gap vanishes and the row is solid.
The page loads n = 5: a 9 by 9 grid. Row 0 has level 1, a star at column 0 and one at column 8, with 7 spaces between. Row 1 has level 2: two stars each side, 5 spaces. Row 2 leaves 3 spaces and row 3 leaves just column 4 empty. Row 4 has level 5, both wings reach column 4, and all 9 cells are stars. Rows 5 to 8 repeat rows 3 to 0. Stars per row: 2, 4, 6, 8, 9, 8, 6, 4, 2, which is 49 stars and 32 spaces in 81 cells.
Many tutorials print the butterfly 2n columns wide and give row i exactly 2(n - i - 1) spaces in the gap, so the middle row has 2n stars and the two wings sit side by side without sharing a column. The shape looks the same. This page uses an odd width so the wings share the centre column, and the middle row has 2n - 1 stars instead.
Time: every cell of the (2n - 1) by (2n - 1) grid gets one test, roughly 4n² in total. Space: the level and the two wing bounds are a handful of integers worked out for each row.