Update quad rect for flippable rects as well

This commit is contained in:
Victor Shcherb 2021-03-18 14:06:25 +01:00
parent 2e04c460b3
commit bc1f18fcb4
2 changed files with 19 additions and 19 deletions

View file

@ -6,6 +6,7 @@ public class QuadRect {
public double top; public double top;
public double bottom; public double bottom;
// left & right / top & bottom could be flipped (so it's useful for latlon bbox)
public QuadRect(double left, double top, double right, double bottom) { public QuadRect(double left, double top, double right, double bottom) {
this.left = left; this.left = left;
this.right = right; this.right = right;
@ -21,16 +22,18 @@ public class QuadRect {
} }
public double width() { public double width() {
return right - left; return Math.abs(right - left);
} }
public double height() { public double height() {
return bottom - top; return Math.abs(bottom - top);
} }
public boolean contains(double left, double top, double right, double bottom) { public boolean contains(double left, double top, double right, double bottom) {
return this.left < this.right && this.top < this.bottom && this.left <= left && this.top <= top && this.right >= right return Math.min(this.left, this.right) <= Math.min(left, right)
&& this.bottom >= bottom; && Math.max(this.left, this.right) >= Math.max(left, right)
&& Math.min(this.top, this.bottom) <= Math.min(top, bottom)
&& Math.max(this.top, this.bottom) <= Math.max(top, bottom);
} }
public boolean contains(QuadRect box) { public boolean contains(QuadRect box) {
@ -38,11 +41,10 @@ public class QuadRect {
} }
public static boolean intersects(QuadRect a, QuadRect b) { public static boolean intersects(QuadRect a, QuadRect b) {
return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom; return Math.min(a.left, a.right) < Math.max(b.left, b.right)
} && Math.max(a.left, a.right) > Math.min(b.left, b.right)
&& Math.min(a.bottom, a.top) < Math.max(b.bottom, b.top)
public static boolean trivialOverlap(QuadRect a, QuadRect b) { && Math.max(a.bottom, a.top) > Math.min(b.bottom, b.top);
return !((a.right < b.left) || (a.left > b.right) || (a.top < b.bottom) || (a.bottom > b.top));
} }
public double centerX() { public double centerX() {

View file

@ -103,18 +103,16 @@ public class QuadTree<T> {
void splitBox(QuadRect node_extent, QuadRect[] n) { void splitBox(QuadRect node_extent, QuadRect[] n) {
// coord2d c=node_extent.center(); // coord2d c=node_extent.center();
double width = node_extent.width();
double height = node_extent.height();
double lox = node_extent.left; double lx = node_extent.left;
double loy = node_extent.top; double ly = node_extent.top;
double hix = node_extent.right; double hx = node_extent.right;
double hiy = node_extent.bottom; double hy = node_extent.bottom;
n[0] = new QuadRect(lox, loy, lox + width * ratio, loy + height * ratio); n[0] = new QuadRect(lx, ly, lx + (hx - lx) * ratio, ly + (hy - ly) * ratio);
n[1] = new QuadRect(hix - width * ratio, loy, hix, loy + height * ratio); n[1] = new QuadRect(lx + (hx - lx) * (1 - ratio), ly, hx, ly + (hy - ly) * ratio);
n[2] = new QuadRect(lox, hiy - height * ratio, lox + width * ratio, hiy); n[2] = new QuadRect(lx, ly + (hy - ly) * (1 - ratio), lx + (hx - lx) * ratio, hy);
n[3] = new QuadRect(hix - width * ratio, hiy - height * ratio, hix, hiy); n[3] = new QuadRect(lx + (hx - lx) * (1 - ratio), ly + (hy - ly) * (1 - ratio), hx, hy);
} }
} }