Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created April 4, 2024 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/c056ebdca33814ef4cb4b71d080c779c to your computer and use it in GitHub Desktop.
Save JadenGeller/c056ebdca33814ef4cb4b71d080c779c to your computer and use it in GitHub Desktop.
func triangularMatrixIndex(row: Int) -> Int {
row * (row + 1) / 2
}
func triangularMatrixCoordinate(_ index: Int) -> (row: Int, column: Int) {
let row = Int(sqrt(2 * Float32(index) + 0.25) - 0.5)
let column = index - triangularMatrixIndex(row: row)
return (row: row, column: column)
}
@JadenGeller
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment