/Users/lyon/j4p/src/bookExamples/ch26Graphics/draw2d/Circle2d.java

1    package bookExamples.ch26Graphics.draw2d; 
2     
3     
4    import java.awt.*; 
5     
6    class Circle2d extends Shape 
7            implements Intersects { 
8        int x1 = 0; 
9        int y1 = 0; 
10       int diameter = 1; 
11       int radius = 1; 
12       Vec2d center = new Vec2d(0, 0); 
13       double radiusSq; 
14    
15       Circle2d(int _x1, int _y1, int _diameter) { 
16           x1 = _x1; 
17           y1 = _y1; 
18           diameter = _diameter; 
19           radius = diameter / 2; 
20           radiusSq = radius * radius; 
21       } 
22    
23       Circle2d(int _x1, int _y1, int _x2, int _y2) { 
24           x1 = _x1; 
25           y1 = _y1; 
26           int dx = x1 - _x2; 
27           int dy = y1 - _y2; 
28           diameter = (int) Math.sqrt(dx * dx + dy * dy); 
29           if (_x1 > _x2) x1 = _x2; 
30           if (_y1 > _y2) y1 = _y2; 
31           radius = diameter / 2; 
32           radiusSq = radius * radius; 
33           center.v[0] = x1 + radius; 
34           center.v[1] = y1 + radius; 
35       } 
36    
37       public void paint(Graphics g) { 
38           int xc = (int) center.v[0]; 
39           int yc = (int) center.v[1]; 
40           g.drawOval(x1, y1, diameter, diameter); 
41           g.fillOval(xc, yc, 2, 2); 
42           g.drawString("(" + xc + "," + yc + ")", xc + 3, yc + 3); 
43           g.drawLine(xc, yc, xc - radius, yc); 
44       } 
45    
46       public String toString() { 
47           return 
48                   "xc,yc,r=" + center.v[0] + "," + center.v[1] + "," + radius; 
49       } 
50    
51    
52       public boolean inside(Vec2d p) { 
53    
54           Vec2d n = new Vec2d(p); 
55           n.sub(center); 
56           if (n.dot(n) > radiusSq) 
57               return false; 
58           else 
59               return true; 
60       } 
61    
62       public Vec2d intersect(Ray2d ray) { 
63    
64           double t,          /* where the ray intersects */ 
65                   // vDotV = distance square from sphere center to 
66                   // ray origin 
67                   vDotV, 
68                   dDotV, 
69                   thc;        /* length sqare of the half chord */ 
70    
71           // vector to the center of the sphere from 
72           // ray origin 
73           Vec2d v 
74                   = new Vec2d(center.v[0], center.v[1]); 
75           boolean inside = false; 
76           //System.out.println(ray); 
77    
78           /* use the closest approach algorithm */ 
79           // v = v - ray.p 
80           //System.out.println(v); 
81           v.sub(ray.p); 
82           // loc = v^2 
83           vDotV = v.dot(v); 
84           // Are we inside the circle? 
85           if (vDotV <= radiusSq) 
86               inside = true; 
87    
88           // dDotV = D dot V = 
89           // cos(angle between ray and vec to center); 
90           dDotV = v.dot(ray.d);   /* find the closest approach */ 
91    
92           // object is behind the ray origin 
93           if ((inside != true) && (dDotV <= 0.0)) 
94               return null; 
95    
96           /* compute the half chord square from the ray intersection to the 
97              intersection normal. */ 
98           thc = (dDotV * dDotV) - vDotV + radiusSq; 
99           if (thc < 0.0) 
100              return null;   /* ray misses the sphere */ 
101   
102          /* find the ray intersection */ 
103          if (inside == true) 
104              t = dDotV + Math.sqrt(thc); 
105          else 
106              t = dDotV - Math.sqrt(thc); 
107   
108          return ray.vecOnLine((double) t); 
109      } 
110  } 
111