Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Last active April 8, 2018 01:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save calebporzio/1e8c442a72e79a488f4d049c8dddab5a to your computer and use it in GitHub Desktop.
Save calebporzio/1e8c442a72e79a488f4d049c8dddab5a to your computer and use it in GitHub Desktop.
<?php
$number = 361527;
$bottomRoot = floor(sqrt($number));
// Skip even numbers because they aren't corners.
if ($bottomRoot % 2 == 0) {
$bottomRoot = $bottomRoot-1;
}
$bottomSquare = $bottomRoot * $bottomRoot;
$topSquare = ($bottomRoot + 2) * ($bottomRoot + 2);
$topRoot = sqrt($topSquare);
$verticalOffset = (($bottomRoot - 1) / 2) + 1;
$pointer = ($number - $bottomSquare);
$height = $topRoot - 1;
$middle = $height / 2;
$horizontalOffset = ($pointer % $height) - $middle;
return $horizontalOffset + $verticalOffset;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment