Fix rounding errors leading to stone sets being dropped at strage positions on the table
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@238 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
27d72ccb61
commit
f7f47d0072
1 changed files with 42 additions and 31 deletions
|
@ -11,7 +11,7 @@ import jrummikub.util.Pair;
|
||||||
* or {@link StoneSet}s.
|
* or {@link StoneSet}s.
|
||||||
*
|
*
|
||||||
* @param <E>
|
* @param <E>
|
||||||
* Type of positioned objects (must implement Sizeable)
|
* Type of positioned objects (must implement Sizeable)
|
||||||
*/
|
*/
|
||||||
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
|
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
|
||||||
|
@ -50,8 +50,7 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
if (currentObject == object)
|
if (currentObject == object)
|
||||||
continue;
|
continue;
|
||||||
Position currentPosition = getPosition(currentObject);
|
Position currentPosition = getPosition(currentObject);
|
||||||
if (!objectsOverlap(object, position, currentObject,
|
if (!objectsOverlap(object, position, currentObject, currentPosition)) {
|
||||||
currentPosition)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Object would be placed inside the current object
|
// Object would be placed inside the current object
|
||||||
|
@ -61,22 +60,23 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
Position newPosition = null;
|
Position newPosition = null;
|
||||||
// Move object to avoid overlap
|
// Move object to avoid overlap
|
||||||
switch (newDirection) {
|
switch (newDirection) {
|
||||||
case TOP:
|
case TOP:
|
||||||
newPosition = new Position(currentPosition.getX(),
|
newPosition = new Position(currentPosition.getX(), position.getY()
|
||||||
position.getY() - currentObject.getHeight());
|
- currentObject.getHeight());
|
||||||
break;
|
break;
|
||||||
case BOTTOM:
|
case BOTTOM:
|
||||||
newPosition = new Position(currentPosition.getX(),
|
newPosition = new Position(currentPosition.getX(), position.getY()
|
||||||
position.getY() + object.getHeight());
|
+ object.getHeight());
|
||||||
break;
|
break;
|
||||||
case LEFT:
|
case LEFT:
|
||||||
newPosition = new Position(position.getX()
|
newPosition = new Position(
|
||||||
- currentObject.getWidth(), currentPosition.getY());
|
position.getX() - currentObject.getWidth(),
|
||||||
break;
|
currentPosition.getY());
|
||||||
case RIGHT:
|
break;
|
||||||
newPosition = new Position(position.getX() + object.getWidth(),
|
case RIGHT:
|
||||||
currentPosition.getY());
|
newPosition = new Position(position.getX() + object.getWidth(),
|
||||||
break;
|
currentPosition.getY());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
objects.remove(currentObject);
|
objects.remove(currentObject);
|
||||||
|
@ -85,14 +85,14 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the object may be placed on the given position, computes
|
* Checks whether the object may be placed on the given position, computes new
|
||||||
* new position if not
|
* position if not
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* to be dropped
|
* to be dropped
|
||||||
* @param dir
|
* @param dir
|
||||||
* @param pos
|
* @param pos
|
||||||
* the object is dropped at
|
* the object is dropped at
|
||||||
* @return null if the drop is valid, new position otherwise
|
* @return null if the drop is valid, new position otherwise
|
||||||
*/
|
*/
|
||||||
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
|
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
|
||||||
|
@ -100,20 +100,33 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean lessOrEqual(float x, float y) {
|
||||||
|
if (-0.000001f < y && y < 0.000001f) {
|
||||||
|
return (x < y + 0.000001f);
|
||||||
|
}
|
||||||
|
|
||||||
|
float q = x / y;
|
||||||
|
if (0.999999f < q && q < 1.000001f) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x < y;
|
||||||
|
}
|
||||||
|
|
||||||
/** Tests whether two objects overlap **/
|
/** Tests whether two objects overlap **/
|
||||||
private boolean objectsOverlap(E object1, Position position1, E object2,
|
private boolean objectsOverlap(E object1, Position position1, E object2,
|
||||||
Position position2) {
|
Position position2) {
|
||||||
// Tests if position is left of, above ... the current object
|
// Tests if position is left of, above ... the current object
|
||||||
if (position1.getX() + object1.getWidth() <= position2.getX()) {
|
if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (position1.getY() + object1.getHeight() <= position2.getY()) {
|
if (lessOrEqual(position1.getY() + object1.getHeight(), position2.getY())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (position1.getX() >= position2.getX() + object2.getWidth()) {
|
if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (position1.getY() >= position2.getY() + object2.getHeight()) {
|
if (lessOrEqual(position2.getY() + object2.getHeight(), position1.getY())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -149,15 +162,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
float blockingRight = blocking.getSecond().getX()
|
float blockingRight = blocking.getSecond().getX()
|
||||||
+ blocking.getFirst().getWidth();
|
+ blocking.getFirst().getWidth();
|
||||||
float overlapRight = Math.min(objectRight, blockingRight);
|
float overlapRight = Math.min(objectRight, blockingRight);
|
||||||
float overlapLeft = Math.max(position.getX(), blocking.getSecond()
|
float overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
|
||||||
.getX());
|
|
||||||
float overlapX = overlapRight - overlapLeft;
|
float overlapX = overlapRight - overlapLeft;
|
||||||
float objectBottom = position.getY() + object.getHeight();
|
float objectBottom = position.getY() + object.getHeight();
|
||||||
float blockingBottom = blocking.getSecond().getY()
|
float blockingBottom = blocking.getSecond().getY()
|
||||||
+ blocking.getFirst().getHeight();
|
+ blocking.getFirst().getHeight();
|
||||||
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||||
float overlapTop = Math.max(position.getY(), blocking.getSecond()
|
float overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
|
||||||
.getY());
|
|
||||||
float overlapY = overlapBottom - overlapTop;
|
float overlapY = overlapBottom - overlapTop;
|
||||||
// vertical or horizontal Shift
|
// vertical or horizontal Shift
|
||||||
// TODO magic factor
|
// TODO magic factor
|
||||||
|
|
Reference in a new issue