2012-06-14 84 views
0

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度的角度,這可能是一個問題,但我真的不知道如何從這裏開始。感謝您的任何幫助!

+0

我想你就更有可能獲得math.stackexchange你的回答,儘管你在代碼執行它的事實。 – joelmdev

+0

它們不是直線,而是圓弧。這在計算上有很大的不同。 – ja72

+0

你是對的,但這是無關緊要的。這是一個三角形,我只需要找出其中一個角落。 – Calneon

回答

0

你想要的是三角形邊角AS1,S2S3。您可以使用law of cosines獲得

A = ACOS((S1*S1+S2*S2-S3*S3)/(2*S1*S2)) 

的頂點的座標找到,如果你知道底壁(S1)的方向。

TH = ATAN((Tower2.Y-Tower1.Y)/(Tower2.X-Tower1.X)) 
Tower3.X = S2*COS(A+TH) 
Tower3.Y = S2*SIN(A+TH) 
+0

更好的是,使用'ATAN2()'函數代替'ATAN'。 – ja72

0

給這個自旋:

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 Vec1; 

    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("--------------------- Angle in degrees" $ (A* 180.f)/M_PI $ " ---------------------"); 

    // Get a vector angle between up and S1. 
    Vec1 = Normal(Tower2-Tower1); 

    // rotate the normalized vector around axis (0,1,0) (assuming Unreals co-ordinate system is x,y,z and Y is UP 
    local quat quatRot = QuatFromAxisAndAngle(Vect(0,0,1), -A); //angle accepted in radians NOTE: Could just be A instead of -A, depends on which way the point will rotate 

    local vector corectedS2Vector = Normal (QuatRotateVector (quatRot , Vec1)); 

    // Determine the location of the new tower. 
    Point = Tower1 + corectedS2Vector * S2; 
} 
+0

這工作出色,謝謝!我甚至不知道UDK有這些功能。 – Calneon

+1

@Calneon如果這對你有用,那麼你應該真的接受答案。 – mathematician1975