Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / Polygon.js
1 Clazz.declarePackage ("java.awt");
2 Clazz.load (["java.awt.Shape", "java.awt.geom.PathIterator"], "java.awt.Polygon", ["java.lang.IndexOutOfBoundsException", "$.NegativeArraySizeException", "java.util.Arrays", "java.awt.Rectangle", "jssun.awt.geom.Crossings"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.npoints = 0;
5 this.xpoints = null;
6 this.ypoints = null;
7 this.bounds = null;
8 if (!Clazz.isClassDefined ("java.awt.Polygon.PolygonPathIterator")) {
9 java.awt.Polygon.$Polygon$PolygonPathIterator$ ();
10 }
11 Clazz.instantialize (this, arguments);
12 }, java.awt, "Polygon", null, java.awt.Shape);
13 Clazz.makeConstructor (c$, 
14 function () {
15 this.xpoints =  Clazz.newIntArray (4, 0);
16 this.ypoints =  Clazz.newIntArray (4, 0);
17 });
18 Clazz.makeConstructor (c$, 
19 function (xpoints, ypoints, npoints) {
20 if (npoints > xpoints.length || npoints > ypoints.length) {
21 throw  new IndexOutOfBoundsException ("npoints > xpoints.length || npoints > ypoints.length");
22 }if (npoints < 0) {
23 throw  new NegativeArraySizeException ("npoints < 0");
24 }this.npoints = npoints;
25 this.xpoints = java.util.Arrays.copyOf (xpoints, npoints);
26 this.ypoints = java.util.Arrays.copyOf (ypoints, npoints);
27 }, "~A,~A,~N");
28 Clazz.defineMethod (c$, "reset", 
29 function () {
30 this.npoints = 0;
31 this.bounds = null;
32 });
33 Clazz.defineMethod (c$, "invalidate", 
34 function () {
35 this.bounds = null;
36 });
37 Clazz.defineMethod (c$, "translate", 
38 function (deltaX, deltaY) {
39 for (var i = 0; i < this.npoints; i++) {
40 this.xpoints[i] += deltaX;
41 this.ypoints[i] += deltaY;
42 }
43 if (this.bounds != null) {
44 this.bounds.translate (deltaX, deltaY);
45 }}, "~N,~N");
46 Clazz.defineMethod (c$, "calculateBounds", 
47 function (xpoints, ypoints, npoints) {
48 var boundsMinX = 2147483647;
49 var boundsMinY = 2147483647;
50 var boundsMaxX = -2147483648;
51 var boundsMaxY = -2147483648;
52 for (var i = 0; i < npoints; i++) {
53 var x = xpoints[i];
54 boundsMinX = Math.min (boundsMinX, x);
55 boundsMaxX = Math.max (boundsMaxX, x);
56 var y = ypoints[i];
57 boundsMinY = Math.min (boundsMinY, y);
58 boundsMaxY = Math.max (boundsMaxY, y);
59 }
60 this.bounds =  new java.awt.Rectangle (boundsMinX, boundsMinY, boundsMaxX - boundsMinX, boundsMaxY - boundsMinY);
61 }, "~A,~A,~N");
62 Clazz.defineMethod (c$, "updateBounds", 
63 function (x, y) {
64 if (x < this.bounds.x) {
65 this.bounds.width = this.bounds.width + (this.bounds.x - x);
66 this.bounds.x = x;
67 } else {
68 this.bounds.width = Math.max (this.bounds.width, x - this.bounds.x);
69 }if (y < this.bounds.y) {
70 this.bounds.height = this.bounds.height + (this.bounds.y - y);
71 this.bounds.y = y;
72 } else {
73 this.bounds.height = Math.max (this.bounds.height, y - this.bounds.y);
74 }}, "~N,~N");
75 Clazz.defineMethod (c$, "addPoint", 
76 function (x, y) {
77 if (this.npoints >= this.xpoints.length || this.npoints >= this.ypoints.length) {
78 var newLength = this.npoints * 2;
79 if (newLength < 4) {
80 newLength = 4;
81 } else if ((newLength & (newLength - 1)) != 0) {
82 newLength = Integer.highestOneBit (newLength);
83 }this.xpoints = java.util.Arrays.copyOf (this.xpoints, newLength);
84 this.ypoints = java.util.Arrays.copyOf (this.ypoints, newLength);
85 }this.xpoints[this.npoints] = x;
86 this.ypoints[this.npoints] = y;
87 this.npoints++;
88 if (this.bounds != null) {
89 this.updateBounds (x, y);
90 }}, "~N,~N");
91 Clazz.overrideMethod (c$, "getBounds", 
92 function () {
93 return this.getBoundingBox ();
94 });
95 Clazz.defineMethod (c$, "getBoundingBox", 
96 function () {
97 if (this.npoints == 0) {
98 return  new java.awt.Rectangle ();
99 }if (this.bounds == null) {
100 this.calculateBounds (this.xpoints, this.ypoints, this.npoints);
101 }return this.bounds.getBounds ();
102 });
103 Clazz.defineMethod (c$, "contains", 
104 function (p) {
105 return this.contains (p.x, p.y);
106 }, "java.awt.Point");
107 Clazz.defineMethod (c$, "contains", 
108 function (x, y) {
109 return this.contains (x, y);
110 }, "~N,~N");
111 Clazz.defineMethod (c$, "inside", 
112 function (x, y) {
113 return this.contains (x, y);
114 }, "~N,~N");
115 Clazz.overrideMethod (c$, "getBounds2D", 
116 function () {
117 return this.getBounds ();
118 });
119 Clazz.defineMethod (c$, "contains", 
120 function (x, y) {
121 if (this.npoints <= 2 || !this.getBoundingBox ().contains (x, y)) {
122 return false;
123 }var hits = 0;
124 var lastx = this.xpoints[this.npoints - 1];
125 var lasty = this.ypoints[this.npoints - 1];
126 var curx;
127 var cury;
128 for (var i = 0; i < this.npoints; lastx = curx, lasty = cury, i++) {
129 curx = this.xpoints[i];
130 cury = this.ypoints[i];
131 if (cury == lasty) {
132 continue;
133 }var leftx;
134 if (curx < lastx) {
135 if (x >= lastx) {
136 continue;
137 }leftx = curx;
138 } else {
139 if (x >= curx) {
140 continue;
141 }leftx = lastx;
142 }var test1;
143 var test2;
144 if (cury < lasty) {
145 if (y < cury || y >= lasty) {
146 continue;
147 }if (x < leftx) {
148 hits++;
149 continue;
150 }test1 = x - curx;
151 test2 = y - cury;
152 } else {
153 if (y < lasty || y >= cury) {
154 continue;
155 }if (x < leftx) {
156 hits++;
157 continue;
158 }test1 = x - lastx;
159 test2 = y - lasty;
160 }if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {
161 hits++;
162 }}
163 return ((hits & 1) != 0);
164 }, "~N,~N");
165 Clazz.defineMethod (c$, "getCrossings", 
166  function (xlo, ylo, xhi, yhi) {
167 var cross =  new jssun.awt.geom.Crossings.EvenOdd (xlo, ylo, xhi, yhi);
168 var lastx = this.xpoints[this.npoints - 1];
169 var lasty = this.ypoints[this.npoints - 1];
170 var curx;
171 var cury;
172 for (var i = 0; i < this.npoints; i++) {
173 curx = this.xpoints[i];
174 cury = this.ypoints[i];
175 if (cross.accumulateLine (lastx, lasty, curx, cury)) {
176 return null;
177 }lastx = curx;
178 lasty = cury;
179 }
180 return cross;
181 }, "~N,~N,~N,~N");
182 Clazz.defineMethod (c$, "contains", 
183 function (p) {
184 return this.contains (p.getX (), p.getY ());
185 }, "java.awt.geom.Point2D");
186 Clazz.defineMethod (c$, "intersects", 
187 function (x, y, w, h) {
188 if (this.npoints <= 0 || !this.getBoundingBox ().intersects (x, y, w, h)) {
189 return false;
190 }var cross = this.getCrossings (x, y, x + w, y + h);
191 return (cross == null || !cross.isEmpty ());
192 }, "~N,~N,~N,~N");
193 Clazz.defineMethod (c$, "intersects", 
194 function (r) {
195 return this.intersects (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
196 }, "java.awt.geom.Rectangle2D");
197 Clazz.defineMethod (c$, "contains", 
198 function (x, y, w, h) {
199 if (this.npoints <= 0 || !this.getBoundingBox ().intersects (x, y, w, h)) {
200 return false;
201 }var cross = this.getCrossings (x, y, x + w, y + h);
202 return (cross != null && cross.covers (y, y + h));
203 }, "~N,~N,~N,~N");
204 Clazz.defineMethod (c$, "contains", 
205 function (r) {
206 return this.contains (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
207 }, "java.awt.geom.Rectangle2D");
208 Clazz.defineMethod (c$, "getPathIterator", 
209 function (at) {
210 return Clazz.innerTypeInstance (java.awt.Polygon.PolygonPathIterator, this, null, this, at);
211 }, "java.awt.geom.AffineTransform");
212 Clazz.defineMethod (c$, "getPathIterator", 
213 function (at, flatness) {
214 return this.getPathIterator (at);
215 }, "java.awt.geom.AffineTransform,~N");
216 c$.$Polygon$PolygonPathIterator$ = function () {
217 Clazz.pu$h(self.c$);
218 c$ = Clazz.decorateAsClass (function () {
219 Clazz.prepareCallback (this, arguments);
220 this.poly = null;
221 this.transform = null;
222 this.index = 0;
223 Clazz.instantialize (this, arguments);
224 }, java.awt.Polygon, "PolygonPathIterator", null, java.awt.geom.PathIterator);
225 Clazz.makeConstructor (c$, 
226 function (a, b) {
227 this.poly = a;
228 this.transform = b;
229 if (a.npoints == 0) {
230 this.index = 1;
231 }}, "java.awt.Polygon,java.awt.geom.AffineTransform");
232 Clazz.overrideMethod (c$, "getWindingRule", 
233 function () {
234 return 0;
235 });
236 Clazz.overrideMethod (c$, "isDone", 
237 function () {
238 return this.index > this.poly.npoints;
239 });
240 Clazz.overrideMethod (c$, "next", 
241 function () {
242 this.index++;
243 });
244 Clazz.defineMethod (c$, "currentSegment", 
245 function (a) {
246 if (this.index >= this.poly.npoints) {
247 return 4;
248 }a[0] = this.poly.xpoints[this.index];
249 a[1] = this.poly.ypoints[this.index];
250 if (this.transform != null) {
251 this.transform.transform (a, 0, a, 0, 1);
252 }return (this.index == 0 ? 0 : 1);
253 }, "~A");
254 c$ = Clazz.p0p ();
255 };
256 Clazz.defineStatics (c$,
257 "MIN_LENGTH", 4);
258 });