如何确定+/-号在二维空间中的两个点之间的计算对角线交点什么时候?对角线、什么时候、交点、两个

2023-09-05 00:45:55 作者:余生可不可以不忧伤

这是another问题并必须与基思·兰德尔的回答的问题。请不要有一个快速浏览一下图像那里看看下面的功能正在努力做的事情。

This is an offshoot of another question and has to do with Keith Randall's answer to the problem. Please do have a quick look at the image there to see what the function below is trying to do.

总之,在2D网格任意两点将有两个对角交叉点,如果 X2!= X1 Y2!= Y1 。我实现了以下功能,但无法弄清楚如何确定哪个单元从减去增量并添加到。其结果是,对于一些对坐标,结果准确,而对于其他人,他们是颠倒的。

In short, any two points on a 2D grid would have two diagonal intersections if x2 != x1 and y2 != y1. I implemented the following function but cannot figure out how to determine which cell to subtract delta from and which to add to. As a result, for some pair of coordinates, the results are accurate while for others they are reversed.

// This class is the same as [Point] except
// it uses BigInteger instead of Int32 types.
public class Cell
{
    System.Numerics.BigInteger X = 0;
    System.Numerics.BigInteger Y = 0;
}

public List<Cell> GetIntersections (Cell c1, Cell c2)
{
    List<Cell> cells = new List<Cell>();
    System.Numerics.BigInteger delta = 0;
    System.Numerics.BigInteger deltaHalf = 0;
    System.Numerics.BigInteger width = 0;
    System.Numerics.BigInteger height = 0;

    width = System.Numerics.BigInteger.Abs(c2.X - c1.X);
    height = System.Numerics.BigInteger.Abs(c2.Y - c1.Y);
    delta = System.Numerics.BigInteger.Abs(height - width);
    deltaHalf = System.Numerics.BigInteger.Divide(delta, 2);

    // INTRODUCE CONDITIONS HERE TO DETERMINE +/- COMBINATION.
    cells.Add(new Cell(c1.X - deltaHalf, c1.Y + deltaHalf));
    cells.Add(new Cell(c2.X + deltaHalf, c2.Y - deltaHalf));

    return (cells);
}

起初我还以为这是一个简单的梯度/斜率的问题,但我似乎无法找到的斜率 +/- deltaHalf一致的相关性的组合。

重要提示:请注意,可以接受的答案应该只做X1,Y1,X2,Y2进行比较。实际计算直线的斜率是不是由于性能损失的一个选项。我们对2已经做了分工,不能再次承受。

IMPORTANT: Please note that acceptable answers should only do x1, y1, x2, y2 comparisons. Actually calculating the slope of the line is not an option due to performance penalties. We are already doing a division by 2 and cannot afford another.

推荐答案

我知道答案了,而不必计算斜边某处简单比较。

I knew the answer was somewhere in simple comparisons instead of having to calculate hypotenuse.

public List<Cell> GetCellIntersections (Cell cell1, Cell cell2)
{
    Cell c1 = null;
    Cell c2 = null;
    List<Cell> cells = null;
    System.Numerics.BigInteger delta = 0;
    System.Numerics.BigInteger deltaHalf = 0;
    System.Numerics.BigInteger width = 0;
    System.Numerics.BigInteger height = 0;

    cells = new List<Cell>();

    // Sorting on y reduces conditions from 8 to 4.
    if (cell1.Y < cell2.Y)
    {
        c1 = cell1;
        c2 = cell2;
    }
    else
    {
        c1 = cell2;
        c2 = cell1;
    }

    if ((c1.X != c2.X) && (c1.Y != c2.Y))
    {
        width = System.Numerics.BigInteger.Abs(c2.X - c1.X);
        height = System.Numerics.BigInteger.Abs(c2.Y - c1.Y);
        delta = System.Numerics.BigInteger.Abs(height - width);
        deltaHalf = System.Numerics.BigInteger.Divide(delta, 2);

        if ((c1.X < c2.X) && (c1.Y < c2.Y))
        {
            if (width < height)
            {
                cells.Add(new Cell(this, c1.X - deltaHalf, c1.Y + deltaHalf));
                cells.Add(new Cell(this, c2.X + deltaHalf, c2.Y - deltaHalf));
            }
            else
            {
                cells.Add(new Cell(this, c1.X + deltaHalf, c1.Y - deltaHalf));
                cells.Add(new Cell(this, c2.X - deltaHalf, c2.Y + deltaHalf));
            }
        }
        else
        {
            if (width < height)
            {
                cells.Add(new Cell(this, c1.X + deltaHalf, c1.Y + deltaHalf));
                cells.Add(new Cell(this, c2.X - deltaHalf, c2.Y - deltaHalf));
            }
            else
            {
                cells.Add(new Cell(this, c1.X - deltaHalf, c1.Y - deltaHalf));
                cells.Add(new Cell(this, c2.X + deltaHalf, c2.Y + deltaHalf));
            }
        }
    }

    return (cells);
}