JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / jssun / awt / geom / Order3.js
1 Clazz.declarePackage ("jssun.awt.geom");\r
2 Clazz.load (["jssun.awt.geom.Curve"], "jssun.awt.geom.Order3", ["java.awt.geom.QuadCurve2D", "jssun.awt.geom.Order2"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.x0 = 0;\r
5 this.y0 = 0;\r
6 this.cx0 = 0;\r
7 this.cy0 = 0;\r
8 this.cx1 = 0;\r
9 this.cy1 = 0;\r
10 this.x1 = 0;\r
11 this.y1 = 0;\r
12 this.xmin = 0;\r
13 this.xmax = 0;\r
14 this.xcoeff0 = 0;\r
15 this.xcoeff1 = 0;\r
16 this.xcoeff2 = 0;\r
17 this.xcoeff3 = 0;\r
18 this.ycoeff0 = 0;\r
19 this.ycoeff1 = 0;\r
20 this.ycoeff2 = 0;\r
21 this.ycoeff3 = 0;\r
22 this.TforY1 = 0;\r
23 this.YforT1 = 0;\r
24 this.TforY2 = 0;\r
25 this.YforT2 = 0;\r
26 this.TforY3 = 0;\r
27 this.YforT3 = 0;\r
28 Clazz.instantialize (this, arguments);\r
29 }, jssun.awt.geom, "Order3", jssun.awt.geom.Curve);\r
30 c$.insert = Clazz.defineMethod (c$, "insert", \r
31 function (curves, tmp, x0, y0, cx0, cy0, cx1, cy1, x1, y1, direction) {\r
32 var numparams = jssun.awt.geom.Order3.getHorizontalParams (y0, cy0, cy1, y1, tmp);\r
33 if (numparams == 0) {\r
34 jssun.awt.geom.Order3.addInstance (curves, x0, y0, cx0, cy0, cx1, cy1, x1, y1, direction);\r
35 return;\r
36 }tmp[3] = x0;\r
37 tmp[4] = y0;\r
38 tmp[5] = cx0;\r
39 tmp[6] = cy0;\r
40 tmp[7] = cx1;\r
41 tmp[8] = cy1;\r
42 tmp[9] = x1;\r
43 tmp[10] = y1;\r
44 var t = tmp[0];\r
45 if (numparams > 1 && t > tmp[1]) {\r
46 tmp[0] = tmp[1];\r
47 tmp[1] = t;\r
48 t = tmp[0];\r
49 }jssun.awt.geom.Order3.split (tmp, 3, t);\r
50 if (numparams > 1) {\r
51 t = (tmp[1] - t) / (1 - t);\r
52 jssun.awt.geom.Order3.split (tmp, 9, t);\r
53 }var index = 3;\r
54 if (direction == -1) {\r
55 index += numparams * 6;\r
56 }while (numparams >= 0) {\r
57 jssun.awt.geom.Order3.addInstance (curves, tmp[index + 0], tmp[index + 1], tmp[index + 2], tmp[index + 3], tmp[index + 4], tmp[index + 5], tmp[index + 6], tmp[index + 7], direction);\r
58 numparams--;\r
59 if (direction == 1) {\r
60 index += 6;\r
61 } else {\r
62 index -= 6;\r
63 }}\r
64 }, "java.util.Vector,~A,~N,~N,~N,~N,~N,~N,~N,~N,~N");\r
65 c$.addInstance = Clazz.defineMethod (c$, "addInstance", \r
66 function (curves, x0, y0, cx0, cy0, cx1, cy1, x1, y1, direction) {\r
67 if (y0 > y1) {\r
68 curves.add ( new jssun.awt.geom.Order3 (x1, y1, cx1, cy1, cx0, cy0, x0, y0, -direction));\r
69 } else if (y1 > y0) {\r
70 curves.add ( new jssun.awt.geom.Order3 (x0, y0, cx0, cy0, cx1, cy1, x1, y1, direction));\r
71 }}, "java.util.Vector,~N,~N,~N,~N,~N,~N,~N,~N,~N");\r
72 c$.getHorizontalParams = Clazz.defineMethod (c$, "getHorizontalParams", \r
73 function (c0, cp0, cp1, c1, ret) {\r
74 if (c0 <= cp0 && cp0 <= cp1 && cp1 <= c1) {\r
75 return 0;\r
76 }c1 -= cp1;\r
77 cp1 -= cp0;\r
78 cp0 -= c0;\r
79 ret[0] = cp0;\r
80 ret[1] = (cp1 - cp0) * 2;\r
81 ret[2] = (c1 - cp1 - cp1 + cp0);\r
82 var numroots = java.awt.geom.QuadCurve2D.solveQuadratic (ret, ret);\r
83 var j = 0;\r
84 for (var i = 0; i < numroots; i++) {\r
85 var t = ret[i];\r
86 if (t > 0 && t < 1) {\r
87 if (j < i) {\r
88 ret[j] = t;\r
89 }j++;\r
90 }}\r
91 return j;\r
92 }, "~N,~N,~N,~N,~A");\r
93 c$.split = Clazz.defineMethod (c$, "split", \r
94 function (coords, pos, t) {\r
95 var x0;\r
96 var y0;\r
97 var cx0;\r
98 var cy0;\r
99 var cx1;\r
100 var cy1;\r
101 var x1;\r
102 var y1;\r
103 coords[pos + 12] = x1 = coords[pos + 6];\r
104 coords[pos + 13] = y1 = coords[pos + 7];\r
105 cx1 = coords[pos + 4];\r
106 cy1 = coords[pos + 5];\r
107 x1 = cx1 + (x1 - cx1) * t;\r
108 y1 = cy1 + (y1 - cy1) * t;\r
109 x0 = coords[pos + 0];\r
110 y0 = coords[pos + 1];\r
111 cx0 = coords[pos + 2];\r
112 cy0 = coords[pos + 3];\r
113 x0 = x0 + (cx0 - x0) * t;\r
114 y0 = y0 + (cy0 - y0) * t;\r
115 cx0 = cx0 + (cx1 - cx0) * t;\r
116 cy0 = cy0 + (cy1 - cy0) * t;\r
117 cx1 = cx0 + (x1 - cx0) * t;\r
118 cy1 = cy0 + (y1 - cy0) * t;\r
119 cx0 = x0 + (cx0 - x0) * t;\r
120 cy0 = y0 + (cy0 - y0) * t;\r
121 coords[pos + 2] = x0;\r
122 coords[pos + 3] = y0;\r
123 coords[pos + 4] = cx0;\r
124 coords[pos + 5] = cy0;\r
125 coords[pos + 6] = cx0 + (cx1 - cx0) * t;\r
126 coords[pos + 7] = cy0 + (cy1 - cy0) * t;\r
127 coords[pos + 8] = cx1;\r
128 coords[pos + 9] = cy1;\r
129 coords[pos + 10] = x1;\r
130 coords[pos + 11] = y1;\r
131 }, "~A,~N,~N");\r
132 Clazz.makeConstructor (c$, \r
133 function (x0, y0, cx0, cy0, cx1, cy1, x1, y1, direction) {\r
134 Clazz.superConstructor (this, jssun.awt.geom.Order3, [direction]);\r
135 if (cy0 < y0) cy0 = y0;\r
136 if (cy1 > y1) cy1 = y1;\r
137 this.x0 = x0;\r
138 this.y0 = y0;\r
139 this.cx0 = cx0;\r
140 this.cy0 = cy0;\r
141 this.cx1 = cx1;\r
142 this.cy1 = cy1;\r
143 this.x1 = x1;\r
144 this.y1 = y1;\r
145 this.xmin = Math.min (Math.min (x0, x1), Math.min (cx0, cx1));\r
146 this.xmax = Math.max (Math.max (x0, x1), Math.max (cx0, cx1));\r
147 this.xcoeff0 = x0;\r
148 this.xcoeff1 = (cx0 - x0) * 3.0;\r
149 this.xcoeff2 = (cx1 - cx0 - cx0 + x0) * 3.0;\r
150 this.xcoeff3 = x1 - (cx1 - cx0) * 3.0 - x0;\r
151 this.ycoeff0 = y0;\r
152 this.ycoeff1 = (cy0 - y0) * 3.0;\r
153 this.ycoeff2 = (cy1 - cy0 - cy0 + y0) * 3.0;\r
154 this.ycoeff3 = y1 - (cy1 - cy0) * 3.0 - y0;\r
155 this.YforT1 = this.YforT2 = this.YforT3 = y0;\r
156 }, "~N,~N,~N,~N,~N,~N,~N,~N,~N");\r
157 Clazz.overrideMethod (c$, "getOrder", \r
158 function () {\r
159 return 3;\r
160 });\r
161 Clazz.overrideMethod (c$, "getXTop", \r
162 function () {\r
163 return this.x0;\r
164 });\r
165 Clazz.overrideMethod (c$, "getYTop", \r
166 function () {\r
167 return this.y0;\r
168 });\r
169 Clazz.overrideMethod (c$, "getXBot", \r
170 function () {\r
171 return this.x1;\r
172 });\r
173 Clazz.overrideMethod (c$, "getYBot", \r
174 function () {\r
175 return this.y1;\r
176 });\r
177 Clazz.overrideMethod (c$, "getXMin", \r
178 function () {\r
179 return this.xmin;\r
180 });\r
181 Clazz.overrideMethod (c$, "getXMax", \r
182 function () {\r
183 return this.xmax;\r
184 });\r
185 Clazz.overrideMethod (c$, "getX0", \r
186 function () {\r
187 return (this.direction == 1) ? this.x0 : this.x1;\r
188 });\r
189 Clazz.overrideMethod (c$, "getY0", \r
190 function () {\r
191 return (this.direction == 1) ? this.y0 : this.y1;\r
192 });\r
193 Clazz.defineMethod (c$, "getCX0", \r
194 function () {\r
195 return (this.direction == 1) ? this.cx0 : this.cx1;\r
196 });\r
197 Clazz.defineMethod (c$, "getCY0", \r
198 function () {\r
199 return (this.direction == 1) ? this.cy0 : this.cy1;\r
200 });\r
201 Clazz.defineMethod (c$, "getCX1", \r
202 function () {\r
203 return (this.direction == -1) ? this.cx0 : this.cx1;\r
204 });\r
205 Clazz.defineMethod (c$, "getCY1", \r
206 function () {\r
207 return (this.direction == -1) ? this.cy0 : this.cy1;\r
208 });\r
209 Clazz.overrideMethod (c$, "getX1", \r
210 function () {\r
211 return (this.direction == -1) ? this.x0 : this.x1;\r
212 });\r
213 Clazz.overrideMethod (c$, "getY1", \r
214 function () {\r
215 return (this.direction == -1) ? this.y0 : this.y1;\r
216 });\r
217 Clazz.overrideMethod (c$, "TforY", \r
218 function (y) {\r
219 if (y <= this.y0) return 0;\r
220 if (y >= this.y1) return 1;\r
221 if (y == this.YforT1) return this.TforY1;\r
222 if (y == this.YforT2) return this.TforY2;\r
223 if (y == this.YforT3) return this.TforY3;\r
224 if (this.ycoeff3 == 0.0) {\r
225 return jssun.awt.geom.Order2.TforY (y, this.ycoeff0, this.ycoeff1, this.ycoeff2);\r
226 }var a = this.ycoeff2 / this.ycoeff3;\r
227 var b = this.ycoeff1 / this.ycoeff3;\r
228 var c = (this.ycoeff0 - y) / this.ycoeff3;\r
229 var Q = (a * a - 3.0 * b) / 9.0;\r
230 var R = (2.0 * a * a * a - 9.0 * a * b + 27.0 * c) / 54.0;\r
231 var R2 = R * R;\r
232 var Q3 = Q * Q * Q;\r
233 var a_3 = a / 3.0;\r
234 var t;\r
235 if (R2 < Q3) {\r
236 var theta = Math.acos (R / Math.sqrt (Q3));\r
237 Q = -2.0 * Math.sqrt (Q);\r
238 t = this.refine (a, b, c, y, Q * Math.cos (theta / 3.0) - a_3);\r
239 if (t < 0) {\r
240 t = this.refine (a, b, c, y, Q * Math.cos ((theta + 6.283185307179586) / 3.0) - a_3);\r
241 }if (t < 0) {\r
242 t = this.refine (a, b, c, y, Q * Math.cos ((theta - 6.283185307179586) / 3.0) - a_3);\r
243 }} else {\r
244 var neg = (R < 0.0);\r
245 var S = Math.sqrt (R2 - Q3);\r
246 if (neg) {\r
247 R = -R;\r
248 }var A = Math.pow (R + S, 0.3333333333333333);\r
249 if (!neg) {\r
250 A = -A;\r
251 }var B = (A == 0.0) ? 0.0 : (Q / A);\r
252 t = this.refine (a, b, c, y, (A + B) - a_3);\r
253 }if (t < 0) {\r
254 var t0 = 0;\r
255 var t1 = 1;\r
256 while (true) {\r
257 t = (t0 + t1) / 2;\r
258 if (t == t0 || t == t1) {\r
259 break;\r
260 }var yt = this.YforT (t);\r
261 if (yt < y) {\r
262 t0 = t;\r
263 } else if (yt > y) {\r
264 t1 = t;\r
265 } else {\r
266 break;\r
267 }}\r
268 }if (t >= 0) {\r
269 this.TforY3 = this.TforY2;\r
270 this.YforT3 = this.YforT2;\r
271 this.TforY2 = this.TforY1;\r
272 this.YforT2 = this.YforT1;\r
273 this.TforY1 = t;\r
274 this.YforT1 = y;\r
275 }return t;\r
276 }, "~N");\r
277 Clazz.defineMethod (c$, "refine", \r
278 function (a, b, c, target, t) {\r
279 if (t < -0.1 || t > 1.1) {\r
280 return -1;\r
281 }var y = this.YforT (t);\r
282 var t0;\r
283 var t1;\r
284 if (y < target) {\r
285 t0 = t;\r
286 t1 = 1;\r
287 } else {\r
288 t0 = 0;\r
289 t1 = t;\r
290 }var origt = t;\r
291 var origy = y;\r
292 var useslope = true;\r
293 while (y != target) {\r
294 if (!useslope) {\r
295 var t2 = (t0 + t1) / 2;\r
296 if (t2 == t0 || t2 == t1) {\r
297 break;\r
298 }t = t2;\r
299 } else {\r
300 var slope = this.dYforT (t, 1);\r
301 if (slope == 0) {\r
302 useslope = false;\r
303 continue;\r
304 }var t2 = t + ((target - y) / slope);\r
305 if (t2 == t || t2 <= t0 || t2 >= t1) {\r
306 useslope = false;\r
307 continue;\r
308 }t = t2;\r
309 }y = this.YforT (t);\r
310 if (y < target) {\r
311 t0 = t;\r
312 } else if (y > target) {\r
313 t1 = t;\r
314 } else {\r
315 break;\r
316 }}\r
317 var verbose = false;\r
318 if (false && t >= 0 && t <= 1) {\r
319 y = this.YforT (t);\r
320 var tdiff = jssun.awt.geom.Curve.diffbits (t, origt);\r
321 var ydiff = jssun.awt.geom.Curve.diffbits (y, origy);\r
322 var yerr = jssun.awt.geom.Curve.diffbits (y, target);\r
323 if (yerr > 0 || (verbose && tdiff > 0)) {\r
324 System.out.println ("target was y = " + target);\r
325 System.out.println ("original was y = " + origy + ", t = " + origt);\r
326 System.out.println ("final was y = " + y + ", t = " + t);\r
327 System.out.println ("t diff is " + tdiff);\r
328 System.out.println ("y diff is " + ydiff);\r
329 System.out.println ("y error is " + yerr);\r
330 var tlow = jssun.awt.geom.Curve.prev (t);\r
331 var ylow = this.YforT (tlow);\r
332 var thi = jssun.awt.geom.Curve.next (t);\r
333 var yhi = this.YforT (thi);\r
334 if (Math.abs (target - ylow) < Math.abs (target - y) || Math.abs (target - yhi) < Math.abs (target - y)) {\r
335 System.out.println ("adjacent y's = [" + ylow + ", " + yhi + "]");\r
336 }}}return (t > 1) ? -1 : t;\r
337 }, "~N,~N,~N,~N,~N");\r
338 Clazz.overrideMethod (c$, "XforY", \r
339 function (y) {\r
340 if (y <= this.y0) {\r
341 return this.x0;\r
342 }if (y >= this.y1) {\r
343 return this.x1;\r
344 }return this.XforT (this.TforY (y));\r
345 }, "~N");\r
346 Clazz.overrideMethod (c$, "XforT", \r
347 function (t) {\r
348 return (((this.xcoeff3 * t) + this.xcoeff2) * t + this.xcoeff1) * t + this.xcoeff0;\r
349 }, "~N");\r
350 Clazz.overrideMethod (c$, "YforT", \r
351 function (t) {\r
352 return (((this.ycoeff3 * t) + this.ycoeff2) * t + this.ycoeff1) * t + this.ycoeff0;\r
353 }, "~N");\r
354 Clazz.overrideMethod (c$, "dXforT", \r
355 function (t, deriv) {\r
356 switch (deriv) {\r
357 case 0:\r
358 return (((this.xcoeff3 * t) + this.xcoeff2) * t + this.xcoeff1) * t + this.xcoeff0;\r
359 case 1:\r
360 return ((3 * this.xcoeff3 * t) + 2 * this.xcoeff2) * t + this.xcoeff1;\r
361 case 2:\r
362 return (6 * this.xcoeff3 * t) + 2 * this.xcoeff2;\r
363 case 3:\r
364 return 6 * this.xcoeff3;\r
365 default:\r
366 return 0;\r
367 }\r
368 }, "~N,~N");\r
369 Clazz.overrideMethod (c$, "dYforT", \r
370 function (t, deriv) {\r
371 switch (deriv) {\r
372 case 0:\r
373 return (((this.ycoeff3 * t) + this.ycoeff2) * t + this.ycoeff1) * t + this.ycoeff0;\r
374 case 1:\r
375 return ((3 * this.ycoeff3 * t) + 2 * this.ycoeff2) * t + this.ycoeff1;\r
376 case 2:\r
377 return (6 * this.ycoeff3 * t) + 2 * this.ycoeff2;\r
378 case 3:\r
379 return 6 * this.ycoeff3;\r
380 default:\r
381 return 0;\r
382 }\r
383 }, "~N,~N");\r
384 Clazz.overrideMethod (c$, "nextVertical", \r
385 function (t0, t1) {\r
386 var eqn =  Clazz.newDoubleArray (-1, [this.xcoeff1, 2 * this.xcoeff2, 3 * this.xcoeff3]);\r
387 var numroots = java.awt.geom.QuadCurve2D.solveQuadratic (eqn, eqn);\r
388 for (var i = 0; i < numroots; i++) {\r
389 if (eqn[i] > t0 && eqn[i] < t1) {\r
390 t1 = eqn[i];\r
391 }}\r
392 return t1;\r
393 }, "~N,~N");\r
394 Clazz.overrideMethod (c$, "enlarge", \r
395 function (r) {\r
396 r.add (this.x0, this.y0);\r
397 var eqn =  Clazz.newDoubleArray (-1, [this.xcoeff1, 2 * this.xcoeff2, 3 * this.xcoeff3]);\r
398 var numroots = java.awt.geom.QuadCurve2D.solveQuadratic (eqn, eqn);\r
399 for (var i = 0; i < numroots; i++) {\r
400 var t = eqn[i];\r
401 if (t > 0 && t < 1) {\r
402 r.add (this.XforT (t), this.YforT (t));\r
403 }}\r
404 r.add (this.x1, this.y1);\r
405 }, "java.awt.geom.Rectangle2D");\r
406 Clazz.defineMethod (c$, "getSubCurve", \r
407 function (ystart, yend, dir) {\r
408 if (ystart <= this.y0 && yend >= this.y1) {\r
409 return this.getWithDirection (dir);\r
410 }var eqn =  Clazz.newDoubleArray (14, 0);\r
411 var t0;\r
412 var t1;\r
413 t0 = this.TforY (ystart);\r
414 t1 = this.TforY (yend);\r
415 eqn[0] = this.x0;\r
416 eqn[1] = this.y0;\r
417 eqn[2] = this.cx0;\r
418 eqn[3] = this.cy0;\r
419 eqn[4] = this.cx1;\r
420 eqn[5] = this.cy1;\r
421 eqn[6] = this.x1;\r
422 eqn[7] = this.y1;\r
423 if (t0 > t1) {\r
424 var t = t0;\r
425 t0 = t1;\r
426 t1 = t;\r
427 }if (t1 < 1) {\r
428 jssun.awt.geom.Order3.split (eqn, 0, t1);\r
429 }var i;\r
430 if (t0 <= 0) {\r
431 i = 0;\r
432 } else {\r
433 jssun.awt.geom.Order3.split (eqn, 0, t0 / t1);\r
434 i = 6;\r
435 }return  new jssun.awt.geom.Order3 (eqn[i + 0], ystart, eqn[i + 2], eqn[i + 3], eqn[i + 4], eqn[i + 5], eqn[i + 6], yend, dir);\r
436 }, "~N,~N,~N");\r
437 Clazz.overrideMethod (c$, "getReversedCurve", \r
438 function () {\r
439 return  new jssun.awt.geom.Order3 (this.x0, this.y0, this.cx0, this.cy0, this.cx1, this.cy1, this.x1, this.y1, -this.direction);\r
440 });\r
441 Clazz.overrideMethod (c$, "getSegment", \r
442 function (coords) {\r
443 if (this.direction == 1) {\r
444 coords[0] = this.cx0;\r
445 coords[1] = this.cy0;\r
446 coords[2] = this.cx1;\r
447 coords[3] = this.cy1;\r
448 coords[4] = this.x1;\r
449 coords[5] = this.y1;\r
450 } else {\r
451 coords[0] = this.cx1;\r
452 coords[1] = this.cy1;\r
453 coords[2] = this.cx0;\r
454 coords[3] = this.cy0;\r
455 coords[4] = this.x0;\r
456 coords[5] = this.y0;\r
457 }return 3;\r
458 }, "~A");\r
459 Clazz.overrideMethod (c$, "controlPointString", \r
460 function () {\r
461 return (("(" + jssun.awt.geom.Curve.round (this.getCX0 ()) + ", " + jssun.awt.geom.Curve.round (this.getCY0 ()) + "), ") + ("(" + jssun.awt.geom.Curve.round (this.getCX1 ()) + ", " + jssun.awt.geom.Curve.round (this.getCY1 ()) + "), "));\r
462 });\r
463 });\r