下面列出了java.awt.geom.Rectangle2D#intersects ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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);
}
}
}
/**
* 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);
}
}
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;
}
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();
}
}
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;
}
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;
}
/**
* 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);
}
}
/**
* 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());
}
/**
* 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);
}
}
}
/**
* 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);
}
}
}
/**
* 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);
}
}
}
protected boolean outsideClip(final Graphics2D g) throws SVGException {
g.getClipBounds(clipBounds);
final Rectangle2D rect = getBoundingBox();
if (rect.intersects(clipBounds)) { return false; }
return true;
}
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;
}
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();
}
}
}
/**
* 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);
}
/**
* 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;
}
private boolean isSamplePointIntersectingWithOthers(SimplePlacemarkObject newPlacemark, SimplePlacemarkObject oldPlacemark) {
final Rectangle2D r1 = createRectangle(newPlacemark.getShape());
final Rectangle2D r2 = createRectangle(oldPlacemark.getShape());
return r2.intersects(r1);
}
/**
* 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);
}