View Javadoc

1   
2   package org.lcsim.detector.solids;
3   
4   import hep.physics.vec.BasicHep3Vector;
5   import java.util.Random;
6   import junit.framework.TestCase;
7   import static java.lang.Math.PI;
8   import static java.lang.Math.abs;
9   
10  /**
11   *
12   * @author Norman A. Graf
13   *
14   * @version $Id: RegularPolygonTest.java,v 1.1 2010/04/09 22:53:24 ngraf Exp $
15   */
16  public class RegularPolygonTest extends TestCase
17  {
18  
19      private boolean debug = false;
20  
21      public RegularPolygonTest(String testName)
22      {
23          super(testName);
24      }
25  
26      public void testRegularPolygon()
27      {
28          // coverage test
29          // generate random points in the circumscribed circle
30          // ratio of inside to outside should equal the ratio of areas
31          //
32          double radius = 1;
33          double circleArea = PI * radius * radius;
34          int nmax = 1000000;
35          int nIn = 0;
36          Random rr = new Random();
37          // test a common range of polygons...
38          for (int np = 3; np < 21; ++np)
39          {
40              int nsides = np;
41              if (debug)
42                  System.out.println("   ");
43              if (debug)
44                  System.out.println("Testing nsides= " + nsides);
45              RegularPolygon p = new RegularPolygon(nsides, radius);
46  
47              int i = 0;
48              nIn = 0;
49              while (i < nmax)
50              {
51                  double x = radius*(2.*rr.nextDouble()-1.);
52                  double y = radius*(2.*rr.nextDouble()-1.);
53                  if ((x * x + y * y) < radius * radius)
54                  {
55                      ++i;
56                      BasicHep3Vector pos = new BasicHep3Vector(x, y, 0.);
57                      Inside in = p.inside(pos);
58                      if (Inside.INSIDE.compareTo(in) == 0)
59                          ++nIn;
60                  }
61              }
62              double meas = (double) nIn / nmax;
63              double pred = p.area() / circleArea;
64              if (debug)
65                  System.out.println("nmax= " + nmax + " nIn= " + nIn + " ratio= " + meas);
66              if (debug)
67                  System.out.println("area ratio= " + pred);
68              double err = (meas - pred) / pred;
69              if (debug)
70                  System.out.println("(meas-pred)/pred= " + err);
71              // .5% uncertainty
72              assertTrue(abs(err) < .005);
73          }
74      }
75  }