当前位置: 首页 > article >正文

碰撞算法8 --直线与圆的碰撞

--线与圆的碰撞
function LineToCircle(linePoint1,linePoint2,circlePos,radius)
    --这里需要分成两种情况,一种是线段两边的点是否在圆内
    --如果点到半径的距离
    if PointToCircle(linePoint1,circlePos,radius) then
        return true
    end

    if PointToCircle(linePoint2,circlePos,radius) then
        return true
    end

    --寻找圆心最近的点
    local lineVec = {x= (linePoint2.x - linePoint1.x) , y = (linePoint2.y - linePoint1.y)}
    local pointToLinePoint1 = {x= (circlePos.x - linePoint1.x) , y = (circlePos.y - linePoint1.y)}

    local lineLength = math.sqrt(lineVec.x * lineVec.x  + lineVec.y * lineVec.y)
    --点乘向量公式 相当于 lineLength * pointLinePoint1Length * cos角度
    local dot = lineVec.x  * pointToLinePoint1.x +  lineVec.y * pointToLinePoint1.y
    --这里就是pointLinePoint1Length * cos角度 及为lineVec 的投影长度,温馨提示pointLinePoint1ToLine可能为正负值 取决于向量的夹角的cos值
    --相当于到线段的投影比例
    local pointLinePoint1ToLine =  dot / lineLength
    local closestX = linePoint1.x + pointLinePoint1ToLine * (linePoint2.x - linePoint1.x)
    local closestY = linePoint1.y + pointLinePoint1ToLine * (linePoint2.y - linePoint1.y)

    local isInLine = PointToLine({ x = closestX,y =  closestY} , linePoint1, linePoint2)
    --如果点不在线上面(就是相当于他是在延长线 ) 直接returnfalse
    if not isInLine then
        return false;
    end
    --点跟圆的碰撞
    local result = PointToCircle({ x = closestX,y =  closestY},circlePos,radius)
    return result
end



--点与直线的碰撞
function PointToLine(point , linePoint1, linePoint2)
    --思想点与线两边线段的距离跟线的距离的判断,相等就是在同一直线
    local line1 = math.sqrt((linePoint1.x - point.x) * (linePoint1.x - point.x) + (linePoint1.y - point.y)* (linePoint1.y - point.y))
    local line2 = math.sqrt((linePoint2.x - point.x) * (linePoint2.x - point.x) + (linePoint2.y - point.y)* (linePoint2.y - point.y))
    local line = math.sqrt((linePoint2.x - linePoint1.x) * (linePoint2.x - linePoint1.x) + (linePoint2.y - linePoint1.y)* (linePoint2.y - linePoint1.y))
    return (line1 + line2) == line
end




--点跟圆是否碰撞
function PointToCircle (point,circlePos,radius)
    local sqr2  = (point.x - circlePos.x) * (point.x - circlePos.x) + (point.y - circlePos.y) * (point.y - circlePos.y)
    return radius * radius >= sqr2
end

http://www.kler.cn/a/429683.html

相关文章:

  • LabVIEW智能水肥一体灌溉控制系统
  • SimpleFOC01|基于STM32F103+CubeMX,移植核心的common代码
  • GitLab本地服务器配置ssh和克隆项目
  • 八股学习 Redis
  • 解决:ubuntu22.04中IsaacGymEnv保存视频报错的问题
  • 使用Docker模拟PX4固件的无人机用于辅助地面站开发
  • UART+DDR3+HDMI联合图像存储与显示系统
  • git 过滤检出包含windows平台不兼容文件
  • FSC认证是什么?FSC认证费用
  • Elasticsearch一分钟
  • 如何借助 LLM Gateway (LLM网关)同时接入多款 AI 大模型?
  • 2024年深圳杯数学建模C题编译器版本的识别问题解题全过程文档及程序
  • C语言 字符数组/多维数组/函数/作用域
  • 【MySQL 进阶之路】存储引擎和SQL优化技巧分析
  • 力扣刷题TOP101: 24.BM30 二叉搜索树与双向链表
  • STELLA软件入门:应用STELLA软件建立系统动态模型的过程;STELLA软件安装、界面及功能讲解等;在农业、生态及环境等科学领域应用
  • 模拟退火算法
  • 计算机网络练习题
  • Python教程104:生成26个英文字母有哪些方法?
  • LEED认证是什么?LEED认证银级和金级之间的区别在哪里
  • Agent AI: Surveying the Horizons of Multimodal Interaction---医疗保健、视频音频、多模态
  • python学习笔记—4—数据类型与数据类型转换
  • Linux上的C语言编程实践
  • JVM(Java虚拟机)类加载子系统是Java运行时环境的重要组成部分
  • 【opencv入门教程】14. 矩阵乘除运算
  • 企业防盗版:SPN安全上网解决方案,您的智能防护盾