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 > )
Gizmos.DrawWireSphere(intersect1, 0.1f); if (intersectCount > )
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 = * (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 - * a * c;
if ((a <= 0.0000001) || (determinate < -0.0000001))
{
// No real solutions.
intersection1 = Vector2.zero;
intersection2 = Vector2.zero;
return ;
}
if (determinate < 0.0000001 && determinate > -0.0000001)
{
// One solution.
t = -b / ( * a);
intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);
intersection2 = Vector2.zero;
return ;
} // Two solutions.
t = (float)((-b + Mathf.Sqrt(determinate)) / ( * a));
intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);
t = (float)((-b - Mathf.Sqrt(determinate)) / ( * a));
intersection2 = new Vector2(point1.x + t * dx, point1.y + t * dy); return ;
}
}
2D空间中求线段与圆的交点的更多相关文章
- 2D空间中求一点是否在多边形内
参考自这篇博文:http://www.cnblogs.com/dabiaoge/p/4491540.html 一开始没仔细看做法,浪费了不少时间.下面是最终实现的效果: 大致流程: 1.随便选取多边形 ...
- 2D空间中求两圆的交点
出处:https://stackoverflow.com/questions/19916880/sphere-sphere-intersection-c-3d-coordinates-of-colli ...
- [译]2D空间中使用四叉树Quadtree进行碰撞检测优化
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity2017.2.0f3 原文出处 : Quick Tip: Use Quadtrees to Detect Lik ...
- 2D空间中判断一点是否在三角形内
要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...
- 2d游戏中求出一个向量的两个垂直向量
function cc.exports.VerticalVector(vec)--求出两个垂直向量 local result = {} result[1] = cc.p(vec.y/vec.x,-1) ...
- HDU 3467 (求五个圆相交面积) Song of the Siren
还没开始写题解我就已经内牛满面了,从晚饭搞到现在,WA得我都快哭了呢 题意: 在DotA中,你现在1V5,但是你的英雄有一个半径为r的眩晕技能,已知敌方五个英雄的坐标,问能否将该技能投放到一个合适的位 ...
- java求两个圆相交坐标
最近由于项目需要,根据两个圆函数求出相交的坐标.实现代码如下,另感谢两圆求交点算法实现Java代码,虽然他所贡献的代码中存在问题,但仍有借鉴意义. 1.两个圆相交的数学求法 在中学数学中我们知道,一个 ...
- HDU 5572--An Easy Physics Problem(射线和圆的交点)
An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Gym - 101617F :Move Away (圆的交点)
pro:给定N个圆,求离原点最远的点,满足它在N个圆里.输出这个距离.N<50; sol:关键点一定是圆与圆的交点. 圆与 圆心到原点的直线 的交点. 然后去验证这些关键点是否在N个圆内. 实际 ...
随机推荐
- 014.Docker Harbor+Keepalived+LVS+共享存储高可用架构
一 多Harbor高可用介绍 共享后端存储是一种比较标准的方案,将多个Harbor实例共享同一个后端存储,任何一个实例持久化到存储的镜像,都可被其他实例中读取.通过前置LB组件,如Keepalived ...
- UC浏览器中Ajax请求中传递数据的一个坑
今天突然收到一个bug,有用户在其浏览器环境中一直无法提交内容,使用的是UC浏览器.当换成Chrome时,内容能够正常提交.鉴于本地没有一直使用Firefox 以及Chrome,于是去下载了一个UC ...
- ubuntu安装nodejs出现./config.gypi错误
报错的内容如下: xxx@xxx [/usr/local/src/node-v0.8.3]# ./configure { 'target_defaults': { 'cflags': [], 'def ...
- [软件研究]对wdcp v3的一次小研究#1
0x00 前言 好久没有更新了,已经长草无疑. 之前团队要搞个测验的系统,用来安全培训考核,团队内又没啥人搞开发的,自己又想学一下vue,就用vue+ci 撸了一个. 搞了一个星期基本搞完(开发能力真 ...
- 使用windows脚本移动文件
1. 移动脚本 在部署web项目时,一般需要将打包的war包发布到Tomcat目录下,所以自己就在网上查找资料写了一个简略的移动文件的脚本,如下: @echo off echo "使用bat ...
- C# DataGridView搜索
public static bool SearchDGV(DataGridView DGV, string strTxt, bool UpSearch = true, bool Show = fals ...
- python 文本分类
python 文本分类 pyhton 机器学习 待续...
- loj#2071. 「JSOI2016」最佳团体
题目链接 loj#2071. 「JSOI2016」最佳团体 题解 树形dp强行01分规 代码 #include<cstdio> #include<cstring> #inclu ...
- [FFT/NTT/MTT]总结
最近重新学了下卷积,简单总结一下,不涉及细节内容: 1.FFT 朴素求法:$Coefficient-O(n^2)-CoefficientResult$ FFT:$Coefficient-O(nlogn ...
- Python图形编程探索系列-02-框架设计
跳转到我的博客 设计任务 在主窗口root中放置三个容器用于容纳组件,容器采用框架设计. 代码初步设计 import tkinter as tk root = tk.Tk() root.geometr ...