21. 移动的圆

21. 移动的圆

题目将给出两个圆A和B的圆心坐标(x,y)和半径r,现给你一个点P,使圆A圆心沿直线运动至点P。

请问圆A在运动过程中是否会与圆B相交?(运动过程包括起点和终点)

若会相交返回1,否则返回-1。

样例

样例 1

输入:[0,0,2.5,3,2,0.5,0,2]

输出:1

样例解释:圆A的圆心(0,0),半径为2.5,圆B的圆心(3,2),半径为0.5,点P(0,2),如图:

样例 2

输入:[0,0,2,5,0,1,0,2]

输出:-1

样例解释:圆A的圆心(0,0),半径为2,圆B的圆心(5,0),半径为1,点P(0,2)

注意事项

两个圆的半径均不超过10000。

横纵坐标值的绝对值均不超过10000。


public class Solution {

    /**

     * @param position: the position of circle A,B and point P.

     * @return: if two circle intersect return 1, otherwise -1.

     */

    public int IfIntersect(double[] position) {

           double x3 = position[0];

            double y3 = position[1];

            double aR = position[2];

            double x1 = position[3];

            double y1 = position[4];

            double bR = position[5];

            double x2 = position[6];

            double y2 = position[7];





            double sumR = aR + bR;

            double sumR2 =aR - bR;

            double r = 0;

            double flag = 100d;//最小可以20

            // System.out.println(sumR+","+sumR2);

            

            double x23 = (x2 - x3) / flag;

            double y23 = (y2 - y3) / flag;

            for (int i = 0; i <= flag; i++) {

                double v = x3 + x23* i - x1;

                double z = y3 + y23* i - y1;

                r = Math.sqrt(v * v + z * z);

                // System.out.println(v+","+z+"="+r);

                if (r <= sumR && r >= sumR2) return 1;

            }

            return -1;

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代我西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值