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;
    }
}
复制代码

 

posted @   HONT  阅读(1060)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2016-05-04 Unity中实现List类型的自定义GUI(ReorderableList)
2016-05-04 [转]Unity: make your lists functional with ReorderableList
2013-05-04 二叉树的学习
点击右上角即可分享
微信分享提示
回到顶部