pattern-printing
v1.0.0
Published
A collection of pattern printing functions — concentric squares, diamonds, circles, zigzags, fractals, letter shapes and more. Ported from C logic.
Maintainers
Readme
pattern-printing
A collection of ASCII pattern printing functions for Node.js — ported from original C logic.
Every function accepts n as input and returns the pattern as a plain string.
Install
npm install pattern-printingQuick Start
const patterns = require('pattern-printing');
// Print a concentric square of size 4
console.log(patterns.concentricSquare(4));
// 4444444
// 4333334
// 4322234
// 4321234
// 4322234
// 4333334
// 4444444
// Print a hollow circle of radius 4
console.log(patterns.hollowCircle(4));
// *****
// ** **
// ** **
// * *
// ...
// Print a zigzag of height 5
console.log(patterns.zigzag(5));CLI Usage
# List all available patterns
node index.js
# Run a specific pattern with n
node index.js concentricSquare 4
node index.js zigzag 6
node index.js sierpinskiCross 3
node index.js hollowDiamond 5API Reference
concentricSquare(n)
Prints a (2n−1) × (2n−1) grid where each cell shows n minus its minimum
distance to any edge. Produces nested square rings of numbers.
concentricSquare(4)
→ 4444444
4333334
4322234
4321234
4322234
4333334
4444444hollowConcentricSquare(n)
Like concentricSquare but only draws the outer border and the two main
diagonals. All other cells are spaces.
hollowConcentricSquare(4)
→ 4444444
43 34
4 2 2 4
4 1 4
4 2 2 4
43 34
4444444checkerboard(n)
n × n checkerboard where * appears when (i+j) is even.
checkerboard(4)
→ * *
* *
* *
* *checkerNumbered(n)
n × n board where the value at (i,j) is n minus the min-distance to
any edge, adjusted ±1 by parity of (i+j).
checkerNumbered(4)
→ 4343
3323
4222
3323hollowCircle(radius)
Hollow circle drawn using Euclidean distance. A * is placed wherever the
distance from the centre is within ±0.5 of radius.
hollowCircle(4)
→ *****
** **
** **
* *
* *
* *
** **
** **
***** invertedTriangle(n)
Row i has i leading spaces then 2*(n−1−i)+1 stars.
invertedTriangle(5)
→ *********
*******
*****
***
*recursiveFrames(n)
Nested hollow square frames on an n × n grid. Frames step inward by 2
each time so there is always a gap between them.
recursiveFrames(9)
→ * * * * * * * * *
* *
* * * * * * *
* * * *
* * * * *
...hollowDiamond(n)
Diamond shape made of two halves. Each row has two runs of * with a hollow
gap in between that widens towards the centre.
hollowDiamond(5)
→ ***** *****
**** ****
*** ***
** **
* *
* *
...concentricDiamond(n)
Concentric diamond rings using the formula |mid−i| + |mid−j| == mid for
the outer ring, with inner rings shifted inward.
concentricDiamond(5)
→ *
***
*****
*******
**** ****
**** ****
...zigzag(n)
n-row zigzag of * characters. Stars alternate between a shrinking gap
2*(n−1−i)−1 and an expanding gap 2*i−1 as rows progress.
zigzag(4)
→ * * *
* * * *
* * * *
* * * *letterJ(n)
n × n grid rendering the letter J — full top bar, right stem going
down, then a curved base going left and up.
letterJ(8)
→ ********
*
*
...
* *
* *letterU(n)
n × n grid rendering the letter U — two vertical bars at top, then
converging diagonals meeting at a point at the bottom. n must be even.
letterU(10)
→ * *
* *
...
* *
**sierpinskiCross(k)
Sierpinski-cross fractal on a 3^k × 3^k grid. Each 3×3 block fills the
four cardinal neighbours and leaves the corners and centre empty. Recurse
until size == 1.
| k | Grid size | |---|-----------| | 1 | 3 × 3 | | 2 | 9 × 9 | | 3 | 27 × 27 | | 4 | 81 × 81 |
sierpinskiCross(2)
→ *
***
*
* * *
*********
* * *
*
***
* matrixZeroSpread(matrix)
Given a 2-D array of 0s and 1s, spreads each 1 across its entire row
and column. Returns the resulting matrix as a string.
matrixZeroSpread([[1,0,0,1],[0,0,1,0],[0,0,0,0]])
→ 1111
1111
1010modDiagonal(n)
6 rows × 101 columns. A * appears at column j in row i when
(i + j) % n === 0. Row 0 is blank.
modDiagonal(20)
→ (blank)
* * ...
* * ...modifiedPascal(n)
Pascal's triangle where the last element of every row is decremented by 1.
modifiedPascal(5)
→ 1
1 0
1 1 0
1 2 1 0
1 3 3 1 0License
MIT
