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>
)
Grid Options
Option | Description | Default |
---|---|---|
rows | Number of rows in the generated grid. | Required |
cols | Number of columns in the generated grid. | Required |
width | Total width of the grid. When omitted, uses cols * xSpacing . | cols * xSpacing |
height | Total height of the grid. When omitted, uses rows * ySpacing . | rows * ySpacing |
xSpacing | Horizontal spacing between cells when width is not provided. | 1 |
ySpacing | Vertical spacing between cells when height is not provided. | 1 |
offsetX | Horizontal offset applied to every cell. | 0 |
offsetY | Vertical offset applied to every cell. | 0 |
yDirection | Sets positive Y direction: "cartesian" keeps positive-up, "up-is-negative" flips it. | "cartesian" |
centered | Centers the grid around the origin when true . | true |
Grid Cell Data
Each object returned by grid
represents a cell with useful positional metadata:
Property | Description |
---|---|
index | Sequential identifier for the cell. |
row / col | Grid 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. |