# hex-layout

> A TypeScript library for calculating hexagonal layouts.

Latest version **1.0.0** (published 2021-01-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install hex-layout
pnpm add hex-layout
yarn add hex-layout
bun add hex-layout
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-01-01 |
| First published | 2021-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 77.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | jraymakers |

## Links

- npm: https://www.npmjs.com/package/hex-layout
- Repository: https://github.com/jraymakers/hex-layout
- Homepage: https://github.com/jraymakers/hex-layout#readme
- Issues: https://github.com/jraymakers/hex-layout/issues
- npm.io page: https://npm.io/package/hex-layout

## Recent versions

- 1.0.0 (latest) — 2021-01-01

## README

# Hex Layout

A TypeScript library for calculating hexagonal layouts.

## Example

```typescript
// Select a distance between centers of adjacent hexes.
const centerToCenterDistance = 100;

// Shift the 0,0 hex to lie entirely within the positive x,y quadrant.
const origin = new Vector(centerToCenterDistance / 2, centerToCenterDistance / Math.sqrt(3));

// Align the q axis, the first hexagonal axis, with the x axis.
const qAxisAngle = 0;

// Set the r axis, the secon hexagonal axis, to be 60° clockwise of the q axis.
const rAxisClockwise = true;

// Create a layout using the above settings.
const layout = new HexLayout({
  centerToCenterDistance,
  origin,
  qAxisAngle,
  rAxisClockwise,
});

// Create 8 rows of alternating 8 and 7 seven hexes:
//
//    0,0 hex                     7,0 hex
//   /                           /
//  *   *   *   *   *   *   *   *  - q axis
//    *   *   *   *   *   *   *
//  *   *   *   *   *   *   *   *
//    *   *   *   *   *   *   *
//  *   *   *   *   *   *   *   *
//    *   *   *   *   *   *   *
//  *   *   *   *   *   *   *   *
//    *   *   *   *   *   *   *
//               / \
//        0,7 hex   r axis
//
const hexes: Hex[] = [];
for (let r = 0; r < 8; r++) {
  for (let c = 0; c < 8 - r%2; c++) {
    hexes.push(new Hex(c - Math.floor(r/2), r));
  }
}

// Calculate the bounds of the hexes.
// Useful for sizing a display (e.g. an SVG element).
const bounds = layout.bounds(hexes);
const width = bounds.width();
const height = bounds.height();

// Calculate the x,y coordinates of the center of each hex.
const centers = hexes.map((hex) => layout.centerOfHex(hex));

// Calculate the corners of each hex.
const boundaries = hexes.map((hex) => layout.cornersOfHex(hex));

// Determine which hex a given point lies in.
const hex = layout.hexFromPoint(new Vector(x,y));
```

---
_Source: https://npm.io/package/hex-layout · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
