Пример #1
0
    private int[] GetRectangleIndices(List <UIVertex> vertices, int[] hexIndices)
    {
        if (hexIndices.Length != 6)
        {
            return(null);
        }
        var result = hexIndices.Distinct().ToArray();

        if (result.Length != System.Enum.GetValues(typeof(RectangleIndex)).Length)
        {
            return(null);
        }
        var upperLeftPosition  = new Vector2(float.MaxValue, float.MinValue);
        var lowerRightPosition = new Vector2(float.MinValue, float.MaxValue);

        foreach (var position in hexIndices.Select(x => vertices[x].position))
        {
            upperLeftPosition.x  = Mathf.Min(upperLeftPosition.x, position.x);
            upperLeftPosition.y  = Mathf.Max(upperLeftPosition.y, position.y);
            lowerRightPosition.x = Mathf.Max(lowerRightPosition.x, position.x);
            lowerRightPosition.y = Mathf.Min(lowerRightPosition.y, position.y);
        }
        foreach (var index in hexIndices)
        {
            var position       = vertices[index].position;
            var rectangleIndex = 0;
            if (position.x == upperLeftPosition.x)
            {
                rectangleIndex = 0x0;
            }
            else if (position.x == lowerRightPosition.x)
            {
                rectangleIndex = 0x1;
            }
            else
            {
                return(null);
            }
            if (position.y == upperLeftPosition.y)
            {
                rectangleIndex ^= 0x0;
            }
            else if (position.y == lowerRightPosition.y)
            {
                rectangleIndex ^= 0x3;
            }
            else
            {
                return(null);
            }
            result[rectangleIndex] = index;
        }
        return(result);
    }