/Users/lyon/j4p/src/ip/transforms/GeometryUtils.java

1    /* 
2     * Created by IntelliJ IDEA. 
3     * User: lyon 
4     * Date: Feb 15, 2003 
5     * Time: 1:25:41 PM 
6     * To change template for new class use 
7     * Code Style | Class Templates options (Tools | IDE Options). 
8     */ 
9    package ip.transforms; 
10    
11   import java.awt.*; 
12    
13   public class GeometryUtils { 
14       private static void nextPointTest() { 
15           System.out.println( 
16                   "input line:(0,10)->(100,100)" + 
17                   " next point is:" + getNextPointOnLine( 
18                           new Point(0, 10), new Point(100, 100)) 
19           ); 
20       } 
21    
22       public static void getNextDirectionTest() { 
23           System.out.println( 
24                   "The points 1,1 and 23,23 have the next direction of:" 
25                   + getNextDirection(new Point(1, 1), new Point(23, 23)) 
26           ); 
27       } 
28    
29       /** 
30        *  getNextDirection returns an integer ranging from -1 to 8. 
31        *  -1 means it does not know the direction 
32        *  Here are the 8 directions 
33        *  6 7 8 
34        *  5   1 
35        *  4 3 2 
36        */ 
37       public static int getNextDirection(Point startPoint, Point endPoint) { 
38           Point p2 = getNextPointOnLine(startPoint, endPoint); 
39           int x1 = startPoint.x; 
40           int y1 = startPoint.y; 
41           int x2 = p2.x; 
42           int y2 = p2.y; 
43           int dx = x2 - x1; 
44           int dy = y2 - y1; 
45           if (dx > 0 && dy < 0) return 2; 
46           if (dx == 0 && dy < 0) return 3; 
47           if (dx < 0 && dy < 0) return 4; 
48           if (dx < 0 && dy == 0) return 5; 
49           if (dx < 0 && dy > 0) return 6; 
50           if (dx == 0 && dy > 0) return 7; 
51           if (dx > 0 && dy > 0) return 8; 
52           if (dx > 0 && dy == 0) return 1; 
53           return -1; 
54       } 
55    
56       /** 
57        * grabs a few points in the general direction 
58        */ 
59       public static Points getNextPoints(Point p1, Point p2) { 
60           Point p0 = getNextPointOnLine(p1, p2); 
61           Points p = new Points(); 
62    
63           int dx = p0.x - p1.x; 
64           int dy = p0.y - p1.y; 
65           /** 
66            *  00,01,10,11 
67            */ 
68    
69           p.addPoint(p0); 
70           p.addPoint(new Point(p0.x + dx, p0.y)); 
71           p.addPoint(new Point(p0.x, p0.y + dy)); 
72           p.addPoint(new Point(p0.x + dx, p0.y + dy)); 
73           return p; 
74    
75       } 
76    
77       /** 
78        *  Use Bresenham's algorithm to get the next point on a line. 
79        */ 
80    
81       public static Point getNextPointOnLine(Point p1, Point p2) { 
82           int x1 = p1.x; 
83           int y1 = p1.y; 
84           int x2 = p2.x; 
85           int y2 = p2.y; 
86           int x = x1; 
87           int y = y1; 
88           int d = 0; 
89           int dx = x2 - x1; 
90           int dy = y2 - y1; 
91           int c, m; 
92           int xInc = 1; 
93           int yInc = 1; 
94    
95           if (dx < 0) { 
96               xInc = -1; 
97               dx = -dx; 
98           } 
99    
100          if (dy < 0) { 
101              yInc = -1; 
102              dy = -dy; 
103          } 
104   
105          if (dy <= dx) { 
106              c = 2 * dx; 
107              m = 2 * dy; 
108   
109              x += xInc; 
110   
111              d += m; 
112   
113              if (d > dx) { 
114                  y += yInc; 
115                  d -= c; 
116              } 
117              return new Point(x, y); 
118          } 
119   
120          c = 2 * dy; 
121          m = 2 * dx; 
122          y += yInc; 
123   
124          d += m; 
125   
126          if (d > dy) { 
127              x += xInc; 
128              d -= c; 
129          } 
130          return new Point(x, y); 
131   
132      } 
133   
134      static void drawLine(Graphics g, int x1, int y1, int x2, int y2) { 
135          int x = x1; 
136          int y = y1; 
137          int d = 0; 
138          int hx = x2 - x1; 
139          int hy = y2 - y1; 
140          int c, M; 
141          int xInc = 1; 
142          int yInc = 1; 
143   
144          if (hx < 0) { 
145              xInc = -1; 
146              hx = -hx; 
147          } 
148   
149          if (hy < 0) { 
150              yInc = -1; 
151              hy = -hy; 
152          } 
153   
154          if (hy <= hx) { 
155              c = 2 * hx; 
156              M = 2 * hy; 
157   
158              for (; ;) { 
159                  ConvolutionUtils.drawPel(g, x, y); 
160   
161                  if (x == x2) break; 
162   
163                  x += xInc; 
164   
165                  d += M; 
166   
167                  if (d > hx) { 
168                      y += yInc; 
169                      d -= c; 
170                  } 
171   
172              } 
173   
174          } else { 
175              c = 2 * hy; 
176              M = 2 * hx; 
177   
178              for (; ;) { 
179                  ConvolutionUtils.drawPel(g, x, y); 
180   
181                  if (y == y2) break; 
182   
183                  y += yInc; 
184   
185                  d += M; 
186   
187                  if (d > hy) { 
188                      x += xInc; 
189                      d -= c; 
190                  } 
191              } 
192          } 
193      } 
194  } 
195