1 package org.lcsim.detector.solids;
2
3 import hep.physics.vec.BasicHep3Vector;
4 import hep.physics.vec.Hep3Vector;
5
6 import java.text.DecimalFormat;
7 import java.text.NumberFormat;
8
9 import static java.lang.Math.abs;
10 import static java.lang.Math.sqrt;
11 import static java.lang.Math.acos;
12 import static java.lang.Math.atan;
13 import static java.lang.Math.PI;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class IsoscelesTrapezoid
36 {
37
38 private double _bottomHalf;
39 private double _topHalf;
40 private double _heightHalf;
41 private Hep3Vector[] _vertices = new Hep3Vector[4];
42 private double _area;
43 private double _baseAngle;
44 private double _topAngle;
45
46
47
48
49
50
51
52 public IsoscelesTrapezoid(double bottomHalf, double topHalf, double heightHalf)
53 {
54 _bottomHalf = bottomHalf;
55 _topHalf = topHalf;
56 _heightHalf = heightHalf;
57 _area = 2. * (_bottomHalf + _topHalf) * _heightHalf;
58
59 _vertices[0] = new BasicHep3Vector(-_bottomHalf, -_heightHalf, 0.);
60 _vertices[1] = new BasicHep3Vector(_bottomHalf, -_heightHalf, 0.);
61 _vertices[2] = new BasicHep3Vector(_topHalf, _heightHalf, 0.);
62 _vertices[3] = new BasicHep3Vector(-_topHalf, _heightHalf, 0.);
63
64 double delta = atan((_topHalf - _bottomHalf) / (2. * _heightHalf));
65 _baseAngle = (_topHalf >_bottomHalf) ? PI / 2. + delta : PI/2.-delta;
66 _topAngle = PI-_baseAngle;
67
68 }
69
70
71
72
73
74
75
76
77
78 public Hep3Vector[] getVertices()
79 {
80 return _vertices;
81 }
82
83
84
85
86
87 public double area()
88 {
89 return _area;
90 }
91
92
93
94
95
96
97
98 public Inside inside(Hep3Vector pos)
99 {
100
101 double y = pos.y();
102 if (abs(y) > _heightHalf)
103 return Inside.OUTSIDE;
104
105
106 if (t(pos, _vertices[1], _vertices[2]) < 0)
107 return Inside.OUTSIDE;
108 if (t(pos, _vertices[3], _vertices[0]) < 0)
109 return Inside.OUTSIDE;
110
111 return Inside.INSIDE;
112 }
113
114 @Override
115 public String toString()
116 {
117 NumberFormat formatter = new DecimalFormat("#0.0000");
118
119 StringBuffer sb = new StringBuffer("IsoscelesTrapezoid\n");
120 sb.append(" base " + 2. * _bottomHalf + " top " + 2. * _topHalf + " height " + 2. * _heightHalf + " \n");
121 sb.append(" area " + _area + " \n");
122 return sb.toString();
123 }
124
125 public double baseAngle()
126 {
127 return _baseAngle;
128 }
129
130 public double topAngle()
131 {
132 return _topAngle;
133 }
134
135 private double t(Hep3Vector a, Hep3Vector b, Hep3Vector c)
136 {
137 double t = (a.y() - b.y()) * (c.x() - b.x()) - (a.x() - b.x()) * (c.y() - b.y());
138 return t;
139 }
140
141 }