計算兩條線段是否相交

UE4的SegmentIntersection2D函數(shù):

    /**
     * Returns true if there is an intersection between the segment specified by SegmentStartA and SegmentEndA, and
     * the segment specified by SegmentStartB and SegmentEndB, in 2D space. If there is an intersection, the point is placed in out_IntersectionPoint
     * @param SegmentStartA - start point of first segment
     * @param SegmentEndA   - end point of first segment
     * @param SegmentStartB - start point of second segment
     * @param SegmentEndB   - end point of second segment
     * @param out_IntersectionPoint - out var for the intersection point (if any)
     * @return true if intersection occurred
     */
bool FMath::SegmentIntersection2D(const FVector& SegmentStartA, const FVector& SegmentEndA, const FVector& SegmentStartB, const FVector& SegmentEndB, FVector& out_IntersectionPoint)
{
    const FVector VectorA = SegmentEndA - SegmentStartA;
    const FVector VectorB = SegmentEndB - SegmentStartB;

    const float S = (-VectorA.Y * (SegmentStartA.X - SegmentStartB.X) + VectorA.X * (SegmentStartA.Y - SegmentStartB.Y)) / (-VectorB.X * VectorA.Y + VectorA.X * VectorB.Y);
    const float T = (VectorB.X * (SegmentStartA.Y - SegmentStartB.Y) - VectorB.Y * (SegmentStartA.X - SegmentStartB.X)) / (-VectorB.X * VectorA.Y + VectorA.X * VectorB.Y);

    const bool bIntersects = (S >= 0 && S <= 1 && T >= 0 && T <= 1);

    if (bIntersects)
    {
        out_IntersectionPoint.X = SegmentStartA.X + (T * VectorA.X);
        out_IntersectionPoint.Y = SegmentStartA.Y + (T * VectorA.Y);
        out_IntersectionPoint.Z = SegmentStartA.Z + (T * VectorA.Z);
    }

    return bIntersects;
}
兩線相交.jpg
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • UE4是我到目前為止,接觸過的最為復雜的技術,無論是深度還是廣度。我們都喜歡簡單,做人和做事都是一樣,如果可以簡單...
    微巖閱讀 5,033評論 1 3
  • 官方 官方文檔 中文版 http://api.unrealengine.com/CHN/ 英文版 https://...
    MogooStudio閱讀 4,614評論 0 2
  • 簡介 現(xiàn)今,網上有大量優(yōu)秀的資源供游戲開發(fā)者學習如何使用UE4內置的AI功能。不幸的是,這些資料中,有許多已經過時...
    閃電的藍熊貓閱讀 17,726評論 0 9
  • 今天是落地實修的最后一天,看著每天大群里每天到點交作業(yè)、發(fā)作業(yè)的日子要結束,感覺還有點舍不得。 十五天中,對自己最...
    olivia_1閱讀 257評論 0 0
  • 1、根據(jù)list集合某個字段升序或者降序(//根據(jù)SumAverage字段升序排序) 2、集合list根據(jù)根據(jù)某個...
    Aluha_f289閱讀 685評論 0 0

友情鏈接更多精彩內容