为什么我的Bresenhams线算法会破坏我的程序?

时间:2015-12-14 10:19:34

标签: c# algorithm unity3d rasterizing

我一直在尝试创建一个保留顺序的Bresenhams线算法,以便用户能够在运行时绘制网格。

它在大多数情况下起作用,然而,它似乎陷入了一个while循环并偶尔崩溃了我的程序。我相信这是鼠标快速移动的时候。

public static IEnumerable<WorldTile> GetWorldTilesOnLine(int x0, int y0, int x1, int y1)
{
    int dy = (int)(y1-y0);
    int dx = (int)(x1-x0);
    int xstep = 1;
    int ystep = 1;

    if (dy < 0) {dy = -dy; xstep = -1;}
    else {ystep = 1;}
    if (dx < 0) {dx = -dx; ystep = -1;}
    else {xstep = 1;}
    dy <<= 1;
    dx <<= 1;

    float fraction = 0;
    //Debug.Log (xstep);

    if (x0 >= 0 && x0 < worldBoard.GetLength(0) && y0 >= 0 && y0 <  worldBoard.GetLength(1))
    {
        yield return worldBoard[x0, y0];
    }

    if (dx > dy) {
        fraction = dy - (dx >> 1);

        while (Mathf.Abs(x0 - x1) > 1) { // This seems to be where the crash occurs

            if (fraction >= 0) {
                y0 += ystep;
                fraction -= dx;
            }
            x0 += xstep;
            fraction += dy;
            if (x0 >= 0 && x0 < worldBoard.GetLength(0) && y0 >= 0 && y0 < worldBoard.GetLength(1))
            {
                yield return worldBoard[x0, y0];
            }
        }
    }
    else {
        fraction = dx - (dy >> 1);

        while (Mathf.Abs(y0 - y1) > 1) { // This seems to be where the crash occurs

            if (fraction >= 0) {
                x0 += xstep;
                fraction -= dy;
            }
            y0 += ystep;
            fraction += dx;
            if (x0 >= 0 && x0 < worldBoard.GetLength(0) && y0 >= 0 && y0 < worldBoard.GetLength(1))
            {
                yield return worldBoard[x0, y0];
            }
        }
    }
    yield break;
}

当用户使用此

按下鼠标按钮时,将调用此方法
IEnumerator Draw()
{
    startPos =  WorldGridUtilities.getNearestWorldTileArrayValue(getMousePosition());
    worldTilesToAdd = new List<WorldTile>();
    Debug.Log (worldTilesToAdd.Count);
    while (true)
    {       
        worldTilesToAdd.Clear();
        nextPos = WorldGridUtilities.getNearestWorldTileArrayValue(getMousePosition());

        if (nextPos != startPos)
        {

            foreach (WorldTile wt in WorldGridUtilities.GetWorldTilesOnLine((int)startPos.x,(int)startPos.y, (int)nextPos.x, (int)nextPos.y))
            {
                worldTilesToAdd.Add (wt);

            startPos = nextPos;
        }

        foreach (WorldTile wt in worldTilesToAdd)
        {
            //Debug.Log ("coordinates added to list used by vectorline: " + wt.gridCoordinates);
            vectorLine.points3.Add(wt.gridCoordinates);
            vectorLine.Draw3D();
        }


        yield return new WaitForEndOfFrame();
    }
}

1 个答案:

答案 0 :(得分:1)

xstep ystep中有一些奇怪的东西。 如果dy > 0dx < 0xstep = 1ystep = 1。 因此x1 < x0 dx < 0因为xstep =1,但您继续使用brushBotton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { pen = new PenTool(mainDrawArea); mainDrawArea.addMouseListener(pen); mainDrawArea.addMouseMotionListener(pen); } }); rectangleButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { shapeToolbar.setVisible(false); rect = new RectangleTool(mainDrawArea); rect.setStrokeSize(strokeInt); mainDrawArea.addMouseListener(rect); mainDrawArea.addMouseMotionListener(rect); } }); 增加x0。这意味着无限循环。可能有些东西超载,有时你会收到错误。