2016-06-10 127 views
0

我試圖理解這個函數的功能。它是由我的老師給出的,我無法理解,發現x和y座標的公式背後的邏輯是什麼。從我的數學課我知道我的公式發現攔截,但它的混亂翻譯成代碼。所以我有一些問題,他們如何定義a,b,c的公式以及查找座標x和y。查找線和圓的交點

void Intersection::getIntersectionPoints(const Arc& arc, const Line& line) { 
    double a, b, c, mu, det; 

    std::pair<double, double> xPoints; 
    std::pair<double, double> yPoints; 
    std::pair<double, double> zPoints; 

//(m2+1)x2+2(mc−mq−p)x+(q2−r2+p2−2cq+c2)=0. 
    //a= m2; 
    //b= 2 * (mc - mq - p); 
    //c= q2−r2+p2−2cq+c2 
    a = pow((line.end().x - line.start().x), 2) + pow((line.end().y - line.start().y), 2) + pow((line.end().z - line.start().z), 2); 

    b = 2 * ((line.end().x - line.start().x)*(line.start().x - arc.center().x) 
    + (line.end().y - line.start().y)*(line.start().y - arc.center().y) 
    + (line.end().z - line.start().z)*(line.start().z - arc.center().z)); 

    c = pow((arc.center().x), 2) + pow((arc.center().y), 2) + 
    pow((arc.center().z), 2) + pow((line.start().x), 2) + 
    pow((line.start().y), 2) + pow((line.start().z), 2) - 
    2 * (arc.center().x * line.start().x + arc.center().y * line.start().y + 
    arc.center().z * line.start().z) - pow((arc.radius()), 2); 

    det = pow(b, 2) - 4 * a * c; 

    /* Tangenta na kružnicu */ 
    if (Math<double>::isEqual(det, 0.0, 0.00001)) { 
    if (!Math<double>::isEqual(a, 0.0, 0.00001)) 
     mu = -b/(2 * a); 
    else 
     mu = 0.0; 
//        x =  h   + t * (p   −  h) 
    xPoints.second = xPoints.first = line.start().x + mu * (line.end().x - line.start().x); 
    yPoints.second = yPoints.first = line.start().y + mu * (line.end().y - line.start().y); 
    zPoints.second = zPoints.first = line.start().z + mu * (line.end().z - line.start().z); 
    } 

    if (Math<double>::isGreater(det, 0.0, 0.00001)) { 
    // first intersection 
    mu = (-b - sqrt(pow(b, 2) - 4 * a * c))/(2 * a); 
    xPoints.first = line.start().x + mu * (line.end().x - line.start().x); 
    yPoints.first = line.start().y + mu * (line.end().y - line.start().y); 
    zPoints.first = line.start().z + mu * (line.end().z - line.start().z); 
    // second intersection 

    mu = (-b + sqrt(pow(b, 2) - 4 * a * c))/(2 * a); 
    xPoints.second = line.start().x + mu * (line.end().x - line.start().x); 
    yPoints.second = line.start().y + mu * (line.end().y - line.start().y); 
    zPoints.second = line.start().z + mu * (line.end().z - line.start().z); 
    } 

回答

0

表示的線的開始點作爲A,結束點作爲B,圓的爲C中心,圓的爲r半徑與交點爲P,那麼我們可以寫數p作爲

P=(1-t)*A + t*B = A+t*(B-A) (1) 

點普也將定位於圓,因此

|P-C|^2 = r^2     (2) 

堵方程(1)代入式(2),你會得到

|B-A|^2*t^2 + 2(B-A)\dot(A-C)*t +(|A-C|^2 - r^2) = 0 (3) 

這就是你如何在你發佈的程序中得到a,b和c的公式。在求解t之後,你將從方程(1)中獲得交點。由於方程(3)是二次方程,所以t可以得到0,1或2個值,它們對應於線可能不與圓相交的幾何配置,與圓正好相切或通過兩個位置處的圓。