博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2D空间中求线段与圆的交点
阅读量:6691 次
发布时间:2019-06-25

本文共 2301 字,大约阅读时间需要 7 分钟。

 

出处: 

 

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

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;    }}

 

转载于:https://www.cnblogs.com/hont/p/8991751.html

你可能感兴趣的文章
ORACLE---添加控制文件
查看>>
Qt中QString,char,int,QByteArray之间到转换
查看>>
Exchange Server 2007邮箱存储服务器的集群和高可用性技术(上)
查看>>
磁盘管理与磁盘阵列RAID
查看>>
Linux学习笔记4-软件安装
查看>>
8.python之面相对象part.8(类装饰器)
查看>>
Spark通过Java Web提交任务
查看>>
Javascript动态加载脚本与样式
查看>>
LINUX用户和组小练习
查看>>
centos 7 配置 iptable-service
查看>>
Spring AOP之简单实践
查看>>
Java序列化漏洞的调研,***和安全监控
查看>>
想要百度信息流效果更好你应该这样投放
查看>>
Oracle教程之Oralce OMF功能详解(三)--使用Oralce OMF管理控制文件
查看>>
C# extern 修饰符的用法
查看>>
Zabbix修正错误两例(只提供解决思路)
查看>>
Redhat6.X 配置HP3PAR7200存储多路径过程
查看>>
Java基础系列19:使用JXL或者POI生成和解析Excel文件
查看>>
使用xshell打开centos中文显示为乱码
查看>>
达内实习——数据库编程、文件读写数据
查看>>