Skip to content

Commit

Permalink
🐛 a spiral's radius now excludes the center
Browse files Browse the repository at this point in the history
This is more consistent with a ring's radius.
  • Loading branch information
flauwekeul committed Aug 25, 2022
1 parent 0879b85 commit 8e5d2b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions docs/guide/traversing-grids.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ An example:
```typescript
const Hex = defineHex({ dimensions: 30 })
const grid = new Grid(Hex, rectangle({ width: 5, height: 5 }))
const spiralTraverser = spiral({ start: [0, 2], radius: 2 })
const spiralTraverser = spiral({ start: [0, 2], radius: 1 })

grid.traverse(spiralTraverser)
```

* Line 2: create a rectangular grid of 5⨉5 hexes.
* Line 3: create a traverser with the built-in `spiral()` function (`spiral()` *returns* a traverser). The spiral starts at the hex with coordinates `[0, 2]` and runs outward until its radius of 2 hexes (including the center hex) is reached for a total of 7 hexes.
* Line 3: create a traverser with the built-in `spiral()` function (`spiral()` *returns* a traverser). The spiral starts at the hex with coordinates `[0, 2]` and runs outward until its radius of 1 hex (excluding the center hex) is reached for a total of 7 hexes.
* Line 5: [`grid.traverse()`](/api/classes/Grid#traverse) internally calls `spiralTraverser` and loops over the hexes it produces. Only the hexes that are present in `grid` are returned in a new grid. In this example all hexes produced by the traverser are also present in `grid`.

This can be visualized like so:

<TileGrid :grid="grid" :traversal="spiral1" />

When the radius is increased from `2` to `3` it becomes apparent that only the hexes present in `grid` are traversed. You see that the traversal "jumps" from `[-2, 4]` to `[0, 0]`:
When the radius is increased from `1` to `2` it becomes apparent that only the hexes present in `grid` are traversed. You see that the traversal "jumps" from `[-2, 4]` to `[0, 0]`:

<TileGrid :grid="grid" :traversal="spiral2" />

Expand Down Expand Up @@ -294,9 +294,9 @@ import TileGrid from '../components/TileGrid.vue';

const Hex = defineHex({ dimensions: 30 })
const grid = new Grid(Hex, rectangle({ width: 5, height: 5 }))
const spiral1 = grid.traverse(spiral({ start: [0, 2], radius: 2 }))
const spiral2 = grid.traverse(spiral({ start: [0, 2], radius: 3 }))
const spiral3 = grid.traverse(spiral({ start: [0, 2], radius: 3 }), { bail: true })
const spiral1 = grid.traverse(spiral({ start: [0, 2], radius: 1 }))
const spiral2 = grid.traverse(spiral({ start: [0, 2], radius: 2 }))
const spiral3 = grid.traverse(spiral({ start: [0, 2], radius: 2 }), { bail: true })
const squareOutline1 = grid.traverse([
line({ direction: Direction.E, length: 4 }),
line({ direction: Direction.S, length: 3 }),
Expand Down
8 changes: 4 additions & 4 deletions src/grid/traversers/spiral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const createHex = vi.fn((coordinates?: HexCoordinates) => new Hex(coordinates))
describe('when called with a radius', () => {
describe('without cursor', () => {
test('returns a traverser that returns hexes in a spiral starting at [0, 0]', () => {
expect(spiral({ radius: 2 })(createHex)).toMatchInlineSnapshot(`
expect(spiral({ radius: 1 })(createHex)).toMatchInlineSnapshot(`
[
Hex {
"q": 0,
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('when called with a radius', () => {
describe('when called with a radius and start', () => {
describe('without cursor', () => {
test('returns a traverser that returns hexes in a spiral starting at start', () => {
expect(spiral({ radius: 2, start: [1, 0] })(createHex)).toMatchInlineSnapshot(`
expect(spiral({ radius: 1, start: [1, 0] })(createHex)).toMatchInlineSnapshot(`
[
Hex {
"q": 1,
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('when called with a radius and start', () => {

describe('with cursor', () => {
test('returns a traverser that returns hexes in a spiral starting at start', () => {
expect(spiral({ radius: 2, start: [1, 1] })(createHex, cursor)).toMatchInlineSnapshot(`
expect(spiral({ radius: 1, start: [1, 1] })(createHex, cursor)).toMatchInlineSnapshot(`
[
Hex {
"q": 1,
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('when called with a radius and start', () => {

describe('when called with a radius and rotation', () => {
test('returns a traverser that returns hexes in a spiral with the given rotation', () => {
expect(spiral({ radius: 2, rotation: Rotation.COUNTERCLOCKWISE })(createHex)).toMatchInlineSnapshot(`
expect(spiral({ radius: 1, rotation: Rotation.COUNTERCLOCKWISE })(createHex)).toMatchInlineSnapshot(`
[
Hex {
"q": 0,
Expand Down
8 changes: 4 additions & 4 deletions src/grid/traversers/spiral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { ring } from './ring'
export function spiral<T extends Hex>({ radius, start, rotation }: SpiralOptions): Traverser<T> {
return function spiralTraverser(createHex, cursor) {
const center = createHex(start ?? cursor)
return repeatWith<T>(line({ start, direction: Direction.N, length: radius }), ring({ center, rotation }))(
createHex,
cursor,
)
// radius excludes the center, so 1 is added to radius
// only when there's a cursor but no start, radius can be used as-is, because then line() already increases its length by 1
const length = !start && cursor ? radius : radius + 1
return repeatWith<T>(line({ start, direction: Direction.N, length }), ring({ center, rotation }))(createHex, cursor)
}
}

Expand Down

0 comments on commit 8e5d2b3

Please sign in to comment.