Java examples for java.lang:Math Geometry Line
Returns intersection point of the rectangle and the line from the middle of that rectangle to the outer point.
/*//from w ww .j av a2 s . c o m * This file is part of WebLookAndFeel library. * * WebLookAndFeel library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * WebLookAndFeel library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with WebLookAndFeel library. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Dimension; import java.awt.Point; import java.awt.Rectangle; import java.util.List; public class Main{ /** * Returns intersection point of the rectangle and the line goin from the * middle of that rectangle to the outer point. * * @param rect * rectangle to process * @param outer * outer point to process * @return intersection point of the rectangle and the line goin from the * middle of that rectangle to the outer point */ public static Point findMiddleLineIntersection(final Rectangle rect, final Point outer) { final Point middle = GeometryUtils.middle(rect); final int x1 = middle.x; final int y1 = middle.y; final int x2 = outer.x; final int y2 = outer.y; if (x2 < rect.x) { final int x = rect.x; final int y = (x1 * (y2 - y1) - y1 * (x2 - x1) - x * (y2 - y1)) / (x1 - x2); if (y >= rect.y && y <= rect.y + rect.height) { return new Point(x, y); } } else if (x2 > rect.x + rect.width) { final int x = rect.x + rect.width; final int y = (x1 * (y2 - y1) - y1 * (x2 - x1) - x * (y2 - y1)) / (x1 - x2); if (y >= rect.y && y <= rect.y + rect.height) { return new Point(x, y); } } if (y2 < rect.y) { final int y = rect.y; final int x = (x1 * (y2 - y1) - y1 * (x2 - x1) - y * (x1 - x2)) / (y2 - y1); if (x >= rect.x && x <= rect.x + rect.width) { return new Point(x, y); } } else if (y2 > rect.y + rect.height) { final int y = rect.y + rect.height; final int x = (x1 * (y2 - y1) - y1 * (x2 - x1) - y * (x1 - x2)) / (y2 - y1); if (x >= rect.x && x <= rect.x + rect.width) { return new Point(x, y); } } return middle; } /** * Returns middle point for the specified rectangle. * * @param rectangle * rectangle to process * @return middle point for the specified rectangle */ public static Point middle(final Rectangle rectangle) { return new Point(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2); } /** * Returns middle point between the specified points. * * @param p1 * first point * @param p2 * second point * @return middle point between the specified points */ public static Point middle(final Point p1, final Point p2) { return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); } /** * Returns middle point between the specified points. * * @param x1 * first point X coordinate * @param y1 * first point Y coordinate * @param x2 * second point X coordinate * @param y2 * second point Y coordinate * @return middle point between the specified points */ public static Point middle(final int x1, final int y1, final int x2, final int y2) { return new Point((x1 + x2) / 2, (y1 + y2) / 2); } }