C#的Random Walker循环问题

时间:2017-05-25 19:40:49

标签: c# loops random stack drawing

我正在研究一个简单的Random Walker程序,该程序应绘制一条10p长的线,然后选择一个随机的基本方向来绘制另一条线(也是10p长),直到达到一定数量的线。

我使用四个坐标绘制线条(X的两个坐标和Y的两个坐标)。在绘制每条线后,Y坐标被推入堆栈,并将它们作为X坐标弹出。这应确保每隔一行的起点是前一行的终点。

程序在按下Button控制器后绘制Windows窗体。截至目前,输出是这样的:

Output of program...:-(

这是按钮的事件处理程序代码块:

// Graphics and Pen classes instantiated

        Graphics graphics;
        graphics = this.CreateGraphics();

        Pen pen = new Pen(Color.Black);
        pen.Width = 1;

        // lineLength is 10 pixels
        // gridLength and gridWidth are needed to keep the Random Path inside a 600×600 field (this is not yet implemented in the code)
        // lineCount is for maximizing the number of lines to be drawn and to control the loop

        int lineLength = 10;
        int gridWidth = 600;
        int gridLength = 600;
        int lineCount = 0;

        // Starting line drawn with the following coordinates:
        int x1 = 20;
        int x2 = 20;
        int y1 = 20;
        int y2 = 30;

        graphics.DrawLine(pen, x1, y1, x2, y2);
        lineCount++;

        // Stack initialized to store "y" coordinates
        // "y" coordinates should be passed on as "x" coordinates for every consecutive lines
        // so that the ending point's coordinate of a line 

        Stack<int> stackY = new Stack<int>();
        stackY.Push(y2);
        stackY.Push(y1);

        for (lineCount = 1; lineCount <= 64; lineCount++)
        {
            // X pops current Y coordinates from stack
            x1 = stackY.Pop();
            x2 = stackY.Pop();

            // Initializing the random number (between 1 and 4) generator to choose from the cardinal directions
            Random rnd = new Random();
            int dir = rnd.Next(1, 5);

            switch (dir)
            {
                // up
                case 1:
                    y1 = y1 + lineLength;   // y1 plus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2); //drawing the line
                    stackY.Push(y2);    // pushing the current y coordinates into the stack
                    stackY.Push(y1);
                    break;

                // right
                case 2:
                    y1 = y2 + lineLength; // y2 plus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                    stackY.Push(y2);
                    stackY.Push(y1);
                    break;

                // down
                case 3:
                    y1 = y1 - lineLength; // y1 minus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                    stackY.Push(y2);
                    stackY.Push(y1);
                    break;

                // left
                case 4:
                    y2 = y2 - lineLength; // y2 minus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                    stackY.Push(y2);
                    stackY.Push(y1);
                    break;

            } //switch
        } //for     

    } //event handler

我不确定出了什么问题 - 我很欣赏任何单挑和建议!谢谢!

1 个答案:

答案 0 :(得分:2)

你使这比你需要的要复杂得多。你也正在以一种没有意义的方式混合x和y坐标。

您不需要堆叠,只需存储最近的积分。这样的事情。

int x = 20, y = 20;
int new_x = x, new_y = y;
Random rnd = new Random();

for (int i = 0; i < numLines; i++)
{
   int dir = rnd.Next(1, 5);
   if (dir == 1) new_x += lineLength;
   if (dir == 2) new_x -= lineLength;
   if (dir == 3) new_y += lineLength;
   if (dir == 4) new_y -= lineLength;

   graphics.DrawLine(pen, x, y, new_x, new_y);

   x = new_x;
   y = new_y;    
}

此外,您不需要每次都重新声明Random对象,只需在循环之前重新启动一次。