出处: https://answers.unity.com/questions/366802/get-intersection-of-a-line-and-a-circle.html

测试脚本(返回值为交点数量):

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class LineCircleIntersect : MonoBehaviour
  6. {
  7. public Transform a;
  8. public Transform b;
  9.  
  10. public Transform circleCenter;
  11. public float radius;
  12.  
  13. void OnDrawGizmos()
  14. {
  15. if (a == null || b == null || circleCenter == null) return;
  16.  
  17. var intersect1 = default(Vector2);
  18. var intersect2 = default(Vector2);
  19. var intersectCount = BetweenLineAndCircle(circleCenter.position, radius, a.position, b.position, out intersect1, out intersect2);
  20.  
  21. if (intersectCount > )
  22. Gizmos.DrawWireSphere(intersect1, 0.1f);
  23.  
  24. if (intersectCount > )
  25. Gizmos.DrawWireSphere(intersect2, 0.1f);
  26.  
  27. Gizmos.DrawLine(a.position, b.position);
  28. Gizmos.DrawWireSphere(circleCenter.position, radius);
  29. }
  30.  
  31. int BetweenLineAndCircle(
  32. Vector2 circleCenter, float circleRadius,
  33. Vector2 point1, Vector2 point2,
  34. out Vector2 intersection1, out Vector2 intersection2)
  35. {
  36. float t;
  37.  
  38. var dx = point2.x - point1.x;
  39. var dy = point2.y - point1.y;
  40.  
  41. var a = dx * dx + dy * dy;
  42. var b = * (dx * (point1.x - circleCenter.x) + dy * (point1.y - circleCenter.y));
  43. var c = (point1.x - circleCenter.x) * (point1.x - circleCenter.x) + (point1.y - circleCenter.y) * (point1.y - circleCenter.y) - circleRadius * circleRadius;
  44.  
  45. var determinate = b * b - * a * c;
  46. if ((a <= 0.0000001) || (determinate < -0.0000001))
  47. {
  48. // No real solutions.
  49. intersection1 = Vector2.zero;
  50. intersection2 = Vector2.zero;
  51. return ;
  52. }
  53. if (determinate < 0.0000001 && determinate > -0.0000001)
  54. {
  55. // One solution.
  56. t = -b / ( * a);
  57. intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);
  58. intersection2 = Vector2.zero;
  59. return ;
  60. }
  61.  
  62. // Two solutions.
  63. t = (float)((-b + Mathf.Sqrt(determinate)) / ( * a));
  64. intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);
  65. t = (float)((-b - Mathf.Sqrt(determinate)) / ( * a));
  66. intersection2 = new Vector2(point1.x + t * dx, point1.y + t * dy);
  67.  
  68. return ;
  69. }
  70. }

2D空间中求线段与圆的交点的更多相关文章

  1. 2D空间中求一点是否在多边形内

    参考自这篇博文:http://www.cnblogs.com/dabiaoge/p/4491540.html 一开始没仔细看做法,浪费了不少时间.下面是最终实现的效果: 大致流程: 1.随便选取多边形 ...

  2. 2D空间中求两圆的交点

    出处:https://stackoverflow.com/questions/19916880/sphere-sphere-intersection-c-3d-coordinates-of-colli ...

  3. [译]2D空间中使用四叉树Quadtree进行碰撞检测优化

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity2017.2.0f3 原文出处 : Quick Tip: Use Quadtrees to Detect Lik ...

  4. 2D空间中判断一点是否在三角形内

    要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...

  5. 2d游戏中求出一个向量的两个垂直向量

    function cc.exports.VerticalVector(vec)--求出两个垂直向量 local result = {} result[1] = cc.p(vec.y/vec.x,-1) ...

  6. HDU 3467 (求五个圆相交面积) Song of the Siren

    还没开始写题解我就已经内牛满面了,从晚饭搞到现在,WA得我都快哭了呢 题意: 在DotA中,你现在1V5,但是你的英雄有一个半径为r的眩晕技能,已知敌方五个英雄的坐标,问能否将该技能投放到一个合适的位 ...

  7. java求两个圆相交坐标

    最近由于项目需要,根据两个圆函数求出相交的坐标.实现代码如下,另感谢两圆求交点算法实现Java代码,虽然他所贡献的代码中存在问题,但仍有借鉴意义. 1.两个圆相交的数学求法 在中学数学中我们知道,一个 ...

  8. HDU 5572--An Easy Physics Problem(射线和圆的交点)

    An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  9. Gym - 101617F :Move Away (圆的交点)

    pro:给定N个圆,求离原点最远的点,满足它在N个圆里.输出这个距离.N<50; sol:关键点一定是圆与圆的交点. 圆与 圆心到原点的直线 的交点. 然后去验证这些关键点是否在N个圆内. 实际 ...

随机推荐

  1. PropertyUtils.copyProperties(); java.lang.NullPointerException可能产生的原因

    PropertyUtils.copyProperties(Object dest, Object orig); 出现空指针异常可能产生的原因(不一定准确):java.lang.NullPointerE ...

  2. UVa 562 - Dividing coins 均分钱币 【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/E 题目大意: 给你一堆硬币,让你分成两堆,分别给A,B两个人,求两人得到的最小差. 解题思路: 求解两人 ...

  3. P1799 数列_NOI导刊2010提高(06)

    P1799 数列_NOI导刊2010提高(06)f[i][j]表示前i个数删去j个数得到的最大价值.if(i-j==x) f[i][j]=max(f[i][j],f[i-1][j]+1); else ...

  4. odoo 模型继承

    在odoo中有两种模型的继承机制(传统方式和委托继承方式) 重点:在__manifest__.py中找到depends,加上要继承的模块 'depends': ['account'] 注意继承的模型所 ...

  5. mvc返回多个结果集,返回多个视图

    System.Web.Mvc.ViewPage<dynamic> public ActionResult Index()     {             IDictionary< ...

  6. 弗洛伊德算法Floyed(求各顶点间最短路径):可打印最短路径

    #include <iostream> #include <string> #include <iomanip> using namespace std; #def ...

  7. putty失活不挂起运行

    https://blog.csdn.net/c1481118216/article/details/53010963 以下方式是试过了https://www.cnblogs.com/mysqlplus ...

  8. mysql的密码忘记了怎么办

    我们的大脑不是电脑,有些东西难免会忘,但是会了这个再也不担心宝宝忘记密码了 (1)点击开始/控制面板/服务/mysql-->右击mysql看属性,里面有mysql的安装地址,然后找到安装地址进行 ...

  9. 喵哈哈村的魔法考试 Round 16 (Div.2) 比赛题解

    A 实际上我们for一遍就好. 坑点就是会爆int #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; ...

  10. oracle 语句 笔记

    1.查询某个表有多少列. select column_name from user_tab_columns where table_name = 'DQ_S1'; 列出所有的字段名. 2.查询昨天一天 ...