@Test public void oneObstacleShiftLeft() { Rectangle r = new Rectangle(0, 0, 10, 100); ArrayList<Rectangle> obs = new ArrayList<>(); obs.add(new Rectangle(-2, 50, 13, 1)); Rectangle pos = new AvoidObstacles().findNewPosition(r, obs); assertEquals(new Rectangle(11, 0, 10, 100), pos); }
public Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles) { List<Vector> horObs = new ArrayList<>(); List<Vector> verObs = new ArrayList<>(); for (Rectangle o: obstacles) { if (o.innerIntersects(rect)) { horObs.add(new Vector(o.origin.x, o.origin.add(o.dimension).x)); verObs.add(new Vector(o.origin.y, o.origin.add(o.dimension).y)); } } if (horObs.size() == 0) { return rect; } Vector min = new Vector(Integer.MIN_VALUE / 2, Integer.MIN_VALUE / 2); Vector max = new Vector(Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2); horObs.add(min); horObs.add(max); verObs.add(min); verObs.add(max); Comparator<Vector> comparator = new Comparator<Vector>() { @Override public int compare(Vector v1, Vector v2) { if (v1.x < v2.x) return -1; if (v1.x > v2.x) return 1; return 0; } }; Collections.sort(horObs, comparator); int bestHor = findShift(new Vector(rect.origin.x, rect.origin.x + rect.dimension.x), horObs); int bestVer = findShift(new Vector(rect.origin.y, rect.origin.y + rect.dimension.y), verObs); Vector shift; if (Math.abs(bestHor) < Math.abs(bestVer)) { shift = new Vector(bestHor, 0); } else { shift = new Vector(0, bestVer); } return rect.add(shift); }
AvoidObstacles { public Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles) { List<Vector> horObs = new ArrayList<>(); List<Vector> verObs = new ArrayList<>(); for (Rectangle o: obstacles) { if (o.innerIntersects(rect)) { horObs.add(new Vector(o.origin.x, o.origin.add(o.dimension).x)); verObs.add(new Vector(o.origin.y, o.origin.add(o.dimension).y)); } } if (horObs.size() == 0) { return rect; } Vector min = new Vector(Integer.MIN_VALUE / 2, Integer.MIN_VALUE / 2); Vector max = new Vector(Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2); horObs.add(min); horObs.add(max); verObs.add(min); verObs.add(max); Comparator<Vector> comparator = new Comparator<Vector>() { @Override public int compare(Vector v1, Vector v2) { if (v1.x < v2.x) return -1; if (v1.x > v2.x) return 1; return 0; } }; Collections.sort(horObs, comparator); int bestHor = findShift(new Vector(rect.origin.x, rect.origin.x + rect.dimension.x), horObs); int bestVer = findShift(new Vector(rect.origin.y, rect.origin.y + rect.dimension.y), verObs); Vector shift; if (Math.abs(bestHor) < Math.abs(bestVer)) { shift = new Vector(bestHor, 0); } else { shift = new Vector(0, bestVer); } return rect.add(shift); } }
AvoidObstacles { public Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles) { List<Vector> horObs = new ArrayList<>(); List<Vector> verObs = new ArrayList<>(); for (Rectangle o: obstacles) { if (o.innerIntersects(rect)) { horObs.add(new Vector(o.origin.x, o.origin.add(o.dimension).x)); verObs.add(new Vector(o.origin.y, o.origin.add(o.dimension).y)); } } if (horObs.size() == 0) { return rect; } Vector min = new Vector(Integer.MIN_VALUE / 2, Integer.MIN_VALUE / 2); Vector max = new Vector(Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2); horObs.add(min); horObs.add(max); verObs.add(min); verObs.add(max); Comparator<Vector> comparator = new Comparator<Vector>() { @Override public int compare(Vector v1, Vector v2) { if (v1.x < v2.x) return -1; if (v1.x > v2.x) return 1; return 0; } }; Collections.sort(horObs, comparator); int bestHor = findShift(new Vector(rect.origin.x, rect.origin.x + rect.dimension.x), horObs); int bestVer = findShift(new Vector(rect.origin.y, rect.origin.y + rect.dimension.y), verObs); Vector shift; if (Math.abs(bestHor) < Math.abs(bestVer)) { shift = new Vector(bestHor, 0); } else { shift = new Vector(0, bestVer); } return rect.add(shift); }  }
AvoidObstacles { public Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles) { List<Vector> horObs = new ArrayList<>(); List<Vector> verObs = new ArrayList<>(); for (Rectangle o: obstacles) { if (o.innerIntersects(rect)) { horObs.add(new Vector(o.origin.x, o.origin.add(o.dimension).x)); verObs.add(new Vector(o.origin.y, o.origin.add(o.dimension).y)); } } if (horObs.size() == 0) { return rect; } Vector min = new Vector(Integer.MIN_VALUE / 2, Integer.MIN_VALUE / 2); Vector max = new Vector(Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2); horObs.add(min); horObs.add(max); verObs.add(min); verObs.add(max); Comparator<Vector> comparator = new Comparator<Vector>() { @Override public int compare(Vector v1, Vector v2) { if (v1.x < v2.x) return -1; if (v1.x > v2.x) return 1; return 0; } }; Collections.sort(horObs, comparator); int bestHor = findShift(new Vector(rect.origin.x, rect.origin.x + rect.dimension.x), horObs); int bestVer = findShift(new Vector(rect.origin.y, rect.origin.y + rect.dimension.y), verObs); Vector shift; if (Math.abs(bestHor) < Math.abs(bestVer)) { shift = new Vector(bestHor, 0); } else { shift = new Vector(0, bestVer); } return rect.add(shift); }  Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles); int findShift(Vector v, List<Vector> obs); }
AvoidObstacles { public Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles) { List<Vector> horObs = new ArrayList<>(); List<Vector> verObs = new ArrayList<>(); for (Rectangle o: obstacles) { if (o.innerIntersects(rect)) { horObs.add(new Vector(o.origin.x, o.origin.add(o.dimension).x)); verObs.add(new Vector(o.origin.y, o.origin.add(o.dimension).y)); } } if (horObs.size() == 0) { return rect; } Vector min = new Vector(Integer.MIN_VALUE / 2, Integer.MIN_VALUE / 2); Vector max = new Vector(Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2); horObs.add(min); horObs.add(max); verObs.add(min); verObs.add(max); Comparator<Vector> comparator = new Comparator<Vector>() { @Override public int compare(Vector v1, Vector v2) { if (v1.x < v2.x) return -1; if (v1.x > v2.x) return 1; return 0; } }; Collections.sort(horObs, comparator); int bestHor = findShift(new Vector(rect.origin.x, rect.origin.x + rect.dimension.x), horObs); int bestVer = findShift(new Vector(rect.origin.y, rect.origin.y + rect.dimension.y), verObs); Vector shift; if (Math.abs(bestHor) < Math.abs(bestVer)) { shift = new Vector(bestHor, 0); } else { shift = new Vector(0, bestVer); } return rect.add(shift); }  Rectangle findNewPosition(Rectangle rect, List<Rectangle> obstacles); int findShift(Vector v, List<Vector> obs);  }