以圆形方式放置瓷砖

时间:2014-05-19 12:10:30

标签: javascript jquery algorithm geometry tile

我需要以这种方式放置瓷砖:

enter image description here

我只需要逻辑,所以语言无所谓。谢谢你!

瓷砖数量未知!

这是我走了多远:

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++
});

3 个答案:

答案 0 :(得分:1)

你可以从起点像一只乌龟在80年代的乌龟图形中走一条螺旋线:直走一段距离,然后右转,走一点,右转等等。你不时要调整你的步行距离。

您可以走四个不同的方向。开始朝右,步长为一个单位。在您走过的每个单元后放置一块瓷砖。如果您已到达此步骤的目的地,请向右转。当您面向左或右时,将步长增加一个单位。

    var x = 350;
    var y = 350;
    var delta = 50;        // tile edge length

    var dir = 0;           // 0 to 3, clockwise: n, e, s, w
    var step = 0;          // current unit edge length of spiral
    var togo = 0;          // how long to go on this edge?
    var n = 49;

    var offset = [      // direction vectors for facing ...
        [0, -1],        // ... north
        [1, 0],         // ... east
        [0, 1],         // ... south
        [-1, 0]         // ... west
    ];

    for (var i = 0; i < n; i++) {
        place_tile(i + 1, x, y);

        if (togo == 0) {
            dir = (dir + 1) % 4;
            if (dir == 1 || dir == 3) step++;
            togo = step;
        }

        togo--;

        x += delta * offset[dir][0]; 
        y += delta * offset[dir][1];
    }

49的限制有点随意;它会产生一个7x7的平方。也许最好让break离开循环并在步骤达到某个值时停止平铺或者当你离开画布时停止平铺。

答案 1 :(得分:0)

也许这有助于一个开始:

假设您对现有瓷砖的位置一无所知:

“circleize”不断增长,从2x2开始,然后是4x4,然后是6x6瓦片,让我们称之为NxN。对于每个圆圈,你必须在每个方向上前进一次,从UP开始(因此ZERO将在1下方),然后向右,向下,向左。在切换到下一个方向之前,您必须在每个方向上采取N-1步。在完成圆圈之后,您必须更改UP以获得LEFT以开始新圆圈(意味着ZERO实际上将是2的位置)。

答案 2 :(得分:0)

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();
    }
}