Java Code Examples for java.awt.geom.Rectangle2D#intersects()

The following examples show how to use java.awt.geom.Rectangle2D#intersects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StendhalRPZone.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
public void notifyBeforeMovement(final ActiveEntity entity, final int oldX, final int oldY,
		final int newX, final int newY) {
	Rectangle2D neArea;
	boolean newIn;

	neArea = entity.getArea(newX, newY);

	for (final MovementListener l : movementListeners) {
		Rectangle2D area = l.getArea();

		newIn = area.intersects(neArea);

		if (newIn) {
			l.beforeMove(entity, this, oldX, oldY, newX, newY);
		}

	}
}
 
Example 2
Source File: StendhalRPZone.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
private Entity getCollidingObject(final Entity entity, final Rectangle2D area) {
	for (final RPObject other : objects.values()) {
		// Ignore same object
		if (entity != other) {
			final Entity otherEntity = (Entity) other;

			// Check if the objects overlap
			if (area.intersects(otherEntity.getX(), otherEntity.getY(), otherEntity.getWidth(), otherEntity.getHeight())) {
				// Check if it's blocking
				if (otherEntity.isObstacle(entity)) {
					return otherEntity;
				}
			}
		}
	}

	return null;
}
 
Example 3
Source File: StyledSegment.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the rectangular intersects the shape of the segment.
 * 
 * @param rec The rectangular you want to check
 * @return Returns true if the rectangular intersects the shape of the segment, false otherwise
 */
public boolean intersects(Rectangle2D rec) {
	
	//If segment is not printed at all the user can't select it
	if (!style.isPrintable()) {
		
		return false;
	}
	
	//Make sure to check whether its a lined shape or a rectangular one
	return (style.getShape() == Shapes.line) ? rec.intersectsLine(x, y, x+width, y+height) : rec.intersects(x, y, width, height);
}
 
Example 4
Source File: GisFeatureRenderer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Draws all the features that are within the graphics clip rectangle,
 * using the previously set displayProjection.
 *
 * @param g the Graphics2D context on which to draw
 * @param pixelAT transforms "Normalized Device" to Device coordinates
 */
public void draw(java.awt.Graphics2D g, AffineTransform pixelAT) {
  g.setColor(color);
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  g.setStroke(new java.awt.BasicStroke(0.0f));

  Rectangle2D clipRect = (Rectangle2D) g.getClip();
  Iterator siter = getShapes(g, pixelAT);
  while (siter.hasNext()) {
    Shape s = (Shape) siter.next();
    Rectangle2D shapeBounds = s.getBounds2D();
    if (shapeBounds.intersects(clipRect))
      g.draw(s);
  }
}
 
Example 5
Source File: SquaresTransition2D.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
protected float findMax(float t0, float t1) {
    while (true) {
        if (t1 - t0 < 0.0001) {
            return Math.max(t0, t1);
        }

        Rectangle2D r = new Rectangle2D.Float(0, 0, 100, 100);
        float mid = t0 / 2.0f + t1 / 2.0f;
        Transition2DInstruction[] instrA = getInstructions(t0, new Dimension(100, 100));
        Transition2DInstruction[] instrB = getInstructions(mid, new Dimension(100, 100));
        Transition2DInstruction[] instrC = getInstructions(t1, new Dimension(100, 100));
        boolean validA = false;
        boolean validB = false;
        boolean validC = false;
        for (int a = 1; a < instrA.length; a++) {
            if (r.intersects((Rectangle2D) ((ImageInstruction) instrA[a]).clipping)) {
                validA = true;
            }
            if (r.intersects((Rectangle2D) ((ImageInstruction) instrB[a]).clipping)) {
                validB = true;
            }
            if (r.intersects((Rectangle2D) ((ImageInstruction) instrC[a]).clipping)) {
                validC = true;
            }
        }
        if (validA && validC) {
            return Math.max(t0, t1);
        }
        if (validA) {
            if (validB) {
                t0 = mid;
            } else {
                t1 = mid;
            }
        } else {
            throw new RuntimeException();
        }
    }
}
 
Example 6
Source File: MultiThumbSliderUI.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
private boolean isOverlap(int thumbIndexA,int thumbIndexB,float newThumbBPosition) {
	if(thumbIndexA==thumbIndexB) return false;
	if(!slider.isThumbOverlap()) {
		Point2D aCenter = getThumbCenter(positions[thumbIndexA]);
		Point2D bCenter = getThumbCenter(newThumbBPosition);
		Rectangle2D aBounds = ShapeBounds.getBounds( getThumbShape(thumbIndexA, aCenter) );
		Rectangle2D bBounds = ShapeBounds.getBounds( getThumbShape(thumbIndexB, bCenter) );
		return aBounds.intersects(bBounds) || aBounds.equals(bBounds);
	}
	return false;
}
 
Example 7
Source File: Group.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
protected boolean outsideClip(final Graphics2D g) throws SVGException {
	g.getClipBounds(clipBounds);
	final Rectangle2D rect = getBoundingBox();
	if (rect.intersects(clipBounds)) { return false; }

	return true;
}
 
Example 8
Source File: StendhalRPZone.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notify anything interested that an entity moved.
 *
 * @param entity
 *            The entity that moved.
 * @param oldX
 *            The old X coordinate.
 * @param oldY
 *            The old Y coordinate.
 * @param newX
 *            The new X coordinate.
 * @param newY
 *            The new Y coordinate.
 */
public void notifyMovement(final ActiveEntity entity, final int oldX, final int oldY,
		final int newX, final int newY) {
	Rectangle2D oeArea;
	Rectangle2D neArea;
	boolean oldIn;
	boolean newIn;

	oeArea = entity.getArea(oldX, oldY);
	neArea = entity.getArea(newX, newY);

	for (final MovementListener l : movementListeners) {
		Rectangle2D area = l.getArea();

		oldIn = area.intersects(oeArea);
		newIn = area.intersects(neArea);

		if (!oldIn && newIn) {
			l.onEntered(entity, this, newX, newY);
		}

		if (oldIn && newIn) {
			l.onMoved(entity, this, oldX, oldY, newX, newY);
		}

		if (oldIn && !newIn) {
			l.onExited(entity, this, oldX, oldY);
		}
	}
}
 
Example 9
Source File: StendhalRPZone.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notify anything interested in when an entity exited.
 *
 * @param entity
 *            The entity that moved.
 * @param oldX
 *            The old X coordinate.
 * @param oldY
 *            The old Y coordinate.
 */
public void notifyExited(final ActiveEntity entity, final int oldX, final int oldY) {
	Rectangle2D eArea;

	eArea = entity.getArea(oldX, oldY);

	for (final MovementListener l : movementListeners) {
		Rectangle2D area = l.getArea();
		if (area.intersects(eArea)) {
			l.onExited(entity, this, oldX, oldY);
		}
	}
}
 
Example 10
Source File: StendhalRPZone.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notify anything interested in when an entity entered.
 *
 * @param entity
 *            The entity that entered.
 * @param newX
 *            The new X coordinate.
 * @param newY
 *            The new Y coordinate.
 */
public void notifyEntered(final ActiveEntity entity, final int newX, final int newY) {
	Rectangle2D eArea;

	eArea = entity.getArea(newX, newY);

	for (final MovementListener l : movementListeners) {
		Rectangle2D area = l.getArea();
		if (area.intersects(eArea)) {
			l.onEntered(entity, this, newX, newY);
		}
	}
}
 
Example 11
Source File: Entity.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks whether the given entity is near this entity.
 *
 * @param entity
 *            the entity
 * @param step
 *            The maximum distance
 * @return true iff the entity is at most <i>step</i> steps away
 */
public boolean nextTo(final Entity entity, final double step) {
	// To check the overlapping between 'this' and the other 'entity'
	// we create two temporary rectangle objects and initialise them
	// with the position of the two entities.
	// The size is calculated from the original objects with the additional
	// 'step' distance on both sides of the two rectangles.
	// As the absolute position is not important, 'step' need not be
	// subtracted from the values of getX() and getY().
	final Rectangle2D thisArea = new Rectangle2D.Double(x - step, y - step, area.getWidth()
			+ 2 * step, area.getHeight() + 2 * step);

	return thisArea.intersects(entity.getArea());
}
 
Example 12
Source File: OccupantArea.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is called when the turn number is reached.
 *
 * @param currentTurn
 *            Current turn number.
 */
@Override
public void onTurnReached(final int currentTurn) {
	IRPZone zone;
	Rectangle2D area;

	zone = getZone();
	area = getArea();

	/*
	 * Perform action on entities still in the area. Remove those that have
	 * gone missing.
	 */
	final Iterator<RPEntity.ID> iter = targets.iterator();

	while (iter.hasNext()) {
		final RPEntity.ID id = iter.next();

		if (zone.has(id)) {
			final RPEntity entity = (RPEntity) zone.get(id);

			if (area.intersects(entity.getArea())) {
				if (!handleInterval(entity)) {
					handleRemoved(entity);
					iter.remove();
				}
			} else {
				handleRemoved(entity);
				iter.remove();
			}
		} else {
			iter.remove();
		}
	}

	if (!targets.isEmpty()) {
		SingletonRepository.getTurnNotifier().notifyInTurns(interval, this);
	}
}
 
Example 13
Source File: GameObjects.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
public boolean collides(final IEntity entity) {
	if (entity instanceof Player) {
		final Player player = (Player) entity;

		if (player.isGhostMode() || player.ignoresCollision()) {
			return false;
		}
	}

	if (entity instanceof NPC) {
		final NPC npc = (NPC) entity;

		if (npc.ignoresCollision()) {
			return false;
		}
	}

	final Rectangle2D area = entity.getArea();

	if (collisionMap.collides(area)) {
		return true;
	}

	for (final IEntity other : objects.values()) {
		if (other.isObstacle(entity) && area.intersects(other.getArea())) {
			return true;
		}
	}

	return false;
}
 
Example 14
Source File: MultiThumbSliderUI.java    From pumpernickel with MIT License 5 votes vote down vote up
private boolean isOverlap(int thumbIndexA, int thumbIndexB,
		float newThumbBPosition) {
	if (thumbIndexA == thumbIndexB)
		return false;
	if (!slider.isThumbOverlap()) {
		Point2D aCenter = getThumbCenter(positions[thumbIndexA]);
		Point2D bCenter = getThumbCenter(newThumbBPosition);
		Rectangle2D aBounds = ShapeBounds.getBounds(getThumbShape(
				thumbIndexA, aCenter));
		Rectangle2D bBounds = ShapeBounds.getBounds(getThumbShape(
				thumbIndexB, bCenter));
		return aBounds.intersects(bBounds) || aBounds.equals(bBounds);
	}
	return false;
}
 
Example 15
Source File: SquaresTransition2D.java    From pumpernickel with MIT License 5 votes vote down vote up
protected float findMax(float t0, float t1) {
	if (t1 - t0 < .0001)
		return Math.max(t0, t1);

	Rectangle2D r = new Rectangle2D.Float(0, 0, 100, 100);
	float mid = t0 / 2f + t1 / 2f;
	Transition2DInstruction[] instrA = getInstructions(t0, new Dimension(
			100, 100));
	Transition2DInstruction[] instrB = getInstructions(mid, new Dimension(
			100, 100));
	Transition2DInstruction[] instrC = getInstructions(t1, new Dimension(
			100, 100));
	boolean validA = false;
	boolean validB = false;
	boolean validC = false;
	for (int a = 1; a < instrA.length; a++) {
		if (r.intersects((Rectangle2D) ((ImageInstruction) instrA[a]).clipping)) {
			validA = true;
		}
		if (r.intersects((Rectangle2D) ((ImageInstruction) instrB[a]).clipping)) {
			validB = true;
		}
		if (r.intersects((Rectangle2D) ((ImageInstruction) instrC[a]).clipping)) {
			validC = true;
		}
	}
	if (validA && validC)
		return Math.max(t0, t1);
	if (validA) {
		if (validB) {
			return findMax(mid, t1);
		} else {
			return findMax(t0, mid);
		}
	} else {
		throw new RuntimeException();
	}
}
 
Example 16
Source File: TouchableHelper.java    From workcraft with MIT License 5 votes vote down vote up
public static boolean touchesRectangle(Touchable node, Rectangle2D rect) {
    Rectangle2D boundingBox = node.getBoundingBox();
    if ((boundingBox != null) && rect.intersects(boundingBox)) {
        if (node instanceof VisualConnection) {
            VisualConnection connection = (VisualConnection) node;
            return rect.contains(boundingBox) || !connection.getIntersections(rect).isEmpty();
        }
        return true;
    }
    return false;
}
 
Example 17
Source File: LocalAreaUtil.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
	 * Checks if a path collides with an existing building, construction site, or
	 * vehicle at a location.
	 * 
	 * @param object      the object being checked (may be null if no object).
	 * @param path        the path to check.
	 * @param coordinates the global coordinate location to check.
	 * @param useCache    true if caching should be used.
	 * @return true if path doesn't collide with anything.
	 */
	private static boolean isPathCollisionFree(Object object, Path2D path, Coordinates coordinates, boolean useCache) {

		boolean result = true;

		// Check if obstacle area has been cached for this coordinate location if using
		// cache.
		boolean cached = false;
		Area obstacleArea = null;
		if (useCache && obstacleAreaCache.containsKey(coordinates)) {
			if (marsClock == null)
				marsClock = Simulation.instance().getMasterClock().getMarsClock();
			String currentTimestamp = marsClock.getDateTimeStamp();
			String cachedTimestamp = obstacleAreaTimestamps.get(coordinates);
			if (currentTimestamp.equals(cachedTimestamp)) {
				cached = true;
				obstacleArea = obstacleAreaCache.get(coordinates);
			}
		}

		if (!cached) {
			// Add all obstacle areas at location together to create a total obstacle area.
			Iterator<LocalBoundedObject> i = getAllLocalBoundedObjectsAtLocation(coordinates).iterator();
			while (i.hasNext()) {
				LocalBoundedObject lbo = i.next();
				if (lbo != object) {
					Rectangle2D objectRect = new Rectangle2D.Double(lbo.getXLocation() - (lbo.getWidth() / 2D),
							lbo.getYLocation() - (lbo.getLength() / 2D), lbo.getWidth(), lbo.getLength());
					Path2D objectPath = getPathFromRectangleRotation(objectRect, lbo.getFacing());
					Area objectArea = new Area(objectPath);
					if (obstacleArea == null) {
						obstacleArea = objectArea;
					} else {
						obstacleArea.add(objectArea);
					}
				}
			}
		}

		if (obstacleArea != null) {
			// Check for intersection of obstacle and path bounding rectangles first
			// (faster).
			Rectangle2D pathBounds = path.getBounds2D();
			Rectangle2D obstacleBounds = obstacleArea.getBounds2D();
			if (pathBounds.intersects(obstacleBounds)) {
				// If rectangles intersect, check for collision of path and obstacle areas
				// (slower).
				Area pathArea = new Area(path);
				result = !doAreasCollide(pathArea, obstacleArea);
			}
		}

		// Store cached obstacle area for location with current timestamp if needed.
		if (useCache && !cached && (obstacleArea != null)) {
			obstacleAreaCache.put(coordinates, obstacleArea);
//			String currentTimestamp = marsClock.getDateTimeStamp();
			obstacleAreaTimestamps.put(coordinates, marsClock.getDateTimeStamp());
		}

		return result;
	}
 
Example 18
Source File: CircleKmlGenerator.java    From collect-earth with MIT License 4 votes vote down vote up
private boolean isSamplePointIntersectingWithOthers(SimplePlacemarkObject newPlacemark, SimplePlacemarkObject oldPlacemark) {
	final Rectangle2D r1 = createRectangle(newPlacemark.getShape());
	final Rectangle2D r2 = createRectangle(oldPlacemark.getShape());
	return r2.intersects(r1);
}
 
Example 19
Source File: StendhalRPZone.java    From stendhal with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Determine if this zone overlaps an area in global coordinates.
 *
 * @param area
 *            The area (in global coordinate space).
 *
 * @return <code>true</code> if the area overlaps.
 */
public boolean intersects(final Rectangle2D area) {
	final Rectangle2D zone = new Rectangle(x, y, getWidth(), getHeight());

	return zone.intersects(area);
}