这是另一个问题的分支,与Keith Randall的答案有关。请快速查看那里的图像,以了解下面的函数试图做什么。
简而言之,在2D网格上的任何两个点如果
简而言之,在2D网格上的任何两个点如果
x2!= x1
且y2!= y1
,则会有两个对角线交点。我实现了以下函数,但无法确定从哪个单元格减去delta,哪个单元格添加delta。因此,对于某些坐标对,结果是准确的,而对于其他坐标对,结果是相反的。// 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);
}
起初我以为这只是一个简单的渐变/斜率问题,但我似乎找不到slope
和+/- deltaHalf
组合之间的一致关联。
重要提示: 请注意,可接受的答案应仅进行x1、y1、x2、y2比较。由于性能惩罚,实际计算线的斜率不是一个选项。我们已经除以2了,不能再承担另一个。