get Intersection Point - Java java.lang

Java examples for java.lang:Math Geometry

Description

get Intersection Point

Demo Code

/*//w w w  .j  av a2s .  c o m
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import java.awt.*;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Main {
    public static Point getIntersectionPoint(Line2D aSegment,
            Rectangle aRectangle) {

        if (segmentOutsideRectangle(aRectangle, aSegment)) {
            throw new IllegalArgumentException("Segment "
                    + toString(aSegment) + " lies out of rectangle "
                    + aRectangle + " or intersects more than one bound");
        }

        if (segmentInsideRectangle(aRectangle, aSegment)) {
            return null;
        }

        Line2D[] bounds = new Line2D[4];
        bounds[0] = getTopOf(aRectangle);
        bounds[1] = getRightOf(aRectangle);
        bounds[2] = getBottomOf(aRectangle);
        bounds[3] = getLeftOf(aRectangle);

        for (int i = 0; i < bounds.length; i++) {
            if (bounds[i].intersectsLine(aSegment)) {
                return getIntersectionPoint(aSegment, bounds[i]);
            }
        }

        return null;
    }

    public static Point getIntersectionPoint(Line2D aFirst, Line2D aSecond) {
        double firstDeltaX = aFirst.getX2() - aFirst.getX1();
        double firstDeltaY = aFirst.getY2() - aFirst.getY1();

        double kFirst = firstDeltaY / firstDeltaX;
        double bFirst = aFirst.getY1() - kFirst * aFirst.getX1();

        double secondDeltaX = aSecond.getX2() - aSecond.getX1();
        double secondDeltaY = aSecond.getY2() - aSecond.getY1();

        double kSecond = secondDeltaY / secondDeltaX;
        double bSecond = aSecond.getY1() - kSecond * aSecond.getX1();

        double xIntersection = -100000000;
        double yIntersection = -100000000;

        double deltaK = (kFirst - kSecond);

        if (linesAreAngledAndParallel(kFirst, kSecond)) {
            return null;
        }

        if (Double.isInfinite(deltaK) || (0 == deltaK)) {

            if (firstDeltaX == secondDeltaX && 0 == firstDeltaX) {
                return null;
            }

            if (firstDeltaY == secondDeltaY && 0 == firstDeltaY) {
                return null;
            }

            if ((0 == firstDeltaX) && (0 == secondDeltaY)) {
                xIntersection = aFirst.getX1();
                yIntersection = aSecond.getY1();
            } else if ((0 == secondDeltaX) && (0 == firstDeltaY)) {
                xIntersection = aSecond.getX1();
                yIntersection = aFirst.getY1();
            } else {

                if (0 == firstDeltaX) {
                    xIntersection = aFirst.getX1();
                    yIntersection = kSecond * xIntersection + bSecond;
                } else {
                    xIntersection = aSecond.getX1();
                    yIntersection = kFirst * xIntersection + bFirst;
                }
            }

        } else {
            xIntersection = (bSecond - bFirst) / deltaK;
            yIntersection = kFirst * xIntersection + bFirst;
        }

        return new Point((int) xIntersection, (int) yIntersection);
    }

    private static boolean segmentOutsideRectangle(Rectangle aRectangle,
            Line2D aSegment) {
        return (!isWithin(aRectangle, aSegment.getP1()))
                && (!isWithin(aRectangle, aSegment.getP2()));
    }

    public static String toString(Line2D aLine) {
        return aLine.getP1() + ":" + aLine.getP2();
    }

    private static boolean segmentInsideRectangle(Rectangle aRectangle,
            Line2D aSegment) {
        return isWithin(aRectangle, aSegment.getP1())
                && isWithin(aRectangle, aSegment.getP2());
    }

    public static Line2D.Double getTopOf(Rectangle aRectangle) {
        return new Line2D.Double(aRectangle.getX(), aRectangle.getY(),
                aRectangle.getX() + aRectangle.getWidth(),
                aRectangle.getY());
    }

    public static Line2D.Double getRightOf(Rectangle aRectangle) {
        return new Line2D.Double(aRectangle.getX() + aRectangle.getWidth(),
                aRectangle.getY(), aRectangle.getX()
                        + aRectangle.getWidth(), aRectangle.getY()
                        + aRectangle.getHeight());
    }

    public static Line2D.Double getBottomOf(Rectangle aRectangle) {
        return new Line2D.Double(aRectangle.getX(), aRectangle.getY()
                + aRectangle.getHeight(), aRectangle.getX()
                + aRectangle.getWidth(), aRectangle.getY()
                + aRectangle.getHeight());
    }

    public static Line2D.Double getLeftOf(Rectangle aRectangle) {
        return new Line2D.Double(aRectangle.getX(), aRectangle.getY(),
                aRectangle.getX(), aRectangle.getY()
                        + aRectangle.getHeight());
    }

    private static boolean linesAreAngledAndParallel(double aKFirst,
            double aKSecond) {
        return (aKFirst == aKSecond) && (0 != aKFirst);
    }

    public static boolean isWithin(Rectangle aRectangle, Point2D aPoint) {
        return (aPoint.getX() > aRectangle.getX())
                && (aPoint.getX() < aRectangle.getX()
                        + aRectangle.getBounds().width)
                && ((aPoint.getY() > aRectangle.getY()) && (aPoint.getY() < aRectangle
                        .getY() + aRectangle.getBounds().height));
    }
}

Related Tutorials