【问题标题】:Seeded random plotting 2d grid种子随机绘制二维网格
【发布时间】:2013-01-24 07:42:33
【问题描述】:

这已经困扰了我几个小时,所以我想知道是否有人可以帮助我,因为我可能会以错误的方式思考这个问题。

我希望能够从具有无限宽度和高度的网格上的一组 x 和 y 坐标中获取布尔值。还有其他约束,沿 x 轴需要在两个真值之间至少有 n 个位置,我还需要知道从 0,0 到 x,y 区域中真值的数量。

给 getTrueCoordinatesInArea 的区域的宽度和高度等于 x 和 y,因为它的区域是从 0,0 到 x,y 创建的

如果有道理的话..

例如:

value = coordinateContainsTrue( x, y );//return true or false.
total = getTrueCoordinatesInArea( x , y );//Returns the total true values inside the area.

编辑:这可以解决种子问题。

【问题讨论】:

  • 没错,我已经编辑了问题。
  • 听起来您在这里几乎有 2 个问题 - 一个是在给定一些约束的情况下生成随机点,另一个是从位于指定区域内的一组坐标中查找坐标数(从 (0, 0))。那个听起来是对的吗?你在第二段有点迷失了我。

标签: c# algorithm math


【解决方案1】:

我不完全了解您需要什么,但我认为这听起来是一个很好且有趣的练习。我希望这是你想要的。 =) 它没有完全按照我想要的方式编码。宁愿使用 bool[,] 数组,但不知道如何使该数组动态化,也不想为此编写自己的类,但也许我应该这样做。

但是,这个解决方案应该可以工作。

private List<List<bool>> grid = new List<List<bool>>();

private int N = 4;

Random randomNumberGenerator = new Random();

private bool getRandomBoolean()
{
    return this.randomNumberGenerator.Next(0, 2) == 1;
}

private void testProgram()
{
    var containsTrue = coordinateContainsTrue(0, 10);
    var total = getTrueCoordinatesInArea(14, 2);
    var isTrue = coordinateIsTrue(15, 11);
}

private bool coordinateIsTrue(int x, int y)
{
    expandGridOfNeeded(x, y);

    return grid[x][y];
}

private bool coordinateContainsTrue(int x, int y)
{
    expandGridOfNeeded(x, y);

    for (int xTemp = 0; xTemp < x; xTemp++) // Loop columns
    {
        for (int yTemp = 0; yTemp < y; yTemp++) // Loop rows
        {
            if (grid[xTemp][yTemp])
                return true; // Return true if any true was found
        }
    }

    return false;
}

private int getTrueCoordinatesInArea(int x, int y)
{
    expandGridOfNeeded(x, y);

    var total = 0;

    for (int xTemp = 0; xTemp < x; xTemp++) // Loop columns
    {
        for (int yTemp = 0; yTemp < y; yTemp++) // Loop rows
        {
            if (grid[xTemp][yTemp])
                total++; // Increase count if true was found
        }
    }

    return total;
}

private void expandGridOfNeeded(int x, int y)
{
    if (x < 0 || y < 0)
        return;

    // Add needed columns
    while (grid.Count <= x)
    {
        var column = new List<bool>();

        // Only add rows if the others have rows
        if (grid.Count > 0)
        {
            while (column.Count < grid[0].Count)
            {
                // Check if legal to add a true value, if it is then add random else add false
                bool forceFalseValue = false;
                for (int i = grid.Count - 1; i > grid.Count + N && forceFalseValue == false; i--)
                {
                    forceFalseValue = grid[i][column.Count - 1];
                }

                column.Add(forceFalseValue ? false : getRandomBoolean());
            }
        }

        grid.Add(column);
    }

    // Add needed rows
    while (grid[0].Count <= y)
    {
        var counter = N;

        foreach (var column in grid)
        {
            // Check if legal to add a true value, if it is then add random else add false
            if (counter < N)
            {
                column.Add(false);
                counter++;
            }
            else
            {
                var randomValue = getRandomBoolean();

                if (randomValue)
                    counter = 0;
                else
                    counter++;

                column.Add(randomValue);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多