View Javadoc

1   package org.lcsim.spacegeom;
2   // Test the TwoSegment class.
3   import junit.framework.TestCase;
4   
5   import org.lcsim.spacegeom.CartesianTwoPoint;
6   import org.lcsim.spacegeom.TwoSegment;
7   import org.lcsim.spacegeom.TwoSpacePoint;
8   
9   public class TwoSegmentTest extends TestCase
10  {
11      boolean debug = false;
12      public void testTwoSegmentTest()
13      {
14          
15          String name = "TwoSegment";
16          String ok_prefix = name + " test (I): ";
17          String error_prefix = name + " test (E): ";
18          
19          if (debug) System.out.println( ok_prefix
20                  + "------- Testing component " + name + ". -------" );
21          
22          if (debug) System.out.println( ok_prefix + "Testing constructors." );
23          
24          TwoSpacePoint a = new CartesianTwoPoint(1.,1.);
25          TwoSpacePoint b = new CartesianTwoPoint(2.,2.);
26          TwoSegment seg = new TwoSegment(a,b);
27          
28          if (debug) System.out.println(seg);
29          
30          assertTrue(a.equals(seg.startPoint()));
31          assertTrue(b.equals(seg.endPoint()));
32          
33          assertTrue(seg.length()==TwoSpacePoint.distance(a,b));
34          
35          
36          if (debug) System.out.println( ok_prefix + "Testing intersection." );
37          TwoSegment seg2 = new TwoSegment(a,b);
38          
39          assertTrue(TwoSegment.intersection(seg,seg2)==null);
40          
41          TwoSegment seg3 = new TwoSegment(new CartesianTwoPoint(-1.,-1.),new CartesianTwoPoint(1.,1.));
42          TwoSegment seg4 = new TwoSegment(new CartesianTwoPoint(-1.,1.),new CartesianTwoPoint(1.,-1.));
43          
44          TwoSpacePoint intersection = TwoSegment.intersection(seg3, seg4);
45          if (debug) System.out.println(intersection);
46          assertTrue(intersection.x()==0.);
47          assertTrue(intersection.y()==0.);
48          
49          
50          // try another...
51          TwoSegment t1 = new TwoSegment(new CartesianTwoPoint(0.25, 0), new CartesianTwoPoint(1.25, 2.0));
52          TwoSegment t2 = new TwoSegment(new CartesianTwoPoint(0.05, 0), new CartesianTwoPoint(-0.947, 2.0));
53          intersection = TwoSegment.intersection(t1, t2);
54          if (debug) System.out.println(intersection);
55          
56          
57          //**********************************************************************
58          
59          if (debug) System.out.println( ok_prefix
60                  + "------------- All tests passed. ------------" );
61          
62      }
63      
64  }