예제 #1
0
    public static List <List <Vector2Int> > RasterizeLineToGroups(Vector2Int from, Vector2Int to, DiagonalRasterizationType diagonalRasterizationType = DiagonalRasterizationType.GoStraight)
    {
        //Debug.Log("    RasterizeLine(from = " + from + ", to = " + to + ")");
        List <List <Vector2Int> > result = new List <List <Vector2Int> >();
        int   yMin    = Mathf.Min(from.y, to.y);
        int   yMax    = Mathf.Max(from.y, to.y);
        int   xMin    = Mathf.Min(from.x, to.x);
        int   xMax    = Mathf.Max(from.x, to.x);
        int   xSignal = (from.x < to.x) ? 1 : -1;
        int   ySignal = (from.y < to.y) ? 1 : -1;
        float yDelta  = yMax - yMin;
        float xDelta  = xMax - xMin;

        if (from == to)
        {
            result.Add(new List <Vector2Int>()
            {
                from
            });
        }
        else if (from.x == to.x)
        {
            for (int i = 0; i <= yDelta; i++)
            {
                result.Add(new List <Vector2Int>()
                {
                    new Vector2Int(from.x, yMin + i)
                });
            }
        }
        else if (from.y == to.y)
        {
            for (int i = 0; i <= xDelta; i++)
            {
                result.Add(new List <Vector2Int>()
                {
                    new Vector2Int(xMin + i, from.y)
                });
            }
        }
        else
        {
            if (xDelta == yDelta)
            {
                for (int i = 0; i <= xDelta; i++)
                {
                    Vector2Int        nextPoint = new Vector2Int(from.x + (i * xSignal), from.y + (i * ySignal));
                    List <Vector2Int> nextList  = new List <Vector2Int>();
                    if (result.Count > 0)
                    {
                        Vector2Int last = result.Last().Last();
                        switch (diagonalRasterizationType)
                        {
                        case DiagonalRasterizationType.GoThruOneDirectNeighbor:
                            nextList.Add(new Vector2Int(nextPoint.x, last.y));
                            break;

                        case DiagonalRasterizationType.GoThruAllDirectNeighbors:
                            //Debug.Log("        where xDelta==yDelta and diagonalRasterizationType==GoThruAllDirectNeighbors");
                            nextList.Add(new Vector2Int(nextPoint.x, last.y));
                            nextList.Add(new Vector2Int(last.x, nextPoint.y));
                            break;
                        }
                    }
                    nextList.Add(nextPoint);
                    result.Add(nextList);
                }
            }
            else if (xDelta > yDelta)
            {
                for (int i = 0; i <= xDelta; i++)
                {
                    int               curY      = Mathf.RoundToInt(i * yDelta / xDelta);
                    Vector2Int        nextPoint = new Vector2Int(from.x + (i * xSignal), from.y + (curY * ySignal));
                    List <Vector2Int> nextList  = new List <Vector2Int>();
                    if (result.Count > 0 && nextPoint.x != result.Last().Last().x&& nextPoint.y != result.Last().Last().y)
                    {
                        Vector2Int last = result.Last().Last();
                        switch (diagonalRasterizationType)
                        {
                        case DiagonalRasterizationType.GoThruOneDirectNeighbor:
                            nextList.Add(new Vector2Int(nextPoint.x, last.y));
                            break;

                        case DiagonalRasterizationType.GoThruAllDirectNeighbors:
                            //Debug.Log("        where xDelta>yDelta and diagonalRasterizationType==GoThruAllDirectNeighbors");
                            nextList.Add(new Vector2Int(nextPoint.x, last.y));
                            nextList.Add(new Vector2Int(last.x, nextPoint.y));
                            break;
                        }
                    }
                    nextList.Add(nextPoint);
                    result.Add(nextList);
                }
            }
            else
            {
                for (int i = 0; i <= yDelta; i++)
                {
                    int               curX      = Mathf.RoundToInt(i * xDelta / yDelta);
                    Vector2Int        nextPoint = new Vector2Int(from.x + (curX * xSignal), from.y + (i * ySignal));
                    List <Vector2Int> nextList  = new List <Vector2Int>();
                    if (result.Count > 0 && nextPoint.x != result.Last().Last().x&& nextPoint.y != result.Last().Last().y)
                    {
                        Vector2Int last = result.Last().Last();
                        switch (diagonalRasterizationType)
                        {
                        case DiagonalRasterizationType.GoThruOneDirectNeighbor:
                            nextList.Add(new Vector2Int(nextPoint.x, last.y));
                            break;

                        case DiagonalRasterizationType.GoThruAllDirectNeighbors:
                            //Debug.Log("        where xDelta<yDelta and diagonalRasterizationType==GoThruAllDirectNeighbors");
                            nextList.Add(new Vector2Int(nextPoint.x, last.y));
                            nextList.Add(new Vector2Int(last.x, nextPoint.y));
                            break;
                        }
                    }
                    nextList.Add(nextPoint);
                    result.Add(nextList);
                }
            }
        }
        //Debug.Log("        return = " + result + ":");
        //result.ForEach(p => Debug.Log("            " + p));
        return(result);
    }
예제 #2
0
 public static List <Vector2Int> RasterizeLine(Vector2Int from, Vector2Int to, DiagonalRasterizationType diagonalRasterizationType = DiagonalRasterizationType.GoStraight)
 {
     return(RasterizeLineToGroups(from, to, diagonalRasterizationType).ToSingleList());
 }