1 package org.feistymeow.algorithms;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Comparator;
6 import java.util.Vector;
18 public static class Point
20 Point(
double x,
double y)
29 public static class Rectangle
31 Rectangle(Point ul, Point lr)
42 return p.x <= r.lr.x && p.x >= r.ul.x && p.y <= r.lr.y && p.y >= r.ul.y;
56 for (
int i = 0; i < list.size(); i++) {
57 for (
int j = i; j < list.size(); j++) {
59 ArrayList<Rectangle> toReturn =
new ArrayList<Rectangle>();
60 toReturn.add(list.get(i));
61 toReturn.add(list.get(j));
68 public static class SortedElement
74 public SortedElement(
double value,
boolean lowerEdge, Rectangle source)
77 this.lowerEdge = lowerEdge;
82 public static class SortedElementComparator
implements Comparator<SortedElement>
85 public int compare(SortedElement k1, SortedElement k2)
87 SortedElementKey key1 =
new SortedElementKey(k1.value);
88 return key1.compareTo(
new SortedElementKey(k2.value));
94 public static class SortedElementKey
implements Comparable<SortedElementKey>
96 public SortedElementKey(
double key)
102 public int compareTo(SortedElementKey k2)
104 return (key == k2.key) ? 0 : (key < k2.key) ? -1 : 1;
115 public static Vector<Rectangle>
findOverlap(Vector<Rectangle> list)
119 ArrayList<SortedElement> xEdges =
new ArrayList<SortedElement>();
120 for (Rectangle r : list) {
121 xEdges.add(
new SortedElement(r.lr.x,
true, r));
122 xEdges.add(
new SortedElement(r.ul.x,
false, r));
126 xEdges.sort(
new SortedElementComparator());
131 BinarySearchTree<SortedElementKey, SortedElement> bst =
new BinarySearchTree<SortedElementKey, SortedElement>();
133 for (SortedElement scanner : xEdges) {
134 Rectangle source = scanner.source;
135 if (scanner.lowerEdge) {
148 bst.insert(
new SortedElementKey(source.lr.y),
new SortedElement(source.lr.y,
true, source));
149 bst.insert(
new SortedElementKey(source.ul.y),
new SortedElement(source.ul.y,
false, source));
153 bst.delete(
new SortedElementKey(source.lr.y));
154 bst.delete(
new SortedElementKey(source.ul.y));
168 public static void main(String[] argv)
170 Rectangle r1 =
new Rectangle(
new Point(0, 0),
new Point(1, 1));
171 Rectangle r2 =
new Rectangle(
new Point(3, 2),
new Point(4, 3));
172 Rectangle r3 =
new Rectangle(
new Point(2, 3),
new Point(3, 4));
173 Rectangle r4 =
new Rectangle(
new Point(5, 6),
new Point(7, 9));
175 Vector<Rectangle> list1 =
new Vector<Rectangle>(Arrays.asList(r1, r2, r3));
176 Vector<Rectangle> list2 =
new Vector<Rectangle>(Arrays.asList(r1, r2));
177 Vector<Rectangle> list3 =
new Vector<Rectangle>(Arrays.asList(r1, r3, r4));
185 if (answer1 ==
null) {
186 System.out.println(
"FAILURE: test 1 did not find intersection in list");
188 System.out.println(
"OKAY: test 1 found intersections " + answer1.get(0) +
" " + answer1.get(1));
190 if (answer2 !=
null) {
191 System.out.println(
"FAILURE: test 2 found an intersection in list that has none");
193 System.out.println(
"OKAY: test 2 no intersections found");
195 if (answer3 !=
null) {
196 System.out.println(
"FAILURE: test 3 found an intersection in list that has none");
198 System.out.println(
"OKAY: test 3 no intersections found");
static Vector< Rectangle > findOverlap(Vector< Rectangle > list)
static boolean doesPointOverlap(Point p, Rectangle r)
static boolean doRectanglesOverlap(Rectangle r1, Rectangle r2)
static void main(String[] argv)
static Vector< Rectangle > findOverlapBruteForce(Vector< Rectangle > list)