-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: - Added `defineHex()` and other helpers to make it easier to extend `Hex`. - Remove obsolete `createHex()` (replaced by `new Hex()`), `createHexPrototype()` (replaced by extending `Hex` and/or using `defineHex()`) and `isHex()` (replaced by `instanceof`) functions. - Remove standalone hex functions `center()`, `cloneHex()`, `corners()`, `height()`, `isFlat()`, `isPointy()` and `width()` and move them to Hex. - Add `translate()` method to Hex. - Change the type of Hex's `offset` property from a number to a union of `-1` and `1`.
- Loading branch information
1 parent
677953d
commit 6b29a12
Showing
38 changed files
with
292 additions
and
896 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
import { isObject } from '../../utils' | ||
import { BoundingBox, Ellipse, Orientation } from '../types' | ||
|
||
export function createHexDimensions(radius: number): Ellipse | ||
export function createHexDimensions(boundingBox: BoundingBox, orientation: Orientation): Ellipse | ||
export function createHexDimensions(ellipse: Ellipse): Ellipse | ||
export function createHexDimensions(input: number | BoundingBox | Ellipse, orientation?: Orientation): Ellipse { | ||
if (isObject<Ellipse>(input) && input.xRadius > 0 && input.yRadius > 0) { | ||
return input | ||
} | ||
|
||
if (isObject<BoundingBox>(input) && input.width > 0 && input.height > 0) { | ||
const { width, height } = input | ||
return orientation === Orientation.POINTY | ||
? { xRadius: width / Math.sqrt(3), yRadius: height / 2 } | ||
: { xRadius: width / 2, yRadius: height / Math.sqrt(3) } | ||
} | ||
|
||
if (input > 0) { | ||
return { xRadius: input, yRadius: input } as Ellipse | ||
} | ||
|
||
throw new TypeError( | ||
`Invalid dimensions: ${JSON.stringify( | ||
input, | ||
)}. Dimensions must be expressed as an Ellipse ({ xRadius: number, yRadius: number }), a Rectangle ({ width: number, height: number }) or a number.`, | ||
) | ||
} |
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,21 @@ | ||
import { isPoint } from '../../utils' | ||
import { BoundingBox, Point } from '../types' | ||
|
||
export function createHexOrigin(input: 'topLeft', boundingBox: BoundingBox): Point | ||
export function createHexOrigin(input: Point): Point | ||
export function createHexOrigin(input: Point | 'topLeft', boundingBox?: BoundingBox): Point { | ||
if (isPoint(input)) return input | ||
|
||
if (!boundingBox) | ||
throw new TypeError( | ||
`Supply a bounding box ({ width: number, height: number }). Received: ${JSON.stringify(boundingBox)}`, | ||
) | ||
|
||
if (input === 'topLeft') return { x: boundingBox.width * -0.5, y: boundingBox.height * -0.5 } | ||
|
||
throw new TypeError( | ||
`Invalid origin: ${JSON.stringify( | ||
input, | ||
)}. Origin must be expressed as a Point ({ x: number, y: number }) or the string 'topLeft'.`, | ||
) | ||
} |
Oops, something went wrong.