How do I detect intersections between a circle and any other circle in the same plane?

前端 未结 7 2243
走了就别回头了
走了就别回头了 2020-11-28 05:54

I\'m looking for an algorithm to detect if a circle intersects with any other circle in the same plane (given that there can be more than one circle in a plane).

One

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 06:04

    This solution in Java used the mathematical expresion which was described above:

    /**
         * 
         * @param values
         *            { x0, y0, r0, x1, y1, r1 }
         * @return true if circles is intersected
         * 
         *         Check if circle is intersect to another circle
         */
        public static boolean isCircleIntersect(double... values) {
            /*
             * check using mathematical relation: ABS(R0-R1) <=
             * SQRT((x0-x1)^2+(y0-y1)^2) <= (R0+R1)
             */
            if (values.length == 6) {
                /* get values from first circle */
                double x0 = values[0];
                double y0 = values[1];
                double r0 = values[2];
                /* get values from second circle */
                double x1 = values[3];
                double y1 = values[4];
                double r1 = values[5];
                /* returun result */
                return (Math.abs(r0 - r1) <= Math.sqrt(Math.pow((x0 - x1), 2)
                        + Math.pow((y0 - y1), 2)))
                        && (Math.sqrt(Math.pow((x0 - x1), 2)
                                + Math.pow((y0 - y1), 2)) <= (r0 + r1));
            } else {
                /* return default result */
                return false;
            }
        }
    

提交回复
热议问题