Skip to content

Commit 3cb0490

Browse files
authored
Merge pull request #92 from davidmoten/intersect-with-zero-area-rect
fix intersection with zero area rectangles
2 parents 6798826 + fed4e01 commit 3cb0490

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
language: java
2-
dist: precise
32
jdk:
4-
- oraclejdk8
5-
- oraclejdk7
6-
- openjdk7
7-
- openjdk6
3+
- openjdk8
84

95
# Workaround for buffer overflow with openjdk6
106
# https://github.com/travis-ci/travis-ci/issues/5227

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@
160160
<optional>true</optional>
161161
</dependency>
162162

163+
<dependency>
164+
<groupId>javax.annotation</groupId>
165+
<artifactId>javax.annotation-api</artifactId>
166+
<version>1.3.2</version>
167+
<optional>true</optional>
168+
</dependency>
169+
163170
</dependencies>
164171

165172
<profiles>

src/main/java/com/github/davidmoten/rtree/internal/RectangleUtil.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.davidmoten.rtree.internal;
22

3+
import java.awt.geom.Line2D;
4+
35
public final class RectangleUtil {
46

57
private RectangleUtil() {
@@ -37,8 +39,14 @@ private RectangleUtil() {
3739
* @since 1.2
3840
*/
3941
public static final int OUT_BOTTOM = 8;
40-
41-
public static boolean rectangleIntersectsLine(double rectX, double rectY, double rectWidth,
42+
43+
public static boolean rectangleIntersectsLine(double rectX, double rectY, double rectWidth, double rectHeight,
44+
double x1, double y1, double x2, double y2) {
45+
return _rectangleIntersectsLine(rectX, rectY, rectWidth, rectHeight, x1, y1, x2, y2)
46+
|| Line2D.Double.linesIntersect(rectX, rectY, rectX + rectWidth, rectY + rectHeight, x1, y1, x2, y2);
47+
}
48+
49+
private static boolean _rectangleIntersectsLine(double rectX, double rectY, double rectWidth,
4250
double rectHeight, double x1, double y1, double x2, double y2) {
4351
if (rectangleCornerOnSegment(rectX, rectY, rectWidth, rectHeight, x1, y1, x2, y2)) {
4452
return true;

src/test/java/com/github/davidmoten/rtree/RTreeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,5 +1140,5 @@ public void testSearchGreekEarthquakesDouble() {
11401140
.assertValueCount(22) //
11411141
.assertCompleted();
11421142
}
1143-
1143+
11441144
}

src/test/java/com/github/davidmoten/rtree/geometry/LineTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import static org.junit.Assert.assertFalse;
66
import static org.junit.Assert.assertTrue;
77

8+
import java.awt.geom.Line2D;
9+
import java.awt.geom.Rectangle2D;
10+
811
import org.junit.Test;
912

1013
public final class LineTest {
@@ -190,6 +193,35 @@ public void testLineDoesNotIntersectsPoint() {
190193
@Test
191194
public void testLineDoesIntersectPoint() {
192195
assertTrue(Geometries.line(1.5, 1.5, 2.5, 2.5).intersects(point(2, 2)));
196+
Rectangle2D d;
197+
}
198+
199+
@Test
200+
public void testLineDoubleIntersectsWithHorizontalLine() {
201+
{ // test expectation on Line2D.Double.intersectsLine which we depend on
202+
Line2D.Double line = new Line2D.Double(35.0d, -25.0d, 45.0d, -25.0d);
203+
assertTrue(line.intersectsLine(40.0d, -20.0d, 45.0d, -40.0d));
204+
}
205+
{
206+
Line line = Geometries.line(40.0d, -20.0d, 45.0d, -40.0d);
207+
Line horizontalLine = Geometries.line(35.0d, -25.0d, 45.0d, -25.0d);
208+
209+
assertTrue(line.intersects(horizontalLine.mbr()));
210+
}
211+
}
212+
213+
@Test
214+
public void testLineFloatIntersectsWithHorizontalLine() {
215+
{ // test expectation on Line2D.Double.intersectsLine which we depend on
216+
Line2D.Float line = new Line2D.Float(35.0f, -25.0f, 45.0f, -25.0f);
217+
assertTrue(line.intersectsLine(40.0f, -20.0f, 45.0f, -40.0f));
218+
}
219+
{
220+
Line line = Geometries.line(40.0f, -20.0f, 45.0f, -40.0f);
221+
Line horizontalLine = Geometries.line(35.0f, -25.0f, 45.0f, -25.0f);
222+
223+
assertTrue(line.intersects(horizontalLine.mbr()));
224+
}
193225
}
194226

195227
}

0 commit comments

Comments
 (0)