1 |
|
---|
2 |
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 |
package base.topology; |
---|
11 |
|
---|
12 |
import java.awt.Graphics2D; |
---|
13 |
import java.awt.Stroke; |
---|
14 |
import java.awt.Color; |
---|
15 |
import java.awt.Point; |
---|
16 |
import java.awt.geom.Line2D; |
---|
17 |
import base.drawable.CoordPixelXform; |
---|
18 |
import base.drawable.DrawnBox; |
---|
19 |
|
---|
20 |
public class Arrow |
---|
21 |
{ |
---|
22 |
private static int Head_Length = 15; |
---|
23 |
private static int Head_Half_Width = 5; |
---|
24 |
|
---|
25 |
|
---|
26 |
public static void setHeadLength( int new_length ) |
---|
27 |
{ |
---|
28 |
Head_Length = new_length; |
---|
29 |
} |
---|
30 |
|
---|
31 |
|
---|
32 |
public static void setHeadWidth( int new_width ) |
---|
33 |
{ |
---|
34 |
Head_Half_Width = new_width / 2; |
---|
35 |
if ( Head_Half_Width < 1 ) |
---|
36 |
Head_Half_Width = 1; |
---|
37 |
} |
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 |
private static int drawForward( Graphics2D g, Color color, Stroke stroke, |
---|
45 |
CoordPixelXform coord_xform, |
---|
46 |
DrawnBox last_drawn_pos, |
---|
47 |
double start_time, float start_ypos, |
---|
48 |
double final_time, float final_ypos ) |
---|
49 |
{ |
---|
50 |
int iStart, jStart, iFinal, jFinal; |
---|
51 |
iStart = coord_xform.convertTimeToPixel( start_time ); |
---|
52 |
iFinal = coord_xform.convertTimeToPixel( final_time ); |
---|
53 |
|
---|
54 |
|
---|
55 |
if ( last_drawn_pos.coversArrow( iStart, iFinal ) ) |
---|
56 |
return 0; |
---|
57 |
last_drawn_pos.set( iStart, iFinal ); |
---|
58 |
|
---|
59 |
jStart = coord_xform.convertRowToPixel( start_ypos ); |
---|
60 |
jFinal = coord_xform.convertRowToPixel( final_ypos ); |
---|
61 |
|
---|
62 |
boolean isStartVtxInImg, isFinalVtxInImg; |
---|
63 |
isStartVtxInImg = iStart > 0 ; |
---|
64 |
isFinalVtxInImg = iFinal < coord_xform.getImageWidth(); |
---|
65 |
|
---|
66 |
boolean isSlopeNonComputable = false; |
---|
67 |
double slope = 0.0; |
---|
68 |
if ( iStart != iFinal ) |
---|
69 |
slope = (double) ( jFinal - jStart ) / ( iFinal - iStart ); |
---|
70 |
else { |
---|
71 |
isSlopeNonComputable = true; |
---|
72 |
if ( jStart != jFinal ) |
---|
73 |
if ( jFinal > jStart ) |
---|
74 |
slope = Double.POSITIVE_INFINITY; |
---|
75 |
else |
---|
76 |
slope = Double.NEGATIVE_INFINITY; |
---|
77 |
else |
---|
78 |
|
---|
79 |
slope = Double.NaN; |
---|
80 |
} |
---|
81 |
|
---|
82 |
int iHead, iTail, jHead, jTail; |
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 |
if ( isStartVtxInImg ) { |
---|
87 |
iHead = iStart; |
---|
88 |
jHead = jStart; |
---|
89 |
} |
---|
90 |
else { |
---|
91 |
if ( isSlopeNonComputable ) |
---|
92 |
return 0; |
---|
93 |
iHead = 0; |
---|
94 |
jHead = (int) Math.rint( jStart - slope * iStart ); |
---|
95 |
} |
---|
96 |
|
---|
97 |
|
---|
98 |
if ( isFinalVtxInImg ) { |
---|
99 |
iTail = iFinal; |
---|
100 |
jTail = jFinal; |
---|
101 |
} |
---|
102 |
else { |
---|
103 |
if ( isSlopeNonComputable ) |
---|
104 |
return 0; |
---|
105 |
iTail = coord_xform.getImageWidth(); |
---|
106 |
jTail = (int) Math.rint( jFinal + slope * ( iTail - iFinal ) ); |
---|
107 |
} |
---|
108 |
|
---|
109 |
int iLeft, jLeft, iRight, jRight; |
---|
110 |
|
---|
111 |
iLeft = 0; jLeft = 0; iRight = 0; jRight = 0; |
---|
112 |
if ( isFinalVtxInImg ) { |
---|
113 |
|
---|
114 |
double cosA, sinA; |
---|
115 |
double xBase, yBase, xOff, yOff; |
---|
116 |
if ( isSlopeNonComputable ) { |
---|
117 |
if ( slope == Double.NaN ) { |
---|
118 |
cosA = 1.0d; |
---|
119 |
sinA = 0.0d; |
---|
120 |
} |
---|
121 |
else { |
---|
122 |
if ( slope == Double.POSITIVE_INFINITY ) { |
---|
123 |
cosA = 0.0d; |
---|
124 |
sinA = 1.0d; |
---|
125 |
} |
---|
126 |
else { |
---|
127 |
cosA = 0.0d; |
---|
128 |
sinA = -1.0d; |
---|
129 |
} |
---|
130 |
} |
---|
131 |
} |
---|
132 |
else { |
---|
133 |
cosA = 1.0d / Math.sqrt( 1.0d + slope * slope ); |
---|
134 |
sinA = slope * cosA; |
---|
135 |
} |
---|
136 |
xBase = iTail - Head_Length * cosA; |
---|
137 |
yBase = jTail - Head_Length * sinA; |
---|
138 |
xOff = Head_Half_Width * sinA; |
---|
139 |
yOff = Head_Half_Width * cosA; |
---|
140 |
iLeft = (int) Math.round( xBase + xOff ); |
---|
141 |
jLeft = (int) Math.round( yBase - yOff ); |
---|
142 |
iRight = (int) Math.round( xBase - xOff ); |
---|
143 |
jRight = (int) Math.round( yBase + yOff ); |
---|
144 |
} |
---|
145 |
|
---|
146 |
Stroke orig_stroke = null; |
---|
147 |
if ( stroke != null ) { |
---|
148 |
orig_stroke = g.getStroke(); |
---|
149 |
g.setStroke( stroke ); |
---|
150 |
} |
---|
151 |
|
---|
152 |
g.setColor( color ); |
---|
153 |
|
---|
154 |
g.drawLine( iHead, jHead, iTail, jTail ); |
---|
155 |
|
---|
156 |
if ( stroke != null ) |
---|
157 |
g.setStroke( orig_stroke ); |
---|
158 |
|
---|
159 |
|
---|
160 |
if ( isFinalVtxInImg ) { |
---|
161 |
g.drawLine( iTail, jTail, iLeft, jLeft ); |
---|
162 |
g.drawLine( iLeft, jLeft, iRight, jRight ); |
---|
163 |
g.drawLine( iRight, jRight, iTail, jTail ); |
---|
164 |
} |
---|
165 |
|
---|
166 |
return 1; |
---|
167 |
} |
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 |
private static int drawBackward( Graphics2D g, Color color, Stroke stroke, |
---|
175 |
CoordPixelXform coord_xform, |
---|
176 |
DrawnBox last_drawn_pos, |
---|
177 |
double start_time, float start_ypos, |
---|
178 |
double final_time, float final_ypos ) |
---|
179 |
{ |
---|
180 |
int iStart, jStart, iFinal, jFinal; |
---|
181 |
iStart = coord_xform.convertTimeToPixel( start_time ); |
---|
182 |
iFinal = coord_xform.convertTimeToPixel( final_time ); |
---|
183 |
|
---|
184 |
|
---|
185 |
if ( last_drawn_pos.coversArrow( iStart, iFinal ) ) |
---|
186 |
return 0; |
---|
187 |
last_drawn_pos.set( iStart, iFinal ); |
---|
188 |
|
---|
189 |
jStart = coord_xform.convertRowToPixel( start_ypos ); |
---|
190 |
jFinal = coord_xform.convertRowToPixel( final_ypos ); |
---|
191 |
|
---|
192 |
boolean isStartVtxInImg, isFinalVtxInImg; |
---|
193 |
isStartVtxInImg = iStart < coord_xform.getImageWidth(); |
---|
194 |
isFinalVtxInImg = iFinal > 0; |
---|
195 |
|
---|
196 |
boolean isSlopeNonComputable = false; |
---|
197 |
double slope = 0.0; |
---|
198 |
if ( iStart != iFinal ) |
---|
199 |
slope = (double) ( jStart - jFinal ) / ( iStart - iFinal ); |
---|
200 |
else { |
---|
201 |
isSlopeNonComputable = true; |
---|
202 |
if ( jStart != jFinal ) |
---|
203 |
if ( jStart > jFinal ) |
---|
204 |
slope = Double.POSITIVE_INFINITY; |
---|
205 |
else |
---|
206 |
slope = Double.NEGATIVE_INFINITY; |
---|
207 |
else |
---|
208 |
|
---|
209 |
slope = Double.NaN; |
---|
210 |
} |
---|
211 |
|
---|
212 |
int iHead, iTail, jHead, jTail; |
---|
213 |
|
---|
214 |
|
---|
215 |
|
---|
216 |
if ( isStartVtxInImg ) { |
---|
217 |
iHead = iStart; |
---|
218 |
jHead = jStart; |
---|
219 |
} |
---|
220 |
else { |
---|
221 |
if ( isSlopeNonComputable ) |
---|
222 |
return 0; |
---|
223 |
iHead = coord_xform.getImageWidth(); |
---|
224 |
jHead = (int) Math.rint( jStart + slope * ( iHead - iStart ) ); |
---|
225 |
} |
---|
226 |
|
---|
227 |
|
---|
228 |
if ( isFinalVtxInImg ) { |
---|
229 |
iTail = iFinal; |
---|
230 |
jTail = jFinal; |
---|
231 |
} |
---|
232 |
else { |
---|
233 |
if ( isSlopeNonComputable ) |
---|
234 |
return 0; |
---|
235 |
iTail = 0; |
---|
236 |
jTail = (int) Math.rint( jFinal - slope * iFinal ); |
---|
237 |
} |
---|
238 |
|
---|
239 |
int iLeft, jLeft, iRight, jRight; |
---|
240 |
|
---|
241 |
iLeft = 0; jLeft = 0; iRight = 0; jRight = 0; |
---|
242 |
if ( isFinalVtxInImg ) { |
---|
243 |
|
---|
244 |
double cosA, sinA; |
---|
245 |
double xBase, yBase, xOff, yOff; |
---|
246 |
if ( isSlopeNonComputable ) { |
---|
247 |
if ( slope == Double.NaN ) { |
---|
248 |
cosA = -1.0d; |
---|
249 |
sinA = 0.0d; |
---|
250 |
} |
---|
251 |
else { |
---|
252 |
if ( slope == Double.POSITIVE_INFINITY ) { |
---|
253 |
cosA = 0.0d; |
---|
254 |
sinA = -1.0d; |
---|
255 |
} |
---|
256 |
else { |
---|
257 |
cosA = 0.0d; |
---|
258 |
sinA = 1.0d; |
---|
259 |
} |
---|
260 |
} |
---|
261 |
} |
---|
262 |
else { |
---|
263 |
cosA = - 1.0d / Math.sqrt( 1.0d + slope * slope ); |
---|
264 |
sinA = slope * cosA; |
---|
265 |
} |
---|
266 |
xBase = iTail - Head_Length * cosA; |
---|
267 |
yBase = jTail - Head_Length * sinA; |
---|
268 |
xOff = Head_Half_Width * sinA; |
---|
269 |
yOff = Head_Half_Width * cosA; |
---|
270 |
iLeft = (int) Math.round( xBase + xOff ); |
---|
271 |
jLeft = (int) Math.round( yBase - yOff ); |
---|
272 |
iRight = (int) Math.round( xBase - xOff ); |
---|
273 |
jRight = (int) Math.round( yBase + yOff ); |
---|
274 |
} |
---|
275 |
|
---|
276 |
Stroke orig_stroke = null; |
---|
277 |
if ( stroke != null ) { |
---|
278 |
orig_stroke = g.getStroke(); |
---|
279 |
g.setStroke( stroke ); |
---|
280 |
} |
---|
281 |
g.setColor( color ); |
---|
282 |
|
---|
283 |
|
---|
284 |
g.drawLine( iTail, jTail, iHead, jHead ); |
---|
285 |
|
---|
286 |
if ( isFinalVtxInImg ) { |
---|
287 |
g.drawLine( iTail, jTail, iLeft, jLeft ); |
---|
288 |
g.drawLine( iLeft, jLeft, iRight, jRight ); |
---|
289 |
g.drawLine( iRight, jRight, iTail, jTail ); |
---|
290 |
} |
---|
291 |
|
---|
292 |
if ( stroke != null ) |
---|
293 |
g.setStroke( orig_stroke ); |
---|
294 |
|
---|
295 |
return 1; |
---|
296 |
} |
---|
297 |
|
---|
298 |
|
---|
299 |
public static int draw( Graphics2D g, Color color, Stroke stroke, |
---|
300 |
CoordPixelXform coord_xform, |
---|
301 |
DrawnBox last_drawn_pos, |
---|
302 |
double start_time, float start_ypos, |
---|
303 |
double final_time, float final_ypos ) |
---|
304 |
{ |
---|
305 |
if ( start_time < final_time ) |
---|
306 |
return drawForward( g, color, stroke, |
---|
307 |
coord_xform, last_drawn_pos, |
---|
308 |
start_time, start_ypos, |
---|
309 |
final_time, final_ypos ); |
---|
310 |
else |
---|
311 |
return drawBackward( g, color, stroke, |
---|
312 |
coord_xform, last_drawn_pos, |
---|
313 |
start_time, start_ypos, |
---|
314 |
final_time, final_ypos ); |
---|
315 |
} |
---|
316 |
} |
---|