Check if the two circle are colliding - Android Graphics

Android examples for Graphics:Path

Description

Check if the two circle are colliding

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from  w  ww  . j av a 2  s  .c  o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;
import android.graphics.PointF;

public class Main {
    /**
     * Check if the two circle are colliding
     * 
     * @param circle1Center
     *            Center point of the first circle
     * @param circle1Radius
     *            Radius of the first circle
     * @param circle2Center
     *            Center point of the second circle
     * @param circle2Radius
     *            Radius of the second circle
     * @return true,if the two circles are colliding.
     */
    public static boolean isColliding(PointF circle1Center,
            float circle1Radius, PointF circle2Center, float circle2Radius) {
        final double a = circle1Radius + circle2Radius;
        final double dx = circle1Center.x - circle2Center.x;
        final double dy = circle1Center.y - circle2Center.y;
        return a * a > (dx * dx + dy * dy);
    }
}

Related Tutorials