-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic cube representation types (#3)
* Basic cube representation types * Renaming non-react files to .ts from .tsx and upgrades static factory methods for index conversion to a factory object. * Adds SlotNumber and FaceletNumber type aliases
- Loading branch information
1 parent
95bb215
commit 6d6ad96
Showing
3 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CubeIndex } from "./cubeindex"; | ||
import { CubeOp } from "./cubeop"; | ||
|
||
export type FaceletNumber = number; | ||
|
||
export interface Cube { | ||
readonly dimension: number; | ||
readonly positionOf: (FaceletNumber: number) => CubeIndex; | ||
readonly occupantOf: (position: CubeIndex) => number; | ||
readonly apply: (op: CubeOp) => Cube; | ||
} |
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,36 @@ | ||
export type SlotNumber = number; | ||
|
||
export interface CubeIndex { | ||
readonly face: number; | ||
readonly row: number; | ||
readonly column: number; | ||
} | ||
|
||
export class IndexConverter { | ||
readonly dimension: number; | ||
|
||
static forDimension(dimension: number): IndexConverter { | ||
return new IndexConverter(dimension); | ||
} | ||
|
||
fromSlotNumber(slotNumber: SlotNumber): CubeIndex { | ||
const dimSquared = this.dimension ** 2; | ||
return { | ||
face: Math.trunc(slotNumber / dimSquared), | ||
row: Math.trunc((slotNumber % dimSquared) / this.dimension), | ||
column: slotNumber % this.dimension, | ||
}; | ||
} | ||
|
||
toSlotNumber(position: CubeIndex): SlotNumber { | ||
return ( | ||
this.dimension ** 2 * position.face + | ||
this.dimension * position.row + | ||
position.column | ||
); | ||
} | ||
|
||
private constructor(dimension: number) { | ||
this.dimension = dimension; | ||
} | ||
} |
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,6 @@ | ||
import { CubeIndex } from "./cubeindex"; | ||
|
||
export interface CubeOp { | ||
readonly imageOf: (cubeIndex: CubeIndex, dimension: number) => CubeIndex; | ||
readonly inverse: () => CubeOp; | ||
} |