這是another question的一個分支,並且與Keith Randall's answer有關。請快速查看下面的圖像,看看下面的功能正在嘗試做什麼。如何在計算二維空間中兩點之間的對角線交點時確定+/-符號?
簡而言之,如果是x2 != x1
和y2 != y1
,則2D網格上的任意兩點都將具有兩個對角線交點。我實現了以下功能,但無法弄清楚如何確定哪個單元從中減去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分區之內進行了分割,並且無法承受另一分割。
2分區只是一個移位,不是那些令人難以置信的便宜?你是如何描述這個的? – djechlin 2012-08-07 20:48:29
嘿拉希爾,你能給我提供一些反饋嗎?謝謝! – 2012-08-07 21:59:09
@AndreCalil:我已經回覆並且添加了我最終使用的代碼,只需要一個除以2。 – 2012-08-08 19:58:28