zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    2019 SDN上机第五次作业
    软件工程实践作业——软件评测
    2019 SDN上机第四次作业
    2019 SDN阅读作业
    2019 SDN上机第三次作业
    2020年系统综合实践 期末大作业 19组
    2020年系统综合实践 第7次实践作业 07组
    2020年系统综合实践 第6次实践作业 07组
    2020年系统综合实践 第五次作业
    2020年系统综合实践 第四次作业
  • 原文地址:https://www.cnblogs.com/sanyejun/p/15801960.html
Copyright © 2011-2022 走看看