001package icy.type.rectangle; 002 003import icy.type.geom.Line2DUtil; 004import icy.util.ShapeUtil; 005 006import java.awt.geom.Line2D; 007import java.awt.geom.Point2D; 008import java.awt.geom.Rectangle2D; 009import java.util.ArrayList; 010import java.util.List; 011 012public class Rectangle2DUtil 013{ 014 /** 015 * Returns the shortest line segment which result from the intersection of the given rectangle <b>bounds</b> and 016 * line. It returns <code>null</code> if the line segment does not intersects the Rectangle <b>content</b>. 017 */ 018 public static Line2D getIntersectionLine(Rectangle2D rectangle, Line2D line) 019 { 020 if (rectangle.intersectsLine(line)) 021 { 022 final List<Point2D> result = new ArrayList<Point2D>(); 023 024 final Point2D topLeft = new Point2D.Double(rectangle.getMinX(), rectangle.getMinY()); 025 final Point2D topRight = new Point2D.Double(rectangle.getMaxX(), rectangle.getMinY()); 026 final Point2D bottomRight = new Point2D.Double(rectangle.getMaxX(), rectangle.getMaxY()); 027 final Point2D bottomLeft = new Point2D.Double(rectangle.getMinX(), rectangle.getMaxY()); 028 Point2D intersection; 029 030 intersection = Line2DUtil.getIntersection(new Line2D.Double(topLeft, topRight), line, true, false); 031 if (intersection != null) 032 result.add(intersection); 033 intersection = Line2DUtil.getIntersection(new Line2D.Double(topRight, bottomRight), line, true, false); 034 if (intersection != null) 035 result.add(intersection); 036 intersection = Line2DUtil.getIntersection(new Line2D.Double(bottomRight, bottomLeft), line, true, false); 037 if (intersection != null) 038 result.add(intersection); 039 intersection = Line2DUtil.getIntersection(new Line2D.Double(bottomLeft, topLeft), line, true, false); 040 if (intersection != null) 041 result.add(intersection); 042 043 if (result.size() >= 2) 044 return new Line2D.Double(result.get(0), result.get(1)); 045 } 046 047 return null; 048 } 049 050 /** 051 * Returns a scaled form of the specified {@link Rectangle2D} by specified factor. 052 * 053 * @param rect 054 * the {@link Rectangle2D} to scale 055 * @param factor 056 * the scale factor 057 * @param centered 058 * if true then scaling is centered (rect location is modified) 059 * @param scalePosition 060 * if true then position is also rescaled (rect location is modified) 061 */ 062 public static Rectangle2D getScaledRectangle(Rectangle2D rect, double factor, boolean centered, 063 boolean scalePosition) 064 { 065 final Rectangle2D result = new Rectangle2D.Double(); 066 067 result.setFrame(rect); 068 ShapeUtil.scale(result, factor, centered, scalePosition); 069 070 return result; 071 } 072 073 /** 074 * Returns a scaled form of the specified {@link Rectangle2D} by specified factor. 075 * 076 * @param rect 077 * the {@link Rectangle2D} to scale 078 * @param factor 079 * the scale factor 080 * @param centered 081 * if true then scaling is centered (rect location is modified) 082 */ 083 public static Rectangle2D getScaledRectangle(Rectangle2D rect, double factor, boolean centered) 084 { 085 return getScaledRectangle(rect, factor, centered, false); 086 } 087}