circle Intersects Line - Android java.lang

Android examples for java.lang:Math Geometry

Description

circle Intersects Line

Demo Code


//package com.java2s;

public class Main {
    /**/*  w ww .  ja  va  2 s  .c o m*/
     * @param circleX
     * @param circleY
     * @param circleRadius
     * @param rectX0
     * @param rectY0
     * @param rectX1
     * @param rectY1
     * @param distance
     * @return Si un c?rculo interseca una recta
     */
    public static boolean circleIntersectsLine(float circleX,
            float circleY, float circleRadius, float rectX0, float rectY0,
            float rectX1, float rectY1, float distance) {
        if (isPointInCircle(rectX0, rectY0, circleX, circleY, circleRadius)) {
            return true;
        }
        if (getDistance(rectX0, rectY0, rectX1, rectY1) > distance) {
            float medX = (rectX0 + rectX1) / 2;
            float medY = (rectY0 + rectY1) / 2;
            return circleIntersectsLine(circleX, circleY, circleRadius,
                    medX, medY, rectX1, rectY1, distance);
        }

        return false;
    }

    /**
     * @param pointX
     * @param pointY
     * @param circleX
     * @param circleY
     * @param radius
     * @return Si un punto est? dentro de un c?rculo
     */
    public static boolean isPointInCircle(float pointX, float pointY,
            float circleX, float circleY, float radius) {
        return circleX + radius >= pointX && circleX - radius <= pointX
                && circleY + radius >= pointY && circleY - radius <= pointY;
    }

    /**
     * @param x0
     * @param y0
     * @param x1
     * @param y1
     * @return distancia entre dos puntos
     */
    public static float getDistance(float x0, float y0, float x1, float y1) {
        return getDistance(x1 - x0, y1 - y0);
    }

    /**
     * @param side0
     * @param side1
     * @return distancia entre dos puntos en la misma recta
     */
    public static float getDistance(float side0, float side1) {
        return (float) Math.sqrt(side0 * side0 + side1 * side1);
    }
}

Related Tutorials