Unity两个圆相交,求两个交点

可用于挖机中的挖沟处机械结构运行

using UnityEngine;

public class JointLinkage : MonoBehaviour
{
    public bool isDrawGizmos = true;
    public Transform circle1; // 圆1的Transform组件
    public Transform circle2; // 圆2的Transform组件
    public float radius1; // 圆1的半径
    public float radius2; // 圆2的半径 
    public Vector3 intersection1; //相交点1
    public Vector3 intersection2; //相交点2
    public CoordinateAxisType coordinateAxisType1 = CoordinateAxisType.X_Y; // 选择要绘制的轴
    public IntersectionType intersectionType = IntersectionType.intersection1;

    private Vector3 center1;
    private Vector3 center2;

    private void Start()
    {
        //SetRadius();
    }

    void Update()
    {
        CircleFocalPoint();
    }

    private void OnDrawGizmos()
    {
        if (!isDrawGizmos) return;
        //SetRadius();
        Gizmos.color = Color.black;
        Gizmos.DrawWireSphere(circle1.position, radius1);
        Gizmos.color = Color.white;
        Gizmos.DrawWireSphere(circle2.position, radius2);
    }

    public Vector3 GetSelfPos()
    {
        switch (intersectionType)
        {
            case IntersectionType.intersection1:
                return transform.position = intersection1;
            case IntersectionType.intersection2:
                return transform.position = intersection2;
        }
        return transform.position = intersection2;
    }

    private void CircleFocalPoint()
    {
        center1 = circle1.position; // 圆1的中心点
        center2 = circle2.position; // 圆2的中心点
        float distance = Vector3.Distance(center1, center2); // 两个圆心的距离

        // 判断两个圆是否相交
        if (distance < radius1 + radius2)
        {
            // 计算圆1和圆2的交点
            float a = (radius1 * radius1 - radius2 * radius2 + distance * distance) / (2 * distance);
            float h = Mathf.Sqrt(radius1 * radius1 - a * a);
            Vector3 p2 = center1 + a * (center2 - center1) / distance;
            Vector3[] intersections;
            GetIntersection(p2, h, center1, center2, distance, out intersections);
            intersection1 = intersections[0];
            intersection2 = intersections[1];

            if (isDrawGizmos)
                DrawLine();
        }
        else
        {
            // 两个圆不相交,
            Debug.LogWarning("挖机报废了!!!");
        }
    }

    private void GetIntersection(Vector3 p2, float h, Vector3 center1, Vector3 center2, float distance, out Vector3[] intersections)
    {
        ComputeIntersection(coordinateAxisType1, p2, h, center1, center2, distance, out intersections);
    }

    private void ComputeIntersection(CoordinateAxisType coordinateAxisType, Vector3 p2, float h, Vector3 center1, Vector3 center2, float distance, out Vector3[] intersections)
    {
        intersections = new Vector3[2];
        switch (coordinateAxisType)
        {
            case CoordinateAxisType.X_Y:

                intersections[0] = new Vector3(
                    p2.x + h * (center2.y - center1.y) / distance,
                    p2.y - h * (center2.x - center1.x) / distance,
                    center1.z);
                intersections[1] = new Vector3(
                    p2.x - h * (center2.y - center1.y) / distance,
                    p2.y + h * (center2.x - center1.x) / distance,
                    center1.z);

                break;
            case CoordinateAxisType.X_Z:

                intersections[0] = new Vector3(
                   p2.x + h * (center2.z - center1.z) / distance,
                   0,
                   p2.z - h * (center2.x - center1.x) / distance);
                intersections[1] = new Vector3(
                    p2.x - h * (center2.z - center1.z) / distance,
                    0,
                    p2.z + h * (center2.x - center1.x) / distance);

                break;
            case CoordinateAxisType.Y_Z:

                intersections[0] = new Vector3(
                    0,
                   p2.y + h * (center2.z - center1.z) / distance,
                   p2.z - h * (center2.y - center1.y) / distance);
                intersections[1] = new Vector3(
                    0,
                    p2.y - h * (center2.z - center1.z) / distance,
                    p2.z + h * (center2.y - center1.y) / distance);

                break;
            default:
                break;
        }
    }

    private void SetRadius()
    {
        radius1 = circle1.localScale.x / 2f;
        radius2 = circle2.localScale.x / 2f;
    }

    private void DrawLine()
    {
        Debug.DrawLine(center1, center2, Color.red);
        Debug.DrawLine(intersection1, intersection2, Color.green);
        switch (intersectionType)
        {
            case IntersectionType.intersection1:

                Debug.DrawLine(center1, intersection1, Color.blue);
                Debug.DrawLine(center2, intersection1, Color.yellow);

                break;
            case IntersectionType.intersection2:

                Debug.DrawLine(center1, intersection2, Color.blue);
                Debug.DrawLine(center2, intersection2, Color.yellow);

                break;
            default:
                break;
        }
    }

    public enum CoordinateAxisType
    {
        X_Y,
        X_Z,
        Y_Z,
    }

    public enum IntersectionType
    {
        intersection1,
        intersection2,
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值