Collision detection between a line and a circle in JavaScript

前端 未结 4 1589
故里飘歌
故里飘歌 2020-12-06 18:57

I\'m looking for a definitive answer, maybe a function cos I\'m slow, that will determine if a line segment and circle have collided, in javascript (working with canvas)

4条回答
  •  隐瞒了意图╮
    2020-12-06 19:42

    Matt DesLauriers published a Javascript library for this problem at https://www.npmjs.com/package/line-circle-collision. The API is straightforward:

    var circle = [5, 5],
        radius = 25,
        a = [5, 6],
        b = [10, 10]
    
    var hit = collide(a, b, circle, radius)
    

提交回复
热议问题