diff --git a/src/hex/functions/createHexPrototype.ts b/src/hex/functions/createHexPrototype.ts index 1403e36b..485113ab 100644 --- a/src/hex/functions/createHexPrototype.ts +++ b/src/hex/functions/createHexPrototype.ts @@ -1,9 +1,9 @@ import { isCartesian, isObject } from '../../utils' import { CartesianCoordinates, DefaultHexPrototype, Ellipse, Hex, HexSettings, Orientation, Rectangle } from '../types' import { height } from './height' +import { hexToPoint } from './hexToPoint' import { isFlat } from './isFlat' import { isPointy } from './isPointy' -import { createToPoint } from './toPoint' import { widthPointy } from './width' export interface HexPrototypeOptions { @@ -87,7 +87,6 @@ export const createHexPrototype = ( origin: { value: normalizeOrigin(prototype) }, offset: { value: assertOffset(prototype) }, }) as T - const toPoint = createToPoint(prototype) // todo: any property accessors that use `this` are pointless in a hex prototype return Object.defineProperties(prototype, { @@ -105,10 +104,9 @@ export const createHexPrototype = ( }, width: { value: widthPointy(prototype.dimensions.xRadius) }, - // toPoint() is created with a closure here for better performance, todo: check if it's better for performance toPoint: { value() { - return toPoint(this) + return hexToPoint(this) }, }, } as PropertyDescriptorMap & ThisType) as T diff --git a/src/hex/functions/hexToPoint.ts b/src/hex/functions/hexToPoint.ts new file mode 100644 index 00000000..5e6b17f1 --- /dev/null +++ b/src/hex/functions/hexToPoint.ts @@ -0,0 +1,12 @@ +import { Hex, Orientation, Point } from '../types' + +export const hexToPoint = ({ orientation, dimensions: { xRadius, yRadius }, q, r }: Hex) => + orientation === Orientation.POINTY + ? ({ + x: xRadius * Math.sqrt(3) * (q + r / 2), + y: ((yRadius * 3) / 2) * r, + } as Point) + : ({ + x: ((xRadius * 3) / 2) * q, + y: yRadius * Math.sqrt(3) * (r + q / 2), + } as Point) diff --git a/src/hex/functions/index.ts b/src/hex/functions/index.ts index 18b2056c..8e273c8c 100644 --- a/src/hex/functions/index.ts +++ b/src/hex/functions/index.ts @@ -2,7 +2,7 @@ export * from './corners' export * from './createHex' export * from './createHexPrototype' export * from './height' +export * from './hexToPoint' export * from './isFlat' export * from './isPointy' -export * from './toPoint' export * from './width' diff --git a/src/hex/functions/toPoint.ts b/src/hex/functions/toPoint.ts deleted file mode 100644 index 681e2c8e..00000000 --- a/src/hex/functions/toPoint.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { AxialCoordinates, Ellipse, HexSettings, Orientation, Point } from '../types' - -// todo: benchmark if currying the higher order functions has significant performance impact - -export const createToPointPointy = ({ xRadius, yRadius }: Ellipse) => ({ q, r }: AxialCoordinates) => - ({ - x: xRadius * Math.sqrt(3) * (q + r / 2), - y: ((yRadius * 3) / 2) * r, - } as Point) - -export const createToPointFlat = ({ xRadius, yRadius }: Ellipse) => ({ q, r }: AxialCoordinates) => - ({ - x: ((xRadius * 3) / 2) * q, - y: yRadius * Math.sqrt(3) * (r + q / 2), - } as Point) - -// todo: improve name? toPointFor()? -export const createToPoint = ({ orientation, dimensions }: HexSettings) => { - return orientation === Orientation.POINTY ? createToPointPointy(dimensions) : createToPointFlat(dimensions) -}