Java Geometry Algorithm interceptLineAndBox(Point2D startPosition, Point2D endPosition, RectangularShape boundingBox)

Here you can find the source of interceptLineAndBox(Point2D startPosition, Point2D endPosition, RectangularShape boundingBox)

Description

intercept Line And Box

License

Open Source License

Declaration

public static Point2D interceptLineAndBox(Point2D startPosition, Point2D endPosition,
            RectangularShape boundingBox) 

Method Source Code

//package com.java2s;
/**/*w  w w  .  jav  a  2  s.c  o  m*/
 * 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.
 */

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

  1. generateRobotPositions(Point start, Point end, int stepSize)
  2. generateSpline(final Point[] controls)
  3. gridAlign(final Point2D point, final double gridX, final double gridY)
  4. hitsLine(final Point2D p, final Point2D fromPoint, final Point2D toPoint, final double thickness)
  5. insidePoly(Polygon pg, Point p)
  6. interpol(Point p1, Point p2, float factor)
  7. invVec(final Point2D v)
  8. lonLatToString(Point2D.Double pt)
  9. makeCircle(double xCenter, double yCenter, double r, int nPoints)