Detection of Triangle Collision in 2D Space

后端 未结 3 1305
悲&欢浪女
悲&欢浪女 2020-12-31 15:28

How can I programmatically detect whether or not two triangles touch each other, given their vertices on a 2D coordinate plane? This includes touching points or edges, as we

3条回答
  •  迷失自我
    2020-12-31 15:52

    Use Line Line intersection

    https://www.topcoder.com/community/data-science/data-science-tutorials/geometry-concepts-line-intersection-and-its-applications/#line_line_intersection

    Also consider the possibility that some vertex might be touching one of the sides of the other triangle.

    http://www.blackpawn.com/texts/pointinpoly/default.html

    function SameSide(p1,p2, a,b)
        cp1 = CrossProduct(b-a, p1-a)
        cp2 = CrossProduct(b-a, p2-a)
        if DotProduct(cp1, cp2) >= 0 then return true
        else return false
    
    function PointInTriangle(p, a,b,c)
        if SameSide(p,a, b,c) and SameSide(p,b, a,c)
            and SameSide(p,c, a,b) then return true
        else return false
    

    Or look at this link and scroll down

    http://compsci.ca/v3/viewtopic.php?t=6034

提交回复
热议问题