Java examples for 2D Graphics:Rectangle
Check if a specified polyline intersects a specified rectangle.
/******************************************************************************* * Copyright (c) 2011 MadRobot.//from w ww . jav a2 s . c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * Elton Kent - initial API and implementation ******************************************************************************/ public class Main{ /** * Check if a specified polyline intersects a specified rectangle. * Integer domain. * * @param x * , y Polyline to check. * @param x0 * , y0, x1, y1 Upper left and lower left corner of rectangle * (inclusive). * @return True if the polyline intersects the rectangle, * false otherwise. */ public static boolean isPolylineIntersectingRectangle(float[] x, float[] y, float x0, float y0, float x1, float y1) { if (x.length == 0) { return false; } if (isPointInsideRectangle(x[0], y[0], x0, y0, x1, y1)) { return true; } else if (x.length == 1) { return false; } for (int i = 1; i < x.length; i++) { if ((x[i - 1] != x[i]) || (y[i - 1] != y[i])) { if (LineUtils.isLineIntersectingRectangle(x[i - 1], y[i - 1], x[i], y[i], x0, y0, x1, y1)) { return true; } } } return false; } /** * Check if a specified point is inside a specified rectangle. * * @param x0 * , y0, x1, y1 Upper left and lower right corner of rectangle * (inclusive) * @param x * ,y Point to check. * @return True if the point is inside the rectangle, * false otherwise. */ public static boolean isPointInsideRectangle(float x0, float y0, float x1, float y1, float x, float y) { return (x >= x0) && (x < x1) && (y >= y0) && (y < y1); } }