Skip to main content

Math Utils

Overview

The @tscircuit/math-utils package provides a set of utilities that are commonly used in circuit design. The @tscircuit/math-utils package is generally available in any platform that uses tscircuit.

The source code for the @tscircuit/math-utils package is available here

grid

A utility function that generates a grid of cells with configurable dimensions, spacing, and positioning. Each cell contains its index, position (row/column), and coordinate points (center, top-left, bottom-right).

import { grid } from "@tscircuit/math-utils"

const gridCells = grid({
rows: 4,
cols: 4,
width: 10,
height: 10,
// xSpacing: 6, // if you want to provide the spacing instead of width
// ySpacing: 6, // if you want to provide the spacing instead of height
offsetX: 0, // optional
offsetY: 0, // optional
yDirection: "cartesian", // optional, default: "cartesian"
centered: true // optional, default: true
})

export default () => (
<board width="10mm" height="10mm" pcbRelative>
{gridCells.map((cell) => (
<led
name={'LED' + cell.index}
footprint="0402"
schX={cell.center.x}
schY={cell.center.y}
pcbX={cell.center.x}
pcbY={cell.center.y}
/>
))}
</board>
)
PCB Circuit Preview

Grid Options

OptionDescriptionDefault
rowsNumber of rows in the generated grid.Required
colsNumber of columns in the generated grid.Required
widthTotal width of the grid. When omitted, uses cols * xSpacing.cols * xSpacing
heightTotal height of the grid. When omitted, uses rows * ySpacing.rows * ySpacing
xSpacingHorizontal spacing between cells when width is not provided.1
ySpacingVertical spacing between cells when height is not provided.1
offsetXHorizontal offset applied to every cell.0
offsetYVertical offset applied to every cell.0
yDirectionSets positive Y direction: "cartesian" keeps positive-up, "up-is-negative" flips it."cartesian"
centeredCenters the grid around the origin when true.true

Grid Cell Data

Each object returned by grid represents a cell with useful positional metadata:

PropertyDescription
indexSequential identifier for the cell.
row / colGrid coordinates of the cell starting from zero.
center{ x, y } coordinates of the cell center.
topLeft{ x, y } coordinates of the cell's top-left corner.
bottomRight{ x, y } coordinates of the cell's bottom-right corner.