http://i.imgur.com/3OAAH.jpg三角 - 查找其中兩個曲線相交
我畫了這個驚人的圖來證明什麼,我試圖來計算。如果你不知道,這是城堡的牆壁和塔樓。爲了「關閉」城堡,我需要找出兩條紅線相交的地方,以便可以將兩個牆壁連接在一起。
爲了這個問題,牆壁的大小是固定的,所以三個邊長都是已知的。這意味着您可以使用餘弦定律找出靜態牆壁上的一個旋轉牆的角度。但是,我試圖實現它,但我無法正常工作。
下面的代碼:
function FindIntersection(vector Tower1, vector Tower2, float Wall1, float Wall2, out vector Point)
{
local float S1; // Length of side between the two towers.
local float S2; // Length of the first wall.
local float S3; // Length of the second wall.
local float CosA, A; // Stores the angle between S1 and S2.
local vector Vec, Vec2;
Tower1.Z = 0; // Make sure the towers are level.
Tower2.Z = 0;
S1 = VSize(Tower2 - Tower1); // Get the first side length.
S2 = Wall1; // Get the second side length.
S3 = Wall2; // Get the third side length.
`log("---------- S1: " $ S1 $ " S2: " $ S2 $ " S3: " $ S3 $ " -----------");
// Perform cosine law to get angle between S1 and S2.
CosA = (Sq(S2) + Sq(S1) - Sq(S3))/(2 * S2 * S1);
A = ACos(CosA);
`log("--------------------- " $ A*57.2957795131 $ " ---------------------");
// Get a vector angle between up and S1.
Vec = Normal(Tower2-Tower1);
// Get a vector angle between S1 and S2.
Vec2.X = Cos(A);
Vec2.Y = Sin(A);
Vec2.Z = 0;
Vec2 = Normal(Vec2);
// Determine the location of the new tower.
Point = Tower1 + Normal(Vec+Vec2) * S2;
}
我幾乎可以肯定的是,輸入的是正確的。我知道我沒有考慮超過90度的角度,這可能是一個問題,但我真的不知道如何從這裏開始。感謝您的任何幫助!
我想你就更有可能獲得math.stackexchange你的回答,儘管你在代碼執行它的事實。 – joelmdev
它們不是直線,而是圓弧。這在計算上有很大的不同。 – ja72
你是對的,但這是無關緊要的。這是一個三角形,我只需要找出其中一個角落。 – Calneon