intercept Line And RectangularShape - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

intercept Line And RectangularShape

Demo Code

/**// w  ww  .j a  va2s.  c om
 * Copyright Tao, All Rights Reserved.
 * Confidential, do not distribute.
 *
 * Any source code displaying this header must
 * be considered closed source and confidential
 * until the project is released under an open
 * source license.
 */
//package com.java2s;
import java.awt.geom.Point2D;

import java.awt.geom.RectangularShape;

public class Main {
    public static Point2D interceptLineAndBox(Point2D startPosition,
            Point2D endPosition, RectangularShape boundingBox) {
        Point2D intercept = new Point2D.Double();

        if (startPosition.getX() == endPosition.getX()) {
            if (startPosition.getY() > endPosition.getY()) {
                intercept.setLocation(startPosition.getX(),
                        boundingBox.getMaxY());
            } else {
                intercept.setLocation(startPosition.getX(),
                        boundingBox.getMinY());
            }
        } else {
            double slope = (endPosition.getY() - startPosition.getY())
                    / (endPosition.getX() - startPosition.getX());
            double offset = endPosition.getY() - slope * endPosition.getX();
            double rightX = boundingBox.getMaxX();
            double leftX = boundingBox.getMinX();

            if (startPosition.getY() > endPosition.getY()) {
                double bottomY = boundingBox.getMaxY();
                double bottomX = intersectHorizontal(bottomY, slope, offset);

                if (bottomX > rightX) {
                    intercept.setLocation(rightX,
                            intersectVertical(rightX, slope, offset));
                } else if (bottomX < leftX) {
                    intercept.setLocation(leftX,
                            intersectVertical(leftX, slope, offset));
                } else {
                    intercept.setLocation(bottomX, bottomY);
                }
            } else if (startPosition.getY() < endPosition.getY()) {
                double topY = boundingBox.getMinY();
                double topX = intersectHorizontal(topY, slope, offset);

                if (topX > rightX) {
                    intercept.setLocation(rightX,
                            intersectVertical(rightX, slope, offset));
                } else if (topX < leftX) {
                    intercept.setLocation(leftX,
                            intersectVertical(leftX, slope, offset));
                } else {
                    intercept.setLocation(topX, topY);
                }
            } else {
                if (startPosition.getX() > rightX) {
                    intercept.setLocation(rightX, startPosition.getY());
                } else {
                    intercept.setLocation(leftX, startPosition.getY());
                }
            }
        }

        return intercept;
    }

    private static double intersectHorizontal(double y, double slope,
            double offset) {
        return (y - offset) / slope;
    }

    private static double intersectVertical(double x, double slope,
            double offset) {
        return slope * x + offset;
    }
}

Related Tutorials