-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hex): add center() function and method
When passed a hex (or used as a hex method) it returns the center point relative to the hex with coordinates { q: 0, r: 0 }. When passed a hex prototype it returns the center relative to the prototype's origin.
- Loading branch information
1 parent
b2a0298
commit c0730b5
Showing
7 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { center } from './center' | ||
import { createHex } from './createHex' | ||
import { createHexPrototype } from './createHexPrototype' | ||
|
||
test('returns the center point relative to the hex with coordinates {q: 0, r: 0}, when a hex is passed', () => { | ||
const hexPrototype = createHexPrototype({ dimensions: 10, origin: 'topLeft' }) | ||
const hex = createHex(hexPrototype, { q: 1, r: 2 }) | ||
|
||
expect(center(hex)).toEqual({ x: -34.64101615137754, y: -30 }) | ||
}) | ||
|
||
test(`returns the center point relative to any hex's origin, when a hex prototype is passed`, () => { | ||
const hexPrototype = createHexPrototype({ dimensions: 10, origin: { x: 1, y: 2 } }) | ||
expect(center(hexPrototype)).toEqual({ x: 7.6602540378443855, y: 8 }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Hex, HexPrototype, Point } from '../types' | ||
import { isHex } from './isHex' | ||
|
||
// todo: add to docs that when passed a hex, its center relative to the "origin hex" are returned (different per hex coordinates) | ||
// and when passed hex prototype, its center relative to any hex's origin are returned (always the same) | ||
export function center(hexOrPrototype: Hex | Pick<HexPrototype, 'width' | 'height' | 'origin'>): Point { | ||
const { width, height } = hexOrPrototype | ||
const { x, y } = isHex(hexOrPrototype) ? hexOrPrototype : hexOrPrototype.origin | ||
return { x: width / 2 - x, y: height / 2 - y } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './center' | ||
export * from './cloneHex' | ||
export * from './corners' | ||
export * from './createHex' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters