[Geom4j-developer] SF.net SVN: geom4j:[23] trunk/src/net/sourceforge/geom4j
Status: Pre-Alpha
Brought to you by:
skhenkin
From: <skh...@us...> - 2009-12-18 22:27:36
|
Revision: 23 http://geom4j.svn.sourceforge.net/geom4j/?rev=23&view=rev Author: skhenkin Date: 2009-12-18 22:27:28 +0000 (Fri, 18 Dec 2009) Log Message: ----------- Tests refactored to use varargs constructors. Line equality and hashCode tests fixed. Modified Paths: -------------- trunk/src/net/sourceforge/geom4j/ClosestPairOfPointsAlgorithmTest.java trunk/src/net/sourceforge/geom4j/Config.java trunk/src/net/sourceforge/geom4j/DistanceTest.java trunk/src/net/sourceforge/geom4j/IntersectionTest.java trunk/src/net/sourceforge/geom4j/Line.java trunk/src/net/sourceforge/geom4j/LineTest.java trunk/src/net/sourceforge/geom4j/PointPairTest.java trunk/src/net/sourceforge/geom4j/PolygonTest.java trunk/src/net/sourceforge/geom4j/SegmentIntersectionAlgorithmTest.java trunk/src/net/sourceforge/geom4j/SegmentSet.java trunk/src/net/sourceforge/geom4j/SegmentTest.java trunk/src/net/sourceforge/geom4j/TriangulationTest.java trunk/src/net/sourceforge/geom4j/Vector.java trunk/src/net/sourceforge/geom4j/VectorTest.java Modified: trunk/src/net/sourceforge/geom4j/ClosestPairOfPointsAlgorithmTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/ClosestPairOfPointsAlgorithmTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/ClosestPairOfPointsAlgorithmTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -43,7 +43,8 @@ ps.add(new Point(2, 2)); ps.add(new Point(5, 3)); ps.add(new Point(11, 4)); - ps.add(new Point(9, 2)); } + ps.add(new Point(9, 2)); + } @Test public void testNaive() { Modified: trunk/src/net/sourceforge/geom4j/Config.java =================================================================== --- trunk/src/net/sourceforge/geom4j/Config.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/Config.java 2009-12-18 22:27:28 UTC (rev 23) @@ -68,4 +68,8 @@ public static double roundValue(double x) { return Math.round(x / precision) * precision; } + + public static boolean isZero(double x) { + return x < precision && x > -precision; + } } Modified: trunk/src/net/sourceforge/geom4j/DistanceTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/DistanceTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/DistanceTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -88,15 +88,15 @@ public void testPolygonDistance() { // TODO: refactor this and other tests using varargs Figure p = new Point(10, 20); - Figure square = new Polygon() - .addVertex(new Point(0, 0)) - .addVertex(new Point(0, 25)) - .addVertex(new Point(25, 25)) - .addVertex(new Point(25, 0)); - Figure triangle = new Polygon() - .addVertex(new Point(5, 10)) - .addVertex(new Point(10, 15)) - .addVertex(new Point(15, 15)); + Figure square = new Polygon( + new Point(0, 0), + new Point(0, 25), + new Point(25, 25), + new Point(25, 0)); + Figure triangle = new Polygon( + new Point(5, 10), + new Point(10, 15), + new Point(15, 15)); assertTrue(Config.equal(5, p.distanceTo(square))); assertTrue(Config.equal(5, p.distanceTo(triangle))); assertTrue(Config.equal(5, triangle.distanceTo(square))); Modified: trunk/src/net/sourceforge/geom4j/IntersectionTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/IntersectionTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/IntersectionTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -92,30 +92,30 @@ @Test public void testLineAndPolygonIntersection() { Figure line = new Line(new Point(0, 0), new Point(1, 1)); - Figure poly = new Polygon().addVertex(new Point(0, 0)) - .addVertex(new Point(10, 20)) - .addVertex(new Point(100, 20)) - .addVertex(new Point(90, 0)); - assertEquals(new PointSet().add(new Point(0, 0)) - .add(new Point(20, 20)), + Figure poly = new Polygon(new Point(0, 0), + new Point(10, 20), + new Point(100, 20), + new Point(90, 0)); + assertEquals(new PointSet(new Point(0, 0), + new Point(20, 20)), line.intersects(poly)); } @Test public void testPolygonIntersection() { - Figure square = new Polygon().addVertex(new Point( 0, 0)) - .addVertex(new Point(100, 0)) - .addVertex(new Point(100, 100)) - .addVertex(new Point( 0, 100)); - Figure trapezoid = new Polygon().addVertex(new Point(-20, 40)) - .addVertex(new Point(120, 40)) - .addVertex(new Point( 90, 130)) - .addVertex(new Point( 40, 130)); - assertEquals(new PointSet().add(new Point( 0, 40)) - .add(new Point( 0, 70)) - .add(new Point( 20, 100)) - .add(new Point(100, 100)) - .add(new Point(100, 40)), + Figure square = new Polygon(new Point( 0, 0), + new Point(100, 0), + new Point(100, 100), + new Point( 0, 100)); + Figure trapezoid = new Polygon(new Point(-20, 40), + new Point(120, 40), + new Point( 90, 130), + new Point( 40, 130)); + assertEquals(new PointSet(new Point( 0, 40), + new Point( 0, 70), + new Point( 20, 100), + new Point(100, 100), + new Point(100, 40)), square.intersects(trapezoid)); } } Modified: trunk/src/net/sourceforge/geom4j/Line.java =================================================================== --- trunk/src/net/sourceforge/geom4j/Line.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/Line.java 2009-12-18 22:27:28 UTC (rev 23) @@ -41,13 +41,41 @@ @Override public int hashCode() { final int prime = 31; - int result = 1; - // TODO: !!! this is completely wrong - result = prime * result + p1.hashCode(); - result = prime * result + p2.hashCode(); - return result; + long result = 1; + result = prime * result + Double.doubleToLongBits(getSlope()); + result = prime * result + Double.doubleToLongBits(getIntercept()); + return (int) result; } + /** + * A slope quotient of the straight line equation in the "slope-intercept" form. + * It is K in the equation y = Kx + B. + * @return slope quotient value (Double.POSITIVE_INFINITY if the line is vertical) + */ + public double getSlope() { + double dx = p2.getX() - p1.getX(); + double dy = p2.getY() - p1.getY(); + if (Config.isZero(dx)) { + return Double.POSITIVE_INFINITY; + } else { + return dy / dx; + } + } + + /** + * A y-intercept of the straight line equation in the "slope-intercept" form. + * It is B in the equation y = Kx + B. + * @return y-intercept value (Double.NaN if the line is vertical) + */ + public double getIntercept() { + double dx = p2.getX() - p1.getX(); + if (Config.isZero(dx)) { + return Double.NaN; + } else { + return (p2.getX()*p1.getY() - p1.getX()*p2.getY()) / dx; + } + } + @Override public boolean equals(Object obj) { if (obj instanceof Line) { Modified: trunk/src/net/sourceforge/geom4j/LineTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/LineTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/LineTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -16,6 +16,7 @@ package net.sourceforge.geom4j; import static org.junit.Assert.*; + import org.junit.Test; public class LineTest { @@ -105,4 +106,37 @@ assertFalse(l1.parallelTo(l3)); } + + @Test + public void testCorrectHashCode() { + Point p1 = new Point(10, 10); + Point p2 = new Point(20, 20); + Point p3 = new Point(30, 30); + Point p4 = new Point(40, 40); + Line line1 = new Line(p1, p2); + Line line2 = new Line(p2, p1); + Line line3 = new Line(p3, p4); + assertEquals(line1.hashCode(), line2.hashCode()); + assertEquals(line1.hashCode(), line3.hashCode()); + } + + @Test + public void testSlopeAndIntercept() { + assertSlopeIntercept(new Point(8, 2), new Point(12, 4), 0.5, -2); + assertSlopeIntercept(new Point(3, 2), new Point(-3, 10), -4.0/3, 6); + assertSlopeIntercept(new Point(2, -5), new Point(5, -5), 0, -5); + assertVertical(new Point(3, 1), new Point(3, 2)); + } + + private void assertSlopeIntercept(Point p1, Point p2, double k, double b) { + Line line = new Line(p1, p2); + assertEquals(0, Double.compare(k, line.getSlope())); + assertEquals(0, Double.compare(b, line.getIntercept())); + } + + private void assertVertical(Point p1, Point p2) { + Line line = new Line(p1, p2); + assertTrue(Double.isInfinite(line.getSlope())); + assertTrue(Double.isNaN(line.getIntercept())); + } } Modified: trunk/src/net/sourceforge/geom4j/PointPairTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/PointPairTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/PointPairTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -29,5 +29,14 @@ assertEquals(new PointPair(p1, p2), new PointPair(p2, p1)); assertFalse(new PointPair(p1, p2).equals(new PointPair(p2, new Point(40, 20)))); } + + @Test + public void testCorrectHashCode() { + Point p1 = new Point(10, 10); + Point p2 = new Point(30, 5); + PointPair pp1 = new PointPair(p1, p2); + PointPair pp2 = new PointPair(p2, p1); + assertEquals(pp1.hashCode(), pp2.hashCode()); + } } Modified: trunk/src/net/sourceforge/geom4j/PolygonTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/PolygonTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/PolygonTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -47,11 +47,11 @@ @Test public void testContains() { - Polygon p = new Polygon().addVertex(new Point(0, 0)) - .addVertex(new Point(100, 0)) - .addVertex(new Point(100, 100)) - .addVertex(new Point(50, 100)) - .addVertex(new Point(0, 50)); + Polygon p = new Polygon(new Point(0, 0), + new Point(100, 0), + new Point(100, 100), + new Point(50, 100), + new Point(0, 50)); assertTrue(p.contains(p)); assertEquals(p, p); assertTrue(p.contains(new Point(50, 100))); @@ -63,92 +63,92 @@ @Test public void testConvex() { - Polygon convex1 = new Polygon() - .addVertex(new Point(0, 0)) - .addVertex(new Point(100, 0)) - .addVertex(new Point(100, 100)) - .addVertex(new Point(50, 100)) - .addVertex(new Point(0, 50)); + Polygon convex1 = new Polygon( + new Point(0, 0), + new Point(100, 0), + new Point(100, 100), + new Point(50, 100), + new Point(0, 50)); assertTrue(convex1.isConvex()); - Polygon convex2 = new Polygon() - .addVertex(new Point(0, 50)) - .addVertex(new Point(50, 100)) - .addVertex(new Point(100, 100)) - .addVertex(new Point(100, 0)) - .addVertex(new Point(0, 0)); + Polygon convex2 = new Polygon( + new Point(0, 50), + new Point(50, 100), + new Point(100, 100), + new Point(100, 0), + new Point(0, 0)); assertTrue(convex2.isConvex()); - Polygon convex3 = new Polygon() - .addVertex(new Point(0, 50)) - .addVertex(new Point(0, 100)) - .addVertex(new Point(100, 100)) - .addVertex(new Point(50, 50)) - .addVertex(new Point(0, 0)); + Polygon convex3 = new Polygon( + new Point(0, 50), + new Point(0, 100), + new Point(100, 100), + new Point(50, 50), + new Point(0, 0)); assertTrue(convex3.isConvex()); - Polygon nonConvex1 = new Polygon() - .addVertex(new Point(0, 0)) - .addVertex(new Point(100, 50)) - .addVertex(new Point(50, 50)) - .addVertex(new Point(50, 100)); + Polygon nonConvex1 = new Polygon( + new Point(0, 0), + new Point(100, 50), + new Point(50, 50), + new Point(50, 100)); assertFalse(nonConvex1.isConvex()); - Polygon nonConvex2 = new Polygon() - .addVertex(new Point(100, 50)) - .addVertex(new Point(0, 0)) - .addVertex(new Point(50, 100)) - .addVertex(new Point(50, 50)); + Polygon nonConvex2 = new Polygon( + new Point(100, 50), + new Point(0, 0), + new Point(50, 100), + new Point(50, 50)); assertFalse(nonConvex2.isConvex()); - Polygon nonConvex3 = new Polygon() - .addVertex(new Point(0, 50)) - .addVertex(new Point(0, 100)) - .addVertex(new Point(100, 100)) - .addVertex(new Point(50, 51)) - .addVertex(new Point(0, 0)); + Polygon nonConvex3 = new Polygon( + new Point(0, 50), + new Point(0, 100), + new Point(100, 100), + new Point(50, 51), + new Point(0, 0)); assertFalse(nonConvex3.isConvex()); } @Test public void testArea() { - Polygon square = new Polygon() - .addVertex(new Point(10, 10)) - .addVertex(new Point(10, 20)) - .addVertex(new Point(20, 20)) - .addVertex(new Point(20, 10)); + Polygon square = new Polygon( + new Point(10, 10), + new Point(10, 20), + new Point(20, 20), + new Point(20, 10)); assertTrue(Config.equal(100, square.getArea())); - Polygon romb = new Polygon() - .addVertex(new Point(10, 10)) - .addVertex(new Point(50, 40)) - .addVertex(new Point(20, 80)) - .addVertex(new Point(-20, 50)); + Polygon romb = new Polygon( + new Point(10, 10), + new Point(50, 40), + new Point(20, 80), + new Point(-20, 50)); assertTrue(Config.equal(2500, romb.getArea())); assertTrue(Config.equal(4+8+6+6+6, p.getArea())); - Polygon mess = new Polygon() - .addVertex(new Point(1, 2)) - .addVertex(new Point(5, 2)) - .addVertex(new Point(2, 3)) - .addVertex(new Point(5, 3)) - .addVertex(new Point(2, 4)) - .addVertex(new Point(5, 6)) - .addVertex(new Point(3, 5)) - .addVertex(new Point(4, 7)) - .addVertex(new Point(4, 6)) - .addVertex(new Point(5, 7)) - .addVertex(new Point(1, 8)); + Polygon mess = new Polygon( + new Point(1, 2), + new Point(5, 2), + new Point(2, 3), + new Point(5, 3), + new Point(2, 4), + new Point(5, 6), + new Point(3, 5), + new Point(4, 7), + new Point(4, 6), + new Point(5, 7), + new Point(1, 8)); assertTrue(Config.equal(24-2-1.5-4.5-1.5, mess.getArea())); } @Test public void testSimplicity() { assertTrue(p.isSimple()); - assertTrue(new Polygon() - .addVertex(new Point(10, 10)) - .addVertex(new Point(10, 20)) - .addVertex(new Point(20, 20)) - .addVertex(new Point(20, 10)) + assertTrue(new Polygon( + new Point(10, 10), + new Point(10, 20), + new Point(20, 20), + new Point(20, 10)) .isSimple()); - assertFalse(new Polygon() - .addVertex(new Point(10, 10)) - .addVertex(new Point(10, 20)) - .addVertex(new Point(20, 10)) - .addVertex(new Point(20, 20)) + assertFalse(new Polygon( + new Point(10, 10), + new Point(10, 20), + new Point(20, 10), + new Point(20, 20)) .isSimple()); } } Modified: trunk/src/net/sourceforge/geom4j/SegmentIntersectionAlgorithmTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/SegmentIntersectionAlgorithmTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/SegmentIntersectionAlgorithmTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -19,18 +19,18 @@ @Before public void setUp() throws Exception { - ss1 = new SegmentSet() - .add(new Segment(new Point(1, 1), new Point(10, 10))) - .add(new Segment(new Point(10, 1), new Point(1, 10))) - .add(new Segment(new Point(1, 4), new Point(5, 2))); - ps1 = new PointSet().add(new Point(5.5, 5.5)).add(new Point(3, 3)); + ss1 = new SegmentSet( + new Segment(new Point(1, 1), new Point(10, 10)), + new Segment(new Point(10, 1), new Point(1, 10)), + new Segment(new Point(1, 4), new Point(5, 2))); + ps1 = new PointSet(new Point(5.5, 5.5), new Point(3, 3)); - Polygon p = new Polygon() - .addVertex(new Point(1, 1)) - .addVertex(new Point(1, 2)) - .addVertex(new Point(2, 3)) - .addVertex(new Point(3, 2)) - .addVertex(new Point(3, 1)); + Polygon p = new Polygon( + new Point(1, 1), + new Point(1, 2), + new Point(2, 3), + new Point(3, 2), + new Point(3, 1)); ss2 = p.getSegmentSet(); ps2 = new PointSet(p.getVertices()); @@ -115,10 +115,10 @@ @Test public void testBentleyOttmanSpecialCase1() { - SegmentSet ss = new SegmentSet() - .add(new Segment(new Point(7.288243310311859, 165.47985174930534), new Point(687.1335778157106, 878.6923172710824))) - .add(new Segment(new Point(189.62978423631606, 843.6858467771974), new Point(997.5303220302917, 900.134939913248))) - .add(new Segment(new Point(89.01407073223055, 331.76634850487164), new Point(967.7067870144189, 441.51160041074144))); + SegmentSet ss = new SegmentSet( + new Segment(new Point(7.288243310311859, 165.47985174930534), new Point(687.1335778157106, 878.6923172710824)), + new Segment(new Point(189.62978423631606, 843.6858467771974), new Point(997.5303220302917, 900.134939913248)), + new Segment(new Point(89.01407073223055, 331.76634850487164), new Point(967.7067870144189, 441.51160041074144))); PointSet naiveSet = ss.getAllIntersectionPoints(SegmentIntersectionAlgorithm.NAIVE); PointSet boSet = ss.getAllIntersectionPoints(SegmentIntersectionAlgorithm.BENTLEY_OTTMANN); assertEquals(naiveSet, boSet); Modified: trunk/src/net/sourceforge/geom4j/SegmentSet.java =================================================================== --- trunk/src/net/sourceforge/geom4j/SegmentSet.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/SegmentSet.java 2009-12-18 22:27:28 UTC (rev 23) @@ -15,6 +15,8 @@ package net.sourceforge.geom4j; +import java.util.Arrays; + /** * A set of segments * @@ -22,6 +24,13 @@ */ public class SegmentSet extends CompoundFigure<Segment> { + public SegmentSet() { + } + + public SegmentSet(Segment... segments) { + super(Arrays.asList(segments)); + } + @Override public SegmentSet add(Segment f) { return (SegmentSet) super.add(f); Modified: trunk/src/net/sourceforge/geom4j/SegmentTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/SegmentTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/SegmentTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -16,6 +16,7 @@ package net.sourceforge.geom4j; import static org.junit.Assert.*; + import org.junit.Test; public class SegmentTest { @@ -101,4 +102,13 @@ assertEquals(topLeft, topHorizontal.getLeftPoint()); assertEquals(topRight, topHorizontal.getRightPoint()); } + + @Test + public void testCorrectHashCode() { + Point p1 = new Point(10, 10); + Point p2 = new Point(30, 5); + Segment s1 = new Segment(p1, p2); + Segment s2 = new Segment(p2, p1); + assertEquals(s1.hashCode(), s2.hashCode()); + } } Modified: trunk/src/net/sourceforge/geom4j/TriangulationTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/TriangulationTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/TriangulationTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -38,12 +38,11 @@ @Test public void testFitting() { - Polygon polygon = new Polygon(new Point[] { + Polygon polygon = new Polygon( new Point(10, 0), new Point(0, 40), new Point(60, 70), - new Point(30, 10) - }); + new Point(30, 10)); Triangulation triangulation = new Triangulation(); triangulation.add(new Triangle(new Point(0, 40), new Point(30, 10), new Point(10, 0))) .add(new Triangle(new Point(30, 10), new Point(0, 40), new Point(60, 70))); Modified: trunk/src/net/sourceforge/geom4j/Vector.java =================================================================== --- trunk/src/net/sourceforge/geom4j/Vector.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/Vector.java 2009-12-18 22:27:28 UTC (rev 23) @@ -63,6 +63,21 @@ @Override + public boolean equals(Object obj) { + if (obj instanceof Vector) { + Vector v = (Vector) obj; + return x == v.x && y == v.y; + } + return false; + } + + @Override + public int hashCode() { + final int prime = 31; + return (int) (Double.doubleToLongBits(x) + prime * Double.doubleToLongBits(y)); + } + + @Override public String toString() { return "(" + x + ", " + y + ")"; } Modified: trunk/src/net/sourceforge/geom4j/VectorTest.java =================================================================== --- trunk/src/net/sourceforge/geom4j/VectorTest.java 2009-12-18 18:16:58 UTC (rev 22) +++ trunk/src/net/sourceforge/geom4j/VectorTest.java 2009-12-18 22:27:28 UTC (rev 23) @@ -51,4 +51,14 @@ assertTrue(Config.equal(Math.sqrt(2), new Vector(1.0, -1.0).length())); assertTrue(Config.equal(10, new Vector(0, -10).length())); } + + @Test + public void testCorrectHashCode() { + Vector v1 = new Vector(2, 1); + Vector v2 = new Vector(1, 2); + Vector v3 = new Vector(new Point(5, 6), new Point(7, 7)); + assertEquals(v1, v3); + assertFalse(v1.equals(v2)); + assertEquals(v1.hashCode(), v3.hashCode()); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |