1 package nl.tudelft.simulation.language.d2; 2 3 import org.djutils.draw.bounds.Bounds2d; 4 5 /** 6 * Shape utilities for world coordinates. 7 * <p> 8 * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See 9 * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL 10 * project is distributed under a three-clause BSD-style license, which can be found at 11 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank"> 12 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>. 13 * </p> 14 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a> 15 */ 16 public final class Shape2d 17 { 18 /** 19 * constructs a new Shape. 20 */ 21 private Shape2d() 22 { 23 // utility class. 24 } 25 26 /** 27 * overlaps extent and shape. Overlap = Intersect + Contain 28 * @param extent Bounds2d; the extent 29 * @param shape Bounds2d; the shape 30 * @return whether extent overlaps shape 31 */ 32 public static boolean overlaps(final Bounds2d extent, final Bounds2d shape) 33 { 34 if (extent.getMaxX() < shape.getMinX()) 35 { 36 return false; 37 } 38 if (extent.getMaxY() < shape.getMinY()) 39 { 40 return false; 41 } 42 if (extent.getMinX() > shape.getMaxX()) 43 { 44 return false; 45 } 46 if (extent.getMinY() > shape.getMaxY()) 47 { 48 return false; 49 } 50 return true; 51 } 52 53 /** 54 * do the rectangles really intersect (so, not being contained)? 55 * @param r1 Bounds2d; the first rectangle 56 * @param r2 Bounds2d; the second rectangle 57 * @return whether r1 really intersects r2 58 */ 59 public static boolean intersects(final Bounds2d r1, final Bounds2d r2) 60 { 61 return !Shape2d.contains(r1, r2) && !Shape2d.contains(r2, r1) && overlaps(r1, r2); 62 } 63 64 /** 65 * is r1 completely in r2. 66 * @param r1 Bounds2d; the first rectangle 67 * @param r2 Bounds2d; the second rectangle 68 * @return whether r1 in r2 69 */ 70 public static boolean contains(final Bounds2d r1, final Bounds2d r2) 71 { 72 boolean contains = (r2.getMinX() <= r1.getMinX() && r2.getMinY() <= r1.getMinY() && r2.getMaxX() >= r1.getMaxX() 73 && r2.getMaxY() >= r1.getMaxY()); 74 return contains; 75 } 76 }