2D空间中求两圆的交点

出处:https://stackoverflow.com/questions/19916880/sphere-sphere-intersection-c-3d-coordinates-of-collision-points

修改(加入包含和不相交情况的判断):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CircleIntersect : MonoBehaviour
{
    public Transform circleA;
    public float radiusA = 1f;
    public Transform circleB;
    public float radiusB = 1f;

    public bool CalculateCircleIntersect(Vector3 c1p, Vector3 c2p, float c1r, float c2r, out Vector3 p0, out Vector3 p1)
    {
        //c1p = circle one position
        //c1r = circle one radius

        var P0 = c1p;
        var P1 = c2p;

        float d, a, h;
        p0 = Vector3.zero;
        p1 = Vector3.zero;

        d = Vector3.Distance(P0, P1);

        if (d > c1r + c2r) return false;
        if (Vector3.Distance(c2p, c1p) + c1r < c2r) return false;
        if (Vector3.Distance(c2p, c1p) + c2r < c1r) return false;

        a = (c1r * c1r - c2r * c2r + d * d) / (2 * d);

        h = Mathf.Sqrt(c1r * c1r - a * a);

        Vector3 P2 = (P1 - P0);
        P2 = (P2 * (a / d));
        P2 = (P2 + P0);

        float x3, y3, x4, y4 = 0;

        x3 = P2.x + h * (P1.y - P0.y) / d;
        y3 = P2.y - h * (P1.x - P0.x) / d;

        x4 = P2.x - h * (P1.y - P0.y) / d;
        y4 = P2.y + h * (P1.x - P0.x) / d; ;

        //out parameters for a line renderer
        p0 = new Vector3(x3, y3, 0);
        p1 = new Vector3(x4, y4, 0);

        return true;
    }

    void OnDrawGizmos()
    {
        if (circleA == null || circleB == null) return;

        var cacheColor = Gizmos.color;

        var p0 = default(Vector3);
        var p1 = default(Vector3);
        var isIntersect = CalculateCircleIntersect(circleA.position, circleB.position, radiusA, radiusB, out p0, out p1);

        if (isIntersect)
        {
            Gizmos.DrawWireSphere(p0, 0.1f);
            Gizmos.DrawWireSphere(p1, 0.1f);
            Gizmos.color = Color.red;
        }

        Gizmos.DrawWireSphere(circleA.position, radiusA);
        Gizmos.DrawWireSphere(circleB.position, radiusB);

        Gizmos.color = cacheColor;
    }
}

原文地址:https://www.cnblogs.com/hont/p/8991727.html

时间: 2024-08-27 12:54:24

2D空间中求两圆的交点的相关文章

[译]2D空间中使用四叉树Quadtree进行碰撞检测优化

操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity2017.2.0f3 原文出处 : Quick Tip: Use Quadtrees to Detect Likely Collisions in 2D Space 许多游戏需要使用碰撞检测算法去判定两个对象是否发生碰撞,但是这些算法通常意味着昂贵操作,拖慢游戏的运行速度.在这篇文章中我们将会学习四叉树 quadtrees,并学习如果通过四叉树跳过那些物理空间距离比较远的对象,最终提高碰撞检测速度. 注:原文

POJ 2546 &amp; ZOJ 1597 Circular Area(求两圆相交的面积 模板)

题目链接: POJ:http://poj.org/problem?id=2546 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=597 Description Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three di

hdu 3264 Open-air shopping malls 求两圆相交

对每个圆二分半径寻找可行的最小半径,然后取最小的一个半径. 对于两圆相交就只要求到两个扇形,然后减去两个全等三角形就行了. #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> using namespace std; #define pi acos(-1.0) #define eps 1e-8 #define maxn 50 int n; struct point{

Gym - 101915B Ali and Wi-Fi 计算几何 求两圆交点

题面 题意:给你n个圆,每个圆有一个权值,你可以选择一个点,可以获得覆盖这个点的圆中,权值最大的m个的权值,问最多权值是多少 题解:好像是叙利亚的题....我们画画图就知道,我们要找的就是圆与圆交的那部分里面的点,我们再仔细看看, 2个圆的交点一定在啊! 别急啊,两个圆包含了,都是交点,取哪呢?当然小圆圆心就够了啊(圆又不多,写的时候直接把所有的圆心都丢进去了) 然后枚举判断每个点有没有被在m个圆中就行了,这里维护最大的m个,用个堆就好了 1 #include<bits/stdc++.h> 2

求两圆相交部分面积(C++)

已知两圆圆心坐标和半径,求相交部分面积: 1 #include <iostream> 2 using namespace std; 3 #include<cmath> 4 #include<stdio.h> 5 #define PI 3.141593 6 struct point//点 7 { 8 double x,y; 9 }; 10 struct circle//圆 11 { 12 point center; 13 double r; 14 }; 15 float

UVa 10674 (求两圆公切线) Tangents

题意: 给出两个圆的圆心坐标和半径,求这两个圆的公切线切点的坐标及对应线段长度.若两圆重合,有无数条公切线则输出-1. 输出是按照一定顺序输出的. 分析: 首先情况比较多,要一一判断,不要漏掉. 如果高中的那点老底还在的话,代码还是很好理解的. 1 //#define LOCAL 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #incl

Python中求两个list列表中共同元素??

1 # 求两个list中的共同元素 2 def commonElement(): 3 a=[1,2,3,5,6]; 4 b=[1,2,5]; 5 commonEle=[val for val in a if val in b]; 6 commonNum=len(commonEle); 7 return commonEle,commonNum; 运行结果: import randMatrix;为自己建立的模块 1 >>> import randMatrix; 2 >>>

2D空间中判断一点是否在三角形内

本来打算做三角形填充多边形,但需要用到耳切法正在看.所以先研究了这个 要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码(Unity3D): using UnityEngine; using System.Collections; using System.Collections.Generic; public class TriangleColl

2d游戏中求出一个向量的两个垂直向量

function cc.exports.VerticalVector(vec)--求出两个垂直向量 local result = {} result[1] = cc.p(vec.y/vec.x,-1)--向下方向 result[2] = cc.p(-vec.y/vec.x,1)--向上方向 return result end