Skip to content

Commit

Permalink
feat(hex): rename createToPoint() to hexToPoint() and make it accept …
Browse files Browse the repository at this point in the history
…a hex

This was (also) done to improve performance and have a simpler interface
  • Loading branch information
flauwekeul committed Apr 22, 2021
1 parent 9a9b4a9 commit 4227737
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/hex/functions/createHexPrototype.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -87,7 +87,6 @@ export const createHexPrototype = <T extends DefaultHexPrototype>(
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, {
Expand All @@ -105,10 +104,9 @@ export const createHexPrototype = <T extends DefaultHexPrototype>(
},
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<T & Hex>) as T
Expand Down
12 changes: 12 additions & 0 deletions src/hex/functions/hexToPoint.ts
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/hex/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
20 changes: 0 additions & 20 deletions src/hex/functions/toPoint.ts

This file was deleted.

0 comments on commit 4227737

Please sign in to comment.