Unity 向量和圆的交点 相交

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;
public float crossRadius = 0.5f;//交点圆圈大小 void OnDrawGizmos() {
if (a == null || b == null || circleCenter == null)
return; //交点1
var intersect1 = default(Vector2);
//交点2
var intersect2 = default(Vector2);
var intersectCount = BetweenLineAndCircle(circleCenter.position, radius, a.position, b.position, out intersect1, out intersect2); if (intersectCount > 0)
Gizmos.DrawWireSphere(intersect1, crossRadius); if (intersectCount > 1)
Gizmos.DrawWireSphere(intersect2, crossRadius); Gizmos.DrawLine(a.position, b.position);
Gizmos.DrawWireSphere(circleCenter.position, radius);
} /// <summary>
///
/// </summary>
/// <param name="circleCenter"></param>
/// <param name="circleRadius"></param>
/// <param name="point1"></param>
/// <param name="point2"></param>
/// <param name="intersection1">交点1</param>
/// <param name="intersection2">交点2</param>
/// <returns></returns>
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)) {
//没有交点的情况
intersection1 = Vector2.zero;
intersection2 = Vector2.zero;
return 0;
} if (determinate < 0.0000001 && determinate > -0.0000001) {
//一个交点的情况
t = -b / (2 * a);
intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);
intersection2 = Vector2.zero;
return 1;
} //两个交点的情况
t = ((-b + Mathf.Sqrt(determinate)) / (2 * a));
intersection1 = new Vector2(point1.x + t * dx, point1.y + t * dy);
t = ((-b - Mathf.Sqrt(determinate)) / (2 * a));
intersection2 = new Vector2(point1.x + t * dx, point1.y + t * dy); return 2;
}
}
Unity 向量和圆的交点 相交的更多相关文章
- Gym - 101617F :Move Away (圆的交点)
pro:给定N个圆,求离原点最远的点,满足它在N个圆里.输出这个距离.N<50; sol:关键点一定是圆与圆的交点. 圆与 圆心到原点的直线 的交点. 然后去验证这些关键点是否在N个圆内. 实际 ...
- 【4opencv】求解向量和轮廓的交点
在“学习OpenCV3"的QQ群众,网友且行且珍惜针对前期博客(https://www.cnblogs.com/jsxyhelu/p/9345590.html)中的内容提出了以下问题: 比如 ...
- HDU 5572--An Easy Physics Problem(射线和圆的交点)
An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Unity绘制圆和缓动雷达图
Unity绘制圆和缓动雷达图 之前在做UI模块的时候遇到过需要做雷达图的效果,所以简单复习了一下 关于网格绘制 我们都知道Unity绘制图形是通过Mesh网格添加顶点进行绘制,那么知道顶点信息后如何实 ...
- 简单几何(直线与圆的交点) ZOJ Collision 3728
题目传送门 题意:有两个一大一小的同心圆,圆心在原点,大圆外有一小圆,其圆心有一个速度(vx, vy),如果碰到了小圆会反弹,问该圆在大圆内运动的时间 分析:将圆外的小圆看成一个点,判断该直线与同心圆 ...
- hdu 5130(2014广州 圆与多边形相交模板)
题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y), (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...
- 2D空间中求线段与圆的交点
出处: https://answers.unity.com/questions/366802/get-intersection-of-a-line-and-a-circle.html 测试脚本(返回值 ...
- 牛客网暑期ACM多校训练营(第三场) J Distance to Work 计算几何求圆与多边形相交面积模板
链接:https://www.nowcoder.com/acm/contest/141/J来源:牛客网 Eddy has graduated from college. Currently, he i ...
- unity, 鼠标与场景交点
在鼠标与场景交点上放一个mark,并于1s后消失: 新建一个空GameObject,命名为moushHitTest,添加下面脚本: using UnityEngine;using System.Col ...
- 2D空间中求两圆的交点
出处:https://stackoverflow.com/questions/19916880/sphere-sphere-intersection-c-3d-coordinates-of-colli ...
随机推荐
- 工具使用之“contains包含的使用总结”
一.集合之间的包含 1.判断listOne是否全部包含listTow中的值 格式:CollectionUtils.containsAll(listOne, listTow) 或 listTow.con ...
- C++强制类型转换运算符(static_cast、reinterpret_cast、const_cast和dynamic_cast)
C++ 引入了四种功能不同的强制类型转换运算符以进行强制类型转换:static_cast.reinterpret_cast.const_cast和dynamic_cast,当然C++为保持与C的兼容, ...
- 去除string前面或后面的空白符
去除string前面或后面的空白符 // trim from start (construct new string) inline std::string ltrim(const std::stri ...
- ASP.NET Core中对开放泛型(Open Generic)的依赖注入
public interface IRepository<T> { void Add(T entity); List<T> Get(); } public class Repo ...
- MySQL 数字保留两位小数
1.ROUND(x,d) 用于数据的四舍五入,ROUND(x)其实就是ROUND(x,0),也就是默认d为0:这里有个值得注意的地方是,d可以是负数,这时是指定小数点左边的d位整数位为0,同时小数位均 ...
- 使用 frp 搭建 https 服务
踩了许多坑,特此记录一下 1.首先服务端的配置(yaml) # 服务端监听本机的 7000 和 443 端口 bindPort: 7000 # 用于 frp 客户端连接 vhostHTTPSPort: ...
- 使用 SpringBoot 集成 WebService [不需要身份验证]
一.使用 JDK 自带的工具生成实体类 # 格式 wsimport -s 保存路径 -p 包路径 -encoding utf-8 wsdl文件地址 # 实例 wsimport -s d:\wsdl - ...
- jenkins的搭建及问题处理
Jenkins搭建第一步 本文讲述的本人搭建Jenkins的流程及遇到的坑及处理方法 1.搭建 Linux操作系统为Centos Jenkins版本为24稳定版 操作命令: sudo wget -O ...
- wc记录
WC记录 咋可 随机化 CF1746F 给每个值随机成\(0/1\),然后查询区间内的所有数的和是否\(\equiv0\mod k\) 考虑错误率,对于一个不合法的权值,设其有\(r\)个,考虑区间内 ...
- Blazor学习之旅:文章目录索引
1 Blazor学习路径 微软学习社区(Microsoft Learn)早在两三年前就推出了一门学习路径<使用Blazor构建Web应用程序>,我们可以学到如何通过Blazor Web应用 ...