Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要帮助修改算法吗_C#_Algorithm - Fatal编程技术网

C# 需要帮助修改算法吗

C# 需要帮助修改算法吗,c#,algorithm,C#,Algorithm,我需要做一个扫描屏幕上每个像素的图像 我目前正在使用: public static void Spiral() { // starting point x = ((int)Math.Floor(Screen.PrimaryScreen.Bounds.Height / 2.0)) - 1; y = ((int)Math.Floor(Screen.PrimaryScreen.Bounds.Width / 2.0)) - 1; for (int j = Screen

我需要做一个扫描屏幕上每个像素的图像

我目前正在使用:

public static void Spiral()
{

    // starting point
    x = ((int)Math.Floor(Screen.PrimaryScreen.Bounds.Height / 2.0)) - 1;
    y = ((int)Math.Floor(Screen.PrimaryScreen.Bounds.Width / 2.0)) - 1;

    for (int j = Screen.PrimaryScreen.Bounds.Height; j <= 2; k--)
    {
        for (int k = 0; k < (k < (Screen.PrimaryScreen.Bounds.Height - 1) ? 2 : 3); j++)
        {
            for (int i = 0; i < s; i++)
            {
                c++;
            }
            d = (d + 1) % 2;
        }
        s = s + 1;
    }
}
publicstaticvoidspiral()
{
//起点
x=((int)Math.Floor(Screen.PrimaryScreen.Bounds.Height/2.0))-1;
y=((int)数学地板(Screen.PrimaryScreen.Bounds.Width/2.0))-1;

对于(int j=Screen.PrimaryScreen.Bounds.Height;j让我们将其转换为一个简单的点序列

我们知道我们正朝着四个方向之一前进

var steps = new (int dx, int dy)[] { (1, 0), (0, 1), (-1, 0), (0, -1) };
要创建螺旋,我们先向第一个方向移动一次,然后向第二个方向移动一次;然后向下两个方向移动两次,然后向下两个方向移动三次,然后向下两个方向移动四次,以此类推。如果我们到达列表的末尾,我们会循环回到起点。因此,如果我们从
n=0
开始,那么我们会重复每个方向
n/2+1
次(知道这是整数数学)

下面是我的
螺旋
生成器方法:

public IEnumerable<Point> Spiral(int x, int y)
{
    yield return new Point(x, y);
    var steps = new(int dx, int dy)[] { (1, 0), (0, 1), (-1, 0), (0, -1) };
    var i = 0;
    var n = 0;
    while (true)
    {
        for (var j = 0; j < n / 2 + 1; j++)
        {
            var (sx, sy) = steps[i];
            x += sx;
            y += sy;
            yield return new Point(x, y);
        }
        if (++i >= steps.Length)
            i = 0;
        n++;
    }
}
如果我们想使用您问题中给出的较小屏幕进行验证,则如下所示:

IEnumerable<Point> query =
    Spiral(0, 0)
        .Where(z => z.Y >= -1 && z.Y <= 1)
        .Where(z => z.X >= -2 && z.X <= 2)
        .Take(5 * 3);
IEnumerable查询=
螺旋线(0,0)
其中(z=>z.Y>=-1&&z.Y z.X>=-2&&z.X=步长长度)
i=0;
n++;
}
}
var w=Screen.PrimaryScreen.Bounds.Width;
var h=Screen.PrimaryScreen.Bounds.Height;
var l=Screen.PrimaryScreen.Bounds.Left;
var r=Screen.PrimaryScreen.Bounds.Right;
var t=Screen.PrimaryScreen.Bounds.Top;
var b=Screen.PrimaryScreen.Bounds.Bottom;
foreach(螺旋点中的点)(w/2,h/2)
其中(z=>z.X>=l和&z.Xz.Y>=t&&z.Y
那它在尺寸不均匀的情况下不起作用呢?对于那些好奇的人来说,。这似乎是一个非常好的问题,我正要发布答案。我不明白为什么它关闭了?我有一个答案等待发布。我只需要更多的用户投票才能重新打开。似乎你只需要在
Math.min中放置一些(你的合作伙伴,最大的合作伙伴)
(Y也是如此)在你的代码中。这样做,直到两个坐标都超出屏幕边界,你就完成了。-然后循环两个坐标,直到两个坐标都达到最大值。也许这会让你找到一条自己解决它的路径。你可以在方法中使用LINQ的东西,这会做你想做的。你可以很容易地用
替换元组-只要refactor与普通一样。对不起,你说的“点”是什么意思?你是说结构
?@Neoray?把它包括在问题中是个好主意。:-)@Neoray-我刚刚更新了答案以使用
。我希望你能看到我是如何重构它的?@Enigmativity我感谢你花时间帮助你,你的答案非常有用。我肯定会把它标记为最佳答案,因为它确实回答了我遇到的主要问题。
IEnumerable<Point> query =
    Spiral(0, 0)
        .Where(z => z.Y >= -1 && z.Y <= 1)
        .Where(z => z.X >= -2 && z.X <= 2)
        .Take(5 * 3);
public static void Spiral()
{
    IEnumerable<Point> SpiralPoints(int x, int y)
    {
        yield return new Point(x, y);
        var steps = new(int dx, int dy)[] { (1, 0), (0, 1), (-1, 0), (0, -1) };
        var i = 0;
        var n = 0;
        while (true)
        {
            for (var j = 0; j < n / 2 + 1; j++)
            {
                var (sx, sy) = steps[i];
                x += sx;
                y += sy;
                yield return new Point(x, y);
            }
            if (++i >= steps.Length)
                i = 0;
            n++;
        }
    }

    var w = Screen.PrimaryScreen.Bounds.Width;
    var h = Screen.PrimaryScreen.Bounds.Height;
    var l = Screen.PrimaryScreen.Bounds.Left;
    var r = Screen.PrimaryScreen.Bounds.Right;
    var t = Screen.PrimaryScreen.Bounds.Top;
    var b = Screen.PrimaryScreen.Bounds.Bottom;

    foreach (Point point in SpiralPoints(w / 2, h / 2)
        .Where(z => z.X >= l && z.X < r)
        .Where(z => z.Y >= t && z.Y < b)
        .Take(w * h))
    {
        /* Do Stuff With Each Point Here */
    }

}