Check if the given point is in the circle - Android Graphics

Android examples for Graphics:Path Point

Description

Check if the given point is in the circle

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*ww w. j  a  v  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 given point is in the circle
     * 
     * @param x
     *            Point x coordinate
     * @param y
     *            Point y coordinate
     * @param circleX
     *            x coordinate of the circle's radius
     * @param circleY
     *            y coordinate of the circle's radius
     * @param circleRadius
     *            radius of the circle
     * @return
     */
    public static boolean isPointInCircle(float x, float y, float circleX,
            float circleY, float circleRadius) {
        double magic = Math.sqrt(Math.pow(circleX - x, 2)
                + Math.pow(circleY - y, 2));
        return magic < circleRadius;
    }

    public static boolean isPointInCircle(PointF point,
            PointF circleCenter, float circleRadius) {
        return isPointInCircle(point.x, point.y, circleCenter.x,
                circleCenter.y, circleRadius);
    }
}

Related Tutorials