2D空间中求线段与圆的交点
出处: https://answers.unity.com/questions/366802/get-intersection-of-a-line-and-a-circle.html
测试脚本(返回值为交点数量):
using System.Collections; using System.Collections.Generic; using UnityEngine;public class LineCircleIntersect : MonoBehaviour {public Transform a;public Transform b;public Transform circleCenter;public float radius;void OnDrawGizmos(){if (a == null || b == null || circleCenter == null) return;var intersect1 = default(Vector2);var intersect2 = default(Vector2);var intersectCount = BetweenLineAndCircle(circleCenter.position, radius, a.position, b.position, out intersect1, out intersect2);if (intersectCount > 0)Gizmos.DrawWireSphere(intersect1, 0.1f);if (intersectCount > 1)Gizmos.DrawWireSphere(intersect2, 0.1f);Gizmos.DrawLine(a.position, b.position);Gizmos.DrawWireSphere(circleCenter.position, radius);}int BetweenLineAndCircle(Vector2 circleCenter, float circleRadius,Vector2 point1, Vector2 point2,out Vector2 intersection1, out Vector2 intersection2){float t;var dx = point2.x - point1.x;var dy = point2.y - point1.y;var a = dx * dx + dy * dy;var b = 2 * (dx * (point1.x - circleCenter.x) + dy * (point1.y - circleCenter.y));var c = (point1.x - circleCenter.x) * (point1.x - circleCenter.x) + (point1.y - circleCenter.y) * (point1.y - circleCenter.y) - circleRadius * circleRadius;var determinate = b * b - 4 * a * c;if ((a <= 0.0000001) || (determinate < -0.0000001)){// No real solutions.intersection1 = Vector2.zero;intersection2 = Vector2.zero;return 0;}if (determinate < 0.0000001 && determinate > -0.0000001){// One solution.t = -b / (2 * a);intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);intersection2 = Vector2.zero;return 1;}// Two solutions.t = (float)((-b + Mathf.Sqrt(determinate)) / (2 * a));intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);t = (float)((-b - Mathf.Sqrt(determinate)) / (2 * a));intersection2 = new Vector2(point1.x + t * dx, point1.y + t * dy);return 2;} }
2D空间中求线段与圆的交点相关推荐
- 计算几何——直线、线段和圆的交点
汇总篇:计算几何汇总 首先,求出圆心O在直线AB上的投影点E 然后求出AB的方向向量e=AB/|AB| 令EC=ED=base base = sqrt(r^2-OE^2) EC = -base *e ...
- python求交点坐标_Python求两个圆的交点坐标或三个圆的交点坐标方法
计算两个圆的交点 代码如下: # -*- coding: utf-8 -*- import math import numpy as np def insec(p1,r1,p2,r2): x = p1 ...
- 计算空间中两线段之间的距离
最近在建立气凝胶的有限元模型中需要计算每两根纤维(线段)之间的距离,最初参考的两篇文章确实提供了关于一些数值方法的计算思路(文章1 && 文章2),但忽略了线段距离问题的理论推导,导致 ...
- 应用区域生长(Seeded Region Growing)算法提取2D激光中的线段数据
0 Background 本文是针对于 **<A line segment extraction algorithm using laser data based on seeded regio ...
- python两个同切圆_求两个圆的交点
我试图在Python中找到两个圆之间的交集(使用Matplotlib),但是找不到任何值.在 为此,我为每个单独的圆创建X和Y的列表(Matplotlib在绘制圆时将第一个参数作为X值,第二个参数作为 ...
- Python求两个圆的交点坐标或三个圆的交点坐标
计算两个圆的交点代码如下 # -*- coding: utf-8 -*- import math import numpy as np def insec(p1,r1,p2,r2):x = p1[0] ...
- 【C\C++】空间中求一点到两点所构成的直线的距离
最近在做叶面重建的工作,构建叶面TIN的算法中会用到3维中点到直线的距离方程,一开始以为像二维一样有公式之类的,后来找了一下没有找到,就写了一个函数,分享一下 double dis_3D(Point ...
- 2D空间中基于矢量运算的碰撞后效果as3源码
碰撞过的过程简单的可以分为两个阶段: A 检测是否碰上的碰撞检测 B 碰撞了之后的效应 示例请见: http://www.cnblogs.com/vilyLei/articles/1386711.ht ...
- JAVA程序设计:求直线与圆的交点坐标
其实圆与直线的交点方程并不难解,难得是算法实现,废话不多说,直接上代码: 核心类 /*** @Author: Zhangwenshun* @Description:* @Date: Created i ...
- js求两圆交点_如何求两个圆的交点坐标,请举例
展开全部 将两个圆62616964757a686964616fe4b893e5b19e31333431363563的方程相减,就消掉了x²,y²项,剩下一个关于x, y的一次方程,可解得y=kx+b. ...
最新文章
- reporting Server組件不全引起的致命錯誤
- Mysql提示缺少表的别名报错_mysql对sql中别名引起的Column not found问题
- [译] ⚛ React 状态管理工具博物馆
- 如何异步的处理restful服务(基础)
- java jvm对象_【Java】JVM
- Python Django项目部署 Linux 服务器
- 微服务架构 —— 服务雪崩与容错方案
- php background-image,css background-image属性怎么用
- 阶段5 3.微服务项目【学成在线】_day01 搭建环境 CMS服务端开发_13-MongoDb入门-数据库和集合...
- 解析Disruptor的依赖关系
- 联机侠控制台JAVA_我的世界MultiMc启动器
- Ubuntu安装Spark
- bt采集蜘蛛,开源版
- MS17-010 永恒之蓝漏洞利用方式
- PHP正则淘口令,Flutter代码锦囊---淘口令复制弹窗
- threejs 加载obj模型
- 矢量网络分析仪--测天线时使用技巧
- E-Trunk小实验
- C语言编程笔记——MOOC翁恺
- 网友在各自领域中所用到的芯片总结(转载赛亚人hanker的博客)
热门文章
- fscapture下载收费吗?_听歌要收费,下载又要付费?国内的音乐app还想干什么?...
- android消息通知布局,Android Design
- oracle12c bug,12c expdp ORA-31623 -又遇到BUG
- elk服务重启_ELK开机启动 service文件内容
- gin -get请求的小示例1-Handle处理GET请求
- golang中的常用内置函数
- 山西省吕梁市职称计算机考试,山西吕梁考点2012年职称计算机考试时间通知
- 基于Yolov5目标检测的物体分类识别及定位(一) -- 数据集原图获取与标注
- 经典神经网络 -- FPN : 设计原理与pytorch实现
- JavaScript实现动态添加、移除元素或属性的方法分析