Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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# 如何修改Bresenham';s线算法来保持秩序?_C#_Algorithm_Unity3d_Rasterizing - Fatal编程技术网

C# 如何修改Bresenham';s线算法来保持秩序?

C# 如何修改Bresenham';s线算法来保持秩序?,c#,algorithm,unity3d,rasterizing,C#,Algorithm,Unity3d,Rasterizing,我一直在努力用一只粘在网格上的鼠标画一条线。我用的是Bresenhams线算法 但是,我需要它来保持顺序,因此如果用户向左绘制,它将按该顺序给我点数 WorldTile是一个在网格上充当节点的类 WorldBoard是一组WorldTile private static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; } publ

我一直在努力用一只粘在网格上的鼠标画一条线。我用的是Bresenhams线算法

但是,我需要它来保持顺序,因此如果用户向左绘制,它将按该顺序给我点数

WorldTile是一个在网格上充当节点的类

WorldBoard是一组WorldTile

private static void Swap<T>(ref T lhs, ref T rhs) 
{
    T temp; 
    temp = lhs; 
    lhs = rhs; 
    rhs = temp; 
}

public static IEnumerable<WorldTile> GetWorldTilesOnLine(int x0, int y0, int x1, int y1)
{


    bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0);

    if (steep)
    { 
        Swap<int>(ref x0, ref y0); // find out how this works
        Swap<int>(ref x1, ref y1); 
    }

    if (x0 > x1)
    {
        Swap<int>(ref x0, ref x1); 
        Swap<int>(ref y0, ref y1);
    }


    int dx = (x1 - x0);
    int dy = Mathf.Abs(y1 - y0);
    int error = (dx / 2); 
    int ystep = (y0 < y1 ? 1 : -1);
    int y = y0;

    for (int x = x0; x <= x1; x++)
    {
        yield return worldBoard[(steep ? y : x), (steep ? x : y)];
        error = error - dy;
        if (error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
    yield break;
}
private静态无效交换(ref T lhs,ref T rhs)
{
温度;
温度=lhs;
lhs=rhs;
rhs=温度;
}
公共静态IEnumerable GetWorldTilesOnLine(int x0、int y0、int x1、int y1)
{
布尔陡峭=数学绝对值(y1-y0)>数学绝对值(x1-x0);
如果(陡峭)
{ 
交换(ref x0,ref y0);//了解这是如何工作的
交换(参考x1,参考y1);
}
如果(x0>x1)
{
交换(参考x0,参考x1);
互换(参考y0,参考y1);
}
int dx=(x1-x0);
int dy=数学绝对值(y1-y0);
int错误=(dx/2);
int-ystep=(y01){
如果(分数>=0){
y0+=ystep;
分数-=dx;
}
x0+=xstep;
分数+=dy;
如果(x0>=0&&x0=0&&y0>1);
而(数学绝对值(y0-y1)>1){
如果(分数>=0){
x0+=xstep;
分数-=dy;
}
y0+=ystep;
分数+=dx;
如果(x0>=0&&x0=0&&y0
谢谢


吉姆

在一位朋友的大力帮助下,这是最终的解决办法

WorldBoard是一组WorldTile

private static void Swap<T>(ref T lhs, ref T rhs) 
{
    T temp; 
    temp = lhs; 
    lhs = rhs; 
    rhs = temp; 
}

public static IEnumerable<WorldTile> GetWorldTilesOnLine(int x0, int y0, int x1, int y1)
{


    bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0);

    if (steep)
    { 
        Swap<int>(ref x0, ref y0); // find out how this works
        Swap<int>(ref x1, ref y1); 
    }

    if (x0 > x1)
    {
        Swap<int>(ref x0, ref x1); 
        Swap<int>(ref y0, ref y1);
    }


    int dx = (x1 - x0);
    int dy = Mathf.Abs(y1 - y0);
    int error = (dx / 2); 
    int ystep = (y0 < y1 ? 1 : -1);
    int y = y0;

    for (int x = x0; x <= x1; x++)
    {
        yield return worldBoard[(steep ? y : x), (steep ? x : y)];
        error = error - dy;
        if (error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
    yield break;
}
WorldTiles本质上是网格上的节点

private static void Swap<T>(ref T lhs, ref T rhs) 
{
    T temp; 
    temp = lhs; 
    lhs = rhs; 
    rhs = temp; 
}

public static IEnumerable<WorldTile> GetWorldTilesOnLine(int x0, int y0, int x1, int y1)
{
    bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0);

    if (steep)
    {
        Swap<int>(ref x0, ref y0);
        Swap<int>(ref x1, ref y1);
    }

    int dx = Mathf.Abs(x1 - x0);
    int dy = Mathf.Abs(y1 - y0);
    int error = (dx / 2);
    int ystep = (y0 < y1 ? 1 : -1);
    int xstep = (x0 < x1 ? 1 : -1);
    int y = y0;

    for (int x = x0; x != (x1 + xstep); x += xstep)
    {
        yield return worldBoard[(steep ? y : x), (steep ? x : y)];
        error = error - dy;
        if (error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
    yield break;
}
private静态无效交换(ref T lhs,ref T rhs)
{
温度;
温度=lhs;
lhs=rhs;
rhs=温度;
}
公共静态IEnumerable GetWorldTilesOnLine(int x0、int y0、int x1、int y1)
{
布尔陡峭=数学绝对值(y1-y0)>数学绝对值(x1-x0);
如果(陡峭)
{
互换(参考x0,参考y0);
交换(参考x1,参考y1);
}
int dx=Mathf.Abs(x1-x0);
int dy=数学绝对值(y1-y0);
int错误=(dx/2);
int-ystep=(y0
在朋友的大力帮助下,这里是最终的解决方案

WorldBoard是一组WorldTile

private static void Swap<T>(ref T lhs, ref T rhs) 
{
    T temp; 
    temp = lhs; 
    lhs = rhs; 
    rhs = temp; 
}

public static IEnumerable<WorldTile> GetWorldTilesOnLine(int x0, int y0, int x1, int y1)
{


    bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0);

    if (steep)
    { 
        Swap<int>(ref x0, ref y0); // find out how this works
        Swap<int>(ref x1, ref y1); 
    }

    if (x0 > x1)
    {
        Swap<int>(ref x0, ref x1); 
        Swap<int>(ref y0, ref y1);
    }


    int dx = (x1 - x0);
    int dy = Mathf.Abs(y1 - y0);
    int error = (dx / 2); 
    int ystep = (y0 < y1 ? 1 : -1);
    int y = y0;

    for (int x = x0; x <= x1; x++)
    {
        yield return worldBoard[(steep ? y : x), (steep ? x : y)];
        error = error - dy;
        if (error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
    yield break;
}
WorldTiles本质上是网格上的节点

private static void Swap<T>(ref T lhs, ref T rhs) 
{
    T temp; 
    temp = lhs; 
    lhs = rhs; 
    rhs = temp; 
}

public static IEnumerable<WorldTile> GetWorldTilesOnLine(int x0, int y0, int x1, int y1)
{
    bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0);

    if (steep)
    {
        Swap<int>(ref x0, ref y0);
        Swap<int>(ref x1, ref y1);
    }

    int dx = Mathf.Abs(x1 - x0);
    int dy = Mathf.Abs(y1 - y0);
    int error = (dx / 2);
    int ystep = (y0 < y1 ? 1 : -1);
    int xstep = (x0 < x1 ? 1 : -1);
    int y = y0;

    for (int x = x0; x != (x1 + xstep); x += xstep)
    {
        yield return worldBoard[(steep ? y : x), (steep ? x : y)];
        error = error - dy;
        if (error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
    yield break;
}
private静态无效交换(ref T lhs,ref T rhs)
{
温度;
温度=lhs;
lhs=rhs;
rhs=温度;
}
公共静态IEnumerable GetWorldTilesOnLine(int x0、int y0、int x1、int y1)
{
布尔陡峭=数学绝对值(y1-y0)>数学绝对值(x1-x0);
如果(陡峭)
{
互换(参考x0,参考y0);
交换(参考x1,参考y1);
}
int dx=Mathf.Abs(x1-x0);
int dy=数学绝对值(y1-y0);
int错误=(dx/2);
int-ystep=(y0
当x0大于x1时,修改此算法以从右向左绘制看起来相当直截了当,但如果有可用于调试的上下文,则更容易执行此操作。你试过了吗?我将把我失败的尝试添加到问题中。你必须更改
for
循环中的逻辑,使其在从右向左行驶时不使用
x++
,并且在直线陡峭时更改登录
ystep
。哦,我会让它减少x吗?如果您有任何代码剪贴画,这将有很大帮助。谢谢你是拥有代码片段的人:)--首先,你为什么不复制
for
循环,然后在复制过程中翻转方向,使其从
x1
x0
,然后添加逻辑,根据
x0
是否
x1
使用其中一个
for
循环。一旦这样做了,你就可以把它们重新组合成一个。修改这个算法,当x0大于x1时,从右向左绘制,看起来很简单,但是当你有一个可以用于调试的上下文时,这样做更容易。你试过了吗?我将把我失败的尝试添加到问题中。你必须更改
for
循环中的逻辑,使其在从右向左行驶时不使用
x++
,并且在直线陡峭时更改登录
ystep
。哦,我会让它减少x吗?如果您有任何代码剪贴画,这将有很大帮助。谢谢你是拥有代码片段的人:)--首先,你为什么不复制
for
循环,然后在复制过程中翻转方向,使其从
x1
x0
,然后添加逻辑,根据
x0
是否
x1
使用其中一个
for
循环。一旦成功了,你可以考虑把它们重新组合成一个。