Skip to content

Instantly share code, notes, and snippets.

@MaulingMonkey
Created January 2, 2012 04:43
Show Gist options
  • Save MaulingMonkey/1549377 to your computer and use it in GitHub Desktop.
Save MaulingMonkey/1549377 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Drawing;
namespace ProcessingTest {
class Program {
// N.B. KnownGood[-y,+x]
static int[,] KnownGoodData = new int[,]
{ { 36, 35, 34, 33, 32, 31, 30, 55 }
, { 37, 16, 15, 14, 13, 12, 29, 54 }
, { 38, 17, 4, 3, 2, 11, 28, 53 }
, { 39, 18, 5, 0, 1, 10, 27, 52 }
, { 40, 19, 6, 7, 8, 9, 26, 51 }
, { 41, 20, 21, 22, 23, 24, 25, 50 }
, { 42, 43, 44, 45, 46, 47, 48, 49 }
};
static int CenterX;
static int CenterY;
static Program() {
var w = KnownGoodData.GetLength(1);
var h = KnownGoodData.GetLength(0);
for ( int x=0 ; x<w ; ++x )
for ( int y=0 ; y<h ; ++y )
{
if ( KnownGoodData[y,x]==0 )
{
CenterX = x;
CenterY = y;
return;
}
}
Debug.Fail("Failed to find center");
}
static void AssertValueAt( int x, int y, int value )
{
Debug.Assert( KnownGoodData[ CenterY-y, CenterX+x ] == value );
}
static Point CalculatePositionOf( int n )
{
var ring = (int)Math.Ceiling( 0.5 * ( 1 + Math.Sqrt( n + 1 ) ) ) - 1;
var corner_value = ring * (ring+1) * 4;
var missing = corner_value-n;
var left = Math.Max(0,Math.Min(2*ring,missing-0*ring));
var up = Math.Max(0,Math.Min(2*ring,missing-2*ring));
var right = Math.Max(0,Math.Min(2*ring,missing-4*ring));
var down = Math.Max(0,Math.Min(2*ring,missing-6*ring));
Debug.Assert( missing <= 8*ring );
var x = +ring - left + right;
var y = -ring - down + up;
return new Point(x,y);
}
static void Main() {
for ( int i=0 ; i<=55 ; ++i )
{
var xy = CalculatePositionOf(i);
AssertValueAt(xy.X,xy.Y,i);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment