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

1    package bookExamples.ch26Graphics.draw2d; 
2     
3     
4    import java.awt.*; 
5     
6    public class Oval2d extends Shape 
7            implements Intersects { 
8        int x1 = 0; 
9        int y1 = 0; 
10       int h = 1; 
11       int w = 1; 
12       int xc = 0; 
13       int yc = 0; 
14       Vec2d center; 
15    
16       public Vec2d intersect(Ray2d ray) { 
17    
18           double t1, t2,          /* where the ray intersects */ 
19                   // vDotV = distance square from sphere center to 
20                   // ray origin 
21                   vDotV, 
22                   dDotV, 
23                   dDotD, 
24                   thc;        // length sqare of the half chord 
25    
26    
27           Vec2d v;    // vector to the center of the sphere from  ray origin 
28           Vec2d d;    // Direction vector 
29    
30           /* use the closest approach algorithm */ 
31           v = new Vec2d(-center.v[0], -center.v[1]); 
32           d = new Vec2d(ray.d.v[0], ray.d.v[1]); 
33           v.add(ray.p); 
34           int wOn2 = w / 2; 
35           int hOn2 = h / 2; 
36           v.v[0] = (double) (v.v[0] / wOn2); 
37           v.v[1] = (double) (v.v[1] / hOn2); 
38           d.v[0] = (double) (d.v[0] / wOn2); 
39           d.v[1] = (double) (d.v[1] / hOn2); 
40    
41           // v = v - ray.p 
42           // loc = v^2 
43           vDotV = v.dot(v); 
44           dDotD = d.dot(d); 
45    
46           // dDotV = D dot V = 
47           // cos(angle between ray and vec to center); 
48           dDotV = v.dot(d);   /* find the closest approach */ 
49    
50    
51           /* compute the half chord square from the ray intersection to the 
52              intersection normal. */ 
53           thc = (dDotV * dDotV) - dDotD * (vDotV - 1); 
54    
55           if (thc < 0.0) 
56               return null;   /* ray misses the sphere */ 
57    
58           /* find the ray intersection */ 
59           t1 = (-dDotV + Math.sqrt(thc)) / (dDotD); 
60           t2 = (-dDotV - Math.sqrt(thc)) / (dDotD); 
61           if ((t1 > 0) && (t2 > 0)) { 
62               if (t1 < t2) 
63                   return ray.vecOnLine((double) t1); 
64               else 
65                   return ray.vecOnLine((double) t2); 
66           } else if (t1 > 0) 
67               return ray.vecOnLine((double) t1); 
68           else if (t2 > 0) 
69               return ray.vecOnLine((double) t2); 
70           else 
71               return null;        // object behind 
72       } 
73    
74       public Oval2d(int _x1, int _y1, int _x2, int _y2) { 
75           x1 = _x1; 
76           y1 = _y1; 
77           w = Math.abs(_x2 - x1); 
78           h = Math.abs(_y2 - y1); 
79           if (_x1 > _x2) x1 = _x2; 
80           if (_y1 > _y2) y1 = _y2; 
81           xc = x1 + w / 2; 
82           yc = y1 + h / 2; 
83           center = 
84                   new Vec2d(xc, yc); 
85       } 
86    
87       public void paint(Graphics g) { 
88           g.drawOval(x1, y1, w, h); 
89           g.fillOval(xc, yc, 2, 2); 
90           g.drawString("(" + xc + "," + yc + ")", xc + 3, yc + 3); 
91           g.drawLine(xc, yc, xc, yc - h / 2); 
92           g.drawLine(xc, yc, xc - w / 2, yc); 
93       } 
94   } 
95