Search code examples
javascriptjqueryalgorithmgeometrytile

Place tiles in circular fashion


I need to place tiles in this fashion:

enter image description here

I only need the logic, so language doesn't matter.Thank you!

The number of tiles is unknown!

This is how far I got:

var setX = 0;
var setY = 0;
var count = 0;
$(".pic").each(function() 
{ 
   // set setX and setX based on count 
   $(this).animate({"left":setX,"top":setY},2000) 
   count++
});

Solution

  • Some implementation in C#:

    using System;
    using System.Collections.Generic;
    using System.Linq;                  
    
    public class Program
    {
    
        public struct Pair
        {
            int X;
            int Y;
            public Pair( int X, int Y )
            {
                this.X = X;
                this.Y = Y;
            }
            public override string ToString()
            {
                return String.Format( "({0},{1})", X, Y );
            }
        }
    
        public static IEnumerable<Pair> Positions( int StartX, int StartY )
        {
            int[] DeltaX = { 0, 1, 0,-1 };
            int[] DeltaY = { 1, 0,-1, 0 };
    
            int Direction = 1;
    
            int X = StartX;
            int Y = StartY;
    
            int Left = StartX;
            int Right = StartX;
            int Top = StartY;
            int Bottom = StartY;
    
            yield return new Pair(X, Y);
    
            while ( true )
            {
                switch ( Direction )
                {
                case 0:
                    Top += 1;
                    while ( Y < Top )
                    {
                        X += DeltaX[Direction];
                        Y += DeltaY[Direction];
                        yield return new Pair( X, Y );
                    }
                    break;
                case 1:
                    Right += 1;
                    while ( X < Right )
                    {
                        X += DeltaX[Direction];
                        Y += DeltaY[Direction];
                        yield return new Pair( X, Y );
                    }
                    break;
                case 2:
                    Bottom -= 1;
                    while ( Y > Bottom )
                    {
                        X += DeltaX[Direction];
                        Y += DeltaY[Direction];
                        yield return new Pair( X, Y );
                    }
                    break;
                case 3:
                    Left -= 1;
                    while ( X > Left )
                    {
                        X += DeltaX[Direction];
                        Y += DeltaY[Direction];
                        yield return new Pair( X, Y );
                    }
                    break;
                default:
                    break;
                }
    
                Direction++;
                Direction %= 4;
            }
        }
    
        public static void Main()
        {
            var Coords = Positions( 10, 10 ).Take( 15 );
            foreach ( Pair iPair in Coords )
            {
                Console.WriteLine( iPair.ToString() );
            }
            Console.ReadKey();
        }
    }