This is the mail archive of the
mauve-patches@sources.redhat.com
mailing list for the Mauve project.
Test cases for Line2D class
- From: David Gilbert <david dot gilbert at object-refinery dot com>
- To: mauve-patches at sources dot redhat dot com
- Date: Mon, 02 Aug 2004 13:43:28 +0100
- Subject: Test cases for Line2D class
First contribution from me...
I've attached some test cases for the Line2D class - all checks pass for
me on JDK 1.4.2_03 and JDK 1.3.1_11 (on SuSE Linux), 10 checks fail
using Classpath (fairly recent copy from CVS). The failures I think
were pointed out earlier by Sven de Marothy on this list). Sven's test
case is included in the attachments since I've added a couple of checks
to it - also his version is not yet in CVS.
Also attached is a patch that I think fixes the linesIntersect() method
(can't be 100% sure since I'm having trouble building Classpath right at
the moment, but I'll confirm it as soon as I can - the code passed my
standalone tests OK).
Regards,
Dave Gilbert
P.S. I have completed the FSF paperwork for contributing to Classpath,
is this a requirement for Mauve also?
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert <david.gilbert@object-refinery.com>
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
/**
* Checks that Line2D.clone() method works correctly.
*/
public class clone implements Testlet {
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness) {
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
Line2D line2 = null;
line2 = (Line2D) line1.clone();
harness.check(line1.getX1() == line2.getX1());
harness.check(line1.getX2() == line2.getX2());
harness.check(line1.getY1() == line2.getY1());
harness.check(line1.getY2() == line2.getY2());
harness.check(line1.getClass() == line2.getClass());
harness.check(line1 != line2);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert <david.gilbert@object-refinery.com>
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
/**
* Checks that Line2D.contains() method works correctly.
*/
public class contains implements Testlet {
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness) {
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
harness.check(line1.contains(1.0, 2.0) == false);
harness.check(line1.contains(3.0, 4.0) == false);
harness.check(line1.contains(new Point2D.Double(1.0, 2.0)) == false);
harness.check(line1.contains(new Point2D.Double(3.0, 4.0)) == false);
harness.check(line1.contains((Point2D) null) == false);
harness.check(line1.contains(new Rectangle2D.Double(1.0, 2.0, 0.0, 0.0)) == false);
harness.check(line1.contains((Rectangle2D) null) == false);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
/**
* Line2D does not override equals (see bug parade id 5057070).
*/
public class equals
implements Testlet
{
/**
* Confirm that two lines with the same end points are NOT considered equal.
*/
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
Line2D line2 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
harness.check(!line1.equals(line2));
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert <david.gilbert@object-refinery.com>
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.Rectangle;
/**
* Checks that Line2D.getBounds() works correctly.
*/
public class getBounds implements Testlet {
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness) {
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
Rectangle bounds = line1.getBounds();
harness.check((int) bounds.getX() == 1);
harness.check((int) bounds.getMaxX() == 3);
harness.check((int) bounds.getY() == 2);
harness.check((int) bounds.getMaxY() == 4);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.getP1() works correctly.
*/
public class getP1
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
Point2D pt1 = line1.getP1();
harness.check(pt1.getX() == 1.0);
harness.check(pt1.getY() == 2.0);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.getP2() works correctly.
*/
public class getP2
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
Point2D pt2 = line1.getP2();
harness.check(pt2.getX() == 3.0);
harness.check(pt2.getY() == 4.0);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
/**
* Checks whether Line2D.getPathIterator() works correctly.
*/
public class getPathIterator
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
PathIterator iterator = line1.getPathIterator(null);
double[] c = new double[6];
harness.check(!iterator.isDone());
harness.check(iterator.currentSegment(c), PathIterator.SEG_MOVETO);
harness.check(c[0], 1.0);
harness.check(c[1], 2.0);
iterator.next();
harness.check(!iterator.isDone());
harness.check(iterator.currentSegment(c), PathIterator.SEG_LINETO);
harness.check(c[0], 3.0);
harness.check(c[1], 4.0);
iterator.next();
harness.check(iterator.isDone());
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
/**
* Checks whether Line2D.intersects() works correctly.
*/
public class intersects
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(0.0, 0.0, 1.0, 0.0);
harness.check(line1.intersects(0.0, -1.0, 1.0, 1.0));
harness.check(line1.intersects(0.0, 0.0, 1.0, 1.0));
harness.check(!line1.intersects(0.0, 1.0, 1.0, 1.0));
harness.check(line1.intersects(new Rectangle2D.Double(0.0, -1.0, 1.0, 1.0)));
harness.check(line1.intersects(new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0)));
harness.check(!line1.intersects(new Rectangle2D.Double(0.0, 1.0, 1.0, 1.0)));
Line2D line2 = new Line2D.Double(0.0, 0.0, 0.0, 1.0);
harness.check(line2.intersects(-1.0, 0.0, 1.0, 1.0));
harness.check(line2.intersects(0.0, 0.0, 1.0, 1.0));
harness.check(!line2.intersects(1.0, 0.0, 1.0, 1.0));
harness.check(line2.intersects(new Rectangle2D.Double(-1.0, 0.0, 1.0, 1.0)));
harness.check(line2.intersects(new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0)));
harness.check(!line2.intersects(new Rectangle2D.Double(1.0, 0.0, 1.0, 1.0)));
Line2D line3 = new Line2D.Double(0.0, 1.0, 1.0, 1.0);
harness.check(!line3.intersects(0.0, -1.0, 1.0, 1.0));
harness.check(line3.intersects(0.0, 0.0, 1.0, 1.0));
harness.check(line3.intersects(0.0, 1.0, 1.0, 1.0));
harness.check(!line3.intersects(new Rectangle2D.Double(0.0, -1.0, 1.0, 1.0)));
harness.check(line3.intersects(new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0)));
harness.check(line3.intersects(new Rectangle2D.Double(0.0, 1.0, 1.0, 1.0)));
Line2D line4 = new Line2D.Double(1.0, 0.0, 1.0, 1.0);
harness.check(!line4.intersects(-1.0, 0.0, 1.0, 1.0));
harness.check(line4.intersects(0.0, 0.0, 1.0, 1.0));
harness.check(line4.intersects(1.0, 0.0, 1.0, 1.0));
harness.check(!line4.intersects(new Rectangle2D.Double(-1.0, 0.0, 1.0, 1.0)));
harness.check(line4.intersects(new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0)));
harness.check(line4.intersects(new Rectangle2D.Double(1.0, 0.0, 1.0, 1.0)));
boolean pass = false;
try {
line4.intersects(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
/**
* Checks whether Line2D.intersectsLine() works correctly.
*/
public class intersectsLine
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(0.0, 0.0, 1.0, 0.0);
harness.check(line1.intersectsLine(0.0, 0.0, 1.0, 0.0));
harness.check(line1.intersectsLine(0.0, 0.0, 1.0, 1.0));
harness.check(line1.intersectsLine(1.0, 1.0, 1.0, 0.0));
harness.check(line1.intersectsLine(0.5, 0.5, 0.5, -0.5));
harness.check(!line1.intersectsLine(0.0, 1.0, 1.0, 1.0));
harness.check(line1.intersectsLine(new Line2D.Double(0.0, 0.0, 1.0, 0.0)));
harness.check(line1.intersectsLine(new Line2D.Double(0.0, 0.0, 1.0, 1.0)));
harness.check(line1.intersectsLine(new Line2D.Double(1.0, 1.0, 1.0, 0.0)));
harness.check(line1.intersectsLine(new Line2D.Double(0.5, 0.5, 0.5, -0.5)));
harness.check(!line1.intersectsLine(new Line2D.Double(0.0, 1.0, 1.0, 1.0)));
boolean pass = false;
try {
line1.intersectsLine(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 Sven de Marothy <sven@physto.se>
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.awt.geom.Line2D;
/**
* Checks whether Line2D.linesIntersect works correctly
*
* @author Sven de Marothy
*/
public class linesIntersect
implements Testlet
{
public void test(TestHarness harness)
{
// Test 1 - a simple intersection
harness.check(Line2D.linesIntersect(0.0, 0.0, 100.0, 50.0,
0.0, 50.0, 100.0, 0.0));
// Test 2 - an orthogonal intersection
harness.check(Line2D.linesIntersect(0.0, 0.0, 100.0, 100.0,
0.0, 100.0, 100.0, 0.0));
// Test 3 - an orthogonal intersection on the axes
harness.check(Line2D.linesIntersect(0.0, 10.0, 100.0, 10.0,
50.0, 0.0, 50.0, 50.0));
// Test 4 - colinear overlapping lines
harness.check(Line2D.linesIntersect(10.0, 10.0, 10.0, 90.0,
10.0, 0.0, 10.0, 100.0));
// Test 5 - colinear nonoverlapping lines
harness.check(!Line2D.linesIntersect(10.0, 10.0, 10.0, 90.0,
10.0, 91.0, 10.0, 100.0));
// Test 6 - zero length lines at same point
harness.check(Line2D.linesIntersect(1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0));
// Test 7 - segments share end point
harness.check(Line2D.linesIntersect(0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0));
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.ptLineDist() works correctly.
*/
public class ptLineDist
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(0.0, 0.0, 1.0, 0.0);
harness.check(0.0, line1.ptLineDist(-50.0, 0.0));
harness.check(0.0, line1.ptLineDist(0.0, 0.0));
harness.check(0.0, line1.ptLineDist(1.0, 0.0));
harness.check(0.0, line1.ptLineDist(50.0, 0.0));
harness.check(1.0, line1.ptLineDist(-50.0, 1.0));
harness.check(1.0, line1.ptLineDist(0.0, 1.0));
harness.check(1.0, line1.ptLineDist(1.0, 1.0));
harness.check(1.0, line1.ptLineDist(50.0, 1.0));
harness.check(1.0, line1.ptLineDist(-50.0, -1.0));
harness.check(1.0, line1.ptLineDist(0.0, -1.0));
harness.check(1.0, line1.ptLineDist(1.0, -1.0));
harness.check(1.0, line1.ptLineDist(50.0, -1.0));
harness.check(0.0, line1.ptLineDist(new Point2D.Double(-50.0, 0.0)));
harness.check(0.0, line1.ptLineDist(new Point2D.Double(0.0, 0.0)));
harness.check(0.0, line1.ptLineDist(new Point2D.Double(1.0, 0.0)));
harness.check(0.0, line1.ptLineDist(new Point2D.Double(50.0, 0.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(-50.0, 1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(0.0, 1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(1.0, 1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(50.0, 1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(-50.0, -1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(0.0, -1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(1.0, -1.0)));
harness.check(1.0, line1.ptLineDist(new Point2D.Double(50.0, -1.0)));
boolean pass = false;
try {
line1.ptLineDist(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.ptLineDistSq() works correctly.
*/
public class ptLineDistSq
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(0.0, 0.0, 1.0, 0.0);
harness.check(0.0, line1.ptLineDistSq(-50.0, 0.0));
harness.check(0.0, line1.ptLineDistSq(0.0, 0.0));
harness.check(0.0, line1.ptLineDistSq(1.0, 0.0));
harness.check(0.0, line1.ptLineDistSq(50.0, 0.0));
harness.check(1.0, line1.ptLineDistSq(-50.0, 1.0));
harness.check(1.0, line1.ptLineDistSq(0.0, 1.0));
harness.check(1.0, line1.ptLineDistSq(1.0, 1.0));
harness.check(1.0, line1.ptLineDistSq(50.0, 1.0));
harness.check(1.0, line1.ptLineDistSq(-50.0, -1.0));
harness.check(1.0, line1.ptLineDistSq(0.0, -1.0));
harness.check(1.0, line1.ptLineDistSq(1.0, -1.0));
harness.check(1.0, line1.ptLineDistSq(50.0, -1.0));
harness.check(0.0, line1.ptLineDistSq(new Point2D.Double(-50.0, 0.0)));
harness.check(0.0, line1.ptLineDistSq(new Point2D.Double(0.0, 0.0)));
harness.check(0.0, line1.ptLineDistSq(new Point2D.Double(1.0, 0.0)));
harness.check(0.0, line1.ptLineDistSq(new Point2D.Double(50.0, 0.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(-50.0, 1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(0.0, 1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(1.0, 1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(50.0, 1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(-50.0, -1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(0.0, -1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(1.0, -1.0)));
harness.check(1.0, line1.ptLineDistSq(new Point2D.Double(50.0, -1.0)));
boolean pass = false;
try {
line1.ptLineDistSq(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.ptSegDist() works correctly.
*/
public class ptSegDist
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(0.0, 0.0, 1.0, 0.0);
harness.check(50.0, line1.ptSegDist(-50.0, 0.0));
harness.check(0.0, line1.ptSegDist(0.0, 0.0));
harness.check(0.0, line1.ptSegDist(1.0, 0.0));
harness.check(49.0, line1.ptSegDist(50.0, 0.0));
harness.check(Math.sqrt(2501.0), line1.ptSegDist(-50.0, 1.0));
harness.check(1.0, line1.ptSegDist(0.0, 1.0));
harness.check(1.0, line1.ptSegDist(1.0, 1.0));
harness.check(Math.sqrt(49.0*49.0+1.0), line1.ptSegDist(50.0, 1.0));
harness.check(Math.sqrt(2501.0), line1.ptSegDist(-50.0, -1.0));
harness.check(1.0, line1.ptSegDist(0.0, -1.0));
harness.check(1.0, line1.ptSegDist(1.0, -1.0));
harness.check(Math.sqrt(49.0*49.0+1.0), line1.ptSegDist(50.0, -1.0));
harness.check(50.0, line1.ptSegDist(new Point2D.Double(-50.0, 0.0)));
harness.check(0.0, line1.ptSegDist(new Point2D.Double(0.0, 0.0)));
harness.check(0.0, line1.ptSegDist(new Point2D.Double(1.0, 0.0)));
harness.check(49.0, line1.ptSegDist(new Point2D.Double(50.0, 0.0)));
harness.check(Math.sqrt(2501.0), line1.ptSegDist(new Point2D.Double(-50.0, 1.0)));
harness.check(1.0, line1.ptSegDist(new Point2D.Double(0.0, 1.0)));
harness.check(1.0, line1.ptSegDist(new Point2D.Double(1.0, 1.0)));
harness.check(Math.sqrt(49.0*49.0+1.0), line1.ptSegDist(new Point2D.Double(50.0, 1.0)));
harness.check(Math.sqrt(2501.0), line1.ptSegDist(new Point2D.Double(-50.0, -1.0)));
harness.check(1.0, line1.ptSegDist(new Point2D.Double(0.0, -1.0)));
harness.check(1.0, line1.ptSegDist(new Point2D.Double(1.0, -1.0)));
harness.check(Math.sqrt(49.0*49.0+1.0), line1.ptSegDist(new Point2D.Double(50.0, -1.0)));
boolean pass = false;
try {
line1.ptSegDist(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.ptSegDistSq() works correctly.
*/
public class ptSegDistSq
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double(0.0, 0.0, 1.0, 0.0);
harness.check(2500.0, line1.ptSegDistSq(-50.0, 0.0));
harness.check(0.0, line1.ptSegDistSq(0.0, 0.0));
harness.check(0.0, line1.ptSegDistSq(1.0, 0.0));
harness.check(49.0*49.0, line1.ptSegDistSq(50.0, 0.0));
harness.check(2501.0, line1.ptSegDistSq(-50.0, 1.0));
harness.check(1.0, line1.ptSegDistSq(0.0, 1.0));
harness.check(1.0, line1.ptSegDistSq(1.0, 1.0));
harness.check(49.0*49.0+1.0, line1.ptSegDistSq(50.0, 1.0));
harness.check(2501.0, line1.ptSegDistSq(-50.0, -1.0));
harness.check(1.0, line1.ptSegDistSq(0.0, -1.0));
harness.check(1.0, line1.ptSegDistSq(1.0, -1.0));
harness.check(49.0*49.0+1.0, line1.ptSegDistSq(50.0, -1.0));
harness.check(2500.0, line1.ptSegDistSq(new Point2D.Double(-50.0, 0.0)));
harness.check(0.0, line1.ptSegDistSq(new Point2D.Double(0.0, 0.0)));
harness.check(0.0, line1.ptSegDistSq(new Point2D.Double(1.0, 0.0)));
harness.check(49.0*49.0, line1.ptSegDistSq(new Point2D.Double(50.0, 0.0)));
harness.check(2501.0, line1.ptSegDistSq(new Point2D.Double(-50.0, 1.0)));
harness.check(1.0, line1.ptSegDistSq(new Point2D.Double(0.0, 1.0)));
harness.check(1.0, line1.ptSegDistSq(new Point2D.Double(1.0, 1.0)));
harness.check(49.0*49.0+1.0, line1.ptSegDistSq(new Point2D.Double(50.0, 1.0)));
harness.check(2501.0, line1.ptSegDistSq(new Point2D.Double(-50.0, -1.0)));
harness.check(1.0, line1.ptSegDistSq(new Point2D.Double(0.0, -1.0)));
harness.check(1.0, line1.ptSegDistSq(new Point2D.Double(1.0, -1.0)));
harness.check(49.0*49.0+1.0, line1.ptSegDistSq(new Point2D.Double(50.0, -1.0)));
boolean pass = false;
try {
line1.ptSegDistSq(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Some checks for the Line2D.relativeCCW() methods.
*/
public class relativeCCW
implements Testlet
{
/**
* Run the test.
*/
public void test(TestHarness harness)
{
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, 1.0, 1.0) == 0);
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, 3.0, 2.0) == 0);
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, 0.0, 0.0) == 1);
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, -1.0, 0.0) == -1);
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, 5.0, 3.0) == 1);
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, 5.0, 4.0) == -1);
harness.check(Line2D.relativeCCW(1.0, 1.0, 3.0, 2.0, -1.0, -1.0) == 1);
harness.check(Line2D.relativeCCW(1.0, 1.0, 1.0, 1.0, 1.0, 1.0) == 0);
harness.check(Line2D.relativeCCW(1.0, 1.0, 1.0, 1.0, 2.0, 2.0) == 0);
Line2D line1 = new Line2D.Double(1.0, 1.0, 3.0, 2.0);
harness.check(line1.relativeCCW(1.0, 1.0) == 0);
harness.check(line1.relativeCCW(3.0, 2.0) == 0);
harness.check(line1.relativeCCW(0.0, 0.0) == 1);
harness.check(line1.relativeCCW(-1.0, 0.0) == -1);
harness.check(line1.relativeCCW(5.0, 3.0) == 1);
harness.check(line1.relativeCCW(5.0, 4.0) == -1);
harness.check(line1.relativeCCW(-1.0, -1.0) == 1);
harness.check(line1.relativeCCW(new Point2D.Double(1.0, 1.0)) == 0);
harness.check(line1.relativeCCW(new Point2D.Double(3.0, 2.0)) == 0);
harness.check(line1.relativeCCW(new Point2D.Double(0.0, 0.0)) == 1);
harness.check(line1.relativeCCW(new Point2D.Double(-1.0, 0.0)) == -1);
harness.check(line1.relativeCCW(new Point2D.Double(5.0, 3.0)) == 1);
harness.check(line1.relativeCCW(new Point2D.Double(5.0, 4.0)) == -1);
harness.check(line1.relativeCCW(new Point2D.Double(-1.0, -1.0)) == 1);
boolean pass = false;
try {
line1.relativeCCW(null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
//Tags: JDK1.2
//Copyright (C) 2004 David Gilbert (david.gilbert@object-refinery.com)
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA. */
package gnu.testlet.java.awt.geom.Line2D;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Checks whether Line2D.setLine() works correctly.
*/
public class setLine
implements Testlet
{
public void test(TestHarness harness)
{
Line2D line1 = new Line2D.Double();
line1.setLine(1.0, 2.0, 3.0, 4.0);
harness.check(line1.getX1() == 1.0);
harness.check(line1.getY1() == 2.0);
harness.check(line1.getX2() == 3.0);
harness.check(line1.getY2() == 4.0);
line1.setLine(new Point2D.Double(1.1, 2.2), new Point2D.Double(3.3, 4.4));
harness.check(line1.getX1() == 1.1);
harness.check(line1.getY1() == 2.2);
harness.check(line1.getX2() == 3.3);
harness.check(line1.getY2() == 4.4);
line1.setLine(new Line2D.Double(1.11, 2.22, 3.33, 4.44));
harness.check(line1.getX1() == 1.11);
harness.check(line1.getY1() == 2.22);
harness.check(line1.getX2() == 3.33);
harness.check(line1.getY2() == 4.44);
Line2D line2 = new Line2D.Float();
line2.setLine(1.1, 2.2, 3.3, 4.4);
harness.check(line2.getX1() == 1.1f);
harness.check(line2.getY1() == 2.2f);
harness.check(line2.getX2() == 3.3f);
harness.check(line2.getY2() == 4.4f);
line2.setLine(new Point2D.Float(1.1f, 2.2f), new Point2D.Float(3.3f, 4.4f));
harness.check(line2.getX1() == 1.1f);
harness.check(line2.getY1() == 2.2f);
harness.check(line2.getX2() == 3.3f);
harness.check(line2.getY2() == 4.4f);
line2.setLine(new Line2D.Float(1.11f, 2.22f, 3.33f, 4.44f));
harness.check(line2.getX1() == 1.11f);
harness.check(line2.getY1() == 2.22f);
harness.check(line2.getX2() == 3.33f);
harness.check(line2.getY2() == 4.44f);
// check that null arguments throw the correct exception
boolean pass = false;
try {
line1.setLine(null, new Point2D.Double());
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
pass = false;
try {
line1.setLine(new Point2D.Double(), null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
pass = false;
try {
line1.setLine((Line2D) null);
}
catch (NullPointerException e) {
pass = true;
}
harness.check(pass);
}
}
Index: java/awt/geom/Line2D.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/geom/Line2D.java,v
retrieving revision 1.6
diff -u -r1.6 Line2D.java
--- java/awt/geom/Line2D.java 18 Jul 2003 19:35:02 -0000 1.6
+++ java/awt/geom/Line2D.java 2 Aug 2004 12:33:11 -0000
@@ -235,11 +235,57 @@
}
/**
- * Test if the line segment (x1,y1)->(x2,y2) intersects the line segment
+ * Computes twice the (signed) area of the triangle defined by the three
+ * points. This method is used for intersection testing.
+ *
+ * @param x1 the x-coordinate of the first point.
+ * @param y1 the y-coordinate of the first point.
+ * @param x2 the x-coordinate of the second point.
+ * @param y2 the y-coordinate of the second point.
+ * @param x3 the x-coordinate of the third point.
+ * @param y3 the y-coordinate of the third point.
+ *
+ * @return Twice the area.
+ */
+ private static double area2(double x1, double y1,
+ double x2, double y2,
+ double x3, double y3)
+ {
+ return (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);
+ }
+
+ /**
+ * Returns <code>true</code> if (x3, y3) lies between (x1, y1) and (x2, y2),
+ * and false otherwise, This test assumes that the three points are
+ * collinear, and is used for intersection testing.
+ *
+ * @param x1 the x-coordinate of the first point.
+ * @param y1 the y-coordinate of the first point.
+ * @param x2 the x-coordinate of the second point.
+ * @param y2 the y-coordinate of the second point.
+ * @param x3 the x-coordinate of the third point.
+ * @param y3 the y-coordinate of the third point.
+ *
+ * @return A boolean.
+ */
+ private static boolean between(double x1, double y1,
+ double x2, double y2,
+ double x3, double y3)
+ {
+ if (x1 != x2) {
+ return (x1 <= x3 && x3 <= x2) || (x1 >= x3 && x3 >= x2);
+ }
+ else {
+ return (y1 <= y3 && y3 <= y2) || (y1 >= y3 && y3 >= y2);
+ }
+ }
+
+ /**
+ * Test if the line segment (x1,y1)->(x2,y2) intersects the line segment
* (x3,y3)->(x4,y4).
*
* @param x1 the first x coordinate of the first segment
- * @param y1 the first y coordinate of the first segment
+ * @param y1 the first y coordinate of the first segment
* @param x2 the second x coordinate of the first segment
* @param y2 the second y coordinate of the first segment
* @param x3 the first x coordinate of the second segment
@@ -249,16 +295,64 @@
* @return true if the segments intersect
*/
public static boolean linesIntersect(double x1, double y1,
- double x2, double y2,
- double x3, double y3,
- double x4, double y4)
- {
- double beta = (((y1 - y3) * (x4 - x3) + (x1 - x3) * (y4 - y3))
- / ((y2 - y1) * (x4 - x3) + (x2 - x1) * (y4 - y3)));
- if (beta < 0.0 || beta > 1.0)
- return false;
- double alpha = (x1 + beta * (x2 - x1) - x3) / (x4 - x3);
- return alpha >= 0.0 && alpha <= 1.0;
+ double x2, double y2,
+ double x3, double y3,
+ double x4, double y4)
+ {
+ double a1, a2, a3, a4;
+
+ // deal with special cases
+ if ((a1 = area2(x1, y1, x2, y2, x3, y3)) == 0.0)
+ {
+ // check if p3 is between p1 and p2 OR
+ // p4 is collinear also AND either between p1 and p2 OR at opposite ends
+ if (between(x1, y1, x2, y2, x3, y3))
+ {
+ return true;
+ }
+ else
+ {
+ if (area2(x1, y1, x2, y2, x4, y4) == 0.0)
+ {
+ return between(x3, y3, x4, y4, x1, y1)
+ || between (x3, y3, x4, y4, x2, y2);
+ }
+ else {
+ return false;
+ }
+ }
+ }
+ else if ((a2 = area2(x1, y1, x2, y2, x4, y4)) == 0.0)
+ {
+ // check if p4 is between p1 and p2 (we already know p3 is not
+ // collinear)
+ return between(x1, y1, x2, y2, x4, y4);
+ }
+
+ if ((a3 = area2(x3, y3, x4, y4, x1, y1)) == 0.0) {
+ // check if p1 is between p3 and p4 OR
+ // p2 is collinear also AND either between p1 and p2 OR at opposite ends
+ if (between(x3, y3, x4, y4, x1, y1)) {
+ return true;
+ }
+ else {
+ if (area2(x3, y3, x4, y4, x2, y2) == 0.0) {
+ return between(x1, y1, x2, y2, x3, y3)
+ || between (x1, y1, x2, y2, x4, y4);
+ }
+ else {
+ return false;
+ }
+ }
+ }
+ else if ((a4 = area2(x3, y3, x4, y4, x2, y2)) == 0.0) {
+ // check if p2 is between p3 and p4 (we already know p1 is not
+ // collinear)
+ return between(x3, y3, x4, y4, x2, y2);
+ }
+ else { // test for regular intersection
+ return ((a1 > 0.0) ^ (a2 > 0.0)) && ((a3 > 0.0) ^ (a4 > 0.0));
+ }
}
/**