circle Intersects Rectangle - Android java.lang

Android examples for java.lang:Math Trigonometric Function

Description

circle Intersects Rectangle

Demo Code


//package com.java2s;

public class Main {
    /**//from w  w w . j a  v a 2 s . c o  m
     * @param circleX
     * @param circleY
     * @param circleRadius
     * @param rectX
     * @param rectY
     * @param rectWidth
     * @param rectHeight
     * @return Si un c?rculo interseca un rect?ngulo
     */
    public static boolean circleIntersectsRectagle(float circleX,
            float circleY, float circleRadius, float rectX, float rectY,
            float rectWidth, float rectHeight) {
        float circleDistanceX = Math.abs(circleX - rectX - rectWidth / 2);
        float circleDistanceY = Math.abs(circleY - rectY - rectHeight / 2);

        if (circleX > (rectWidth / 2 + circleRadius)) {
            return false;
        }
        if (circleY > (rectHeight / 2 + circleRadius)) {
            return false;
        }

        if (circleX <= (rectWidth / 2)) {
            return true;
        }
        if (circleY <= (rectHeight / 2)) {
            return true;
        }

        float cornerDistance_sq = (float) (Math.sqrt(circleDistanceX
                - rectWidth / 2) + Math.sqrt(circleDistanceY - rectHeight
                / 2));

        return cornerDistance_sq <= Math.sqrt(circleRadius);
    }
}

Related Tutorials