]> git.proxmox.com Git - extjs.git/blame - extjs/packages/charts/src/draw/overrides/Path.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / charts / src / draw / overrides / Path.js
CommitLineData
6527f429
DM
1Ext.define('Ext.draw.overrides.Path', {\r
2 override: 'Ext.draw.Path',\r
3\r
4 // An arbitrary point outside the path used for hit testing with ray casting method.\r
5 rayOrigin: {\r
6 x: -10000,\r
7 y: -10000\r
8 },\r
9\r
10 /**\r
11 * Tests whether the given point is inside the path.\r
12 * @param {Number} x\r
13 * @param {Number} y\r
14 * @return {Boolean}\r
15 * @member Ext.draw.Path\r
16 */\r
17 isPointInPath: function (x, y) {\r
18 var me = this,\r
19 commands = me.commands,\r
20 solver = Ext.draw.PathUtil,\r
21 origin = me.rayOrigin,\r
22 params = me.params,\r
23 ln = commands.length,\r
24 firstX = null,\r
25 firstY = null,\r
26 lastX = 0,\r
27 lastY = 0,\r
28 count = 0,\r
29 i, j;\r
30\r
31 for (i = 0, j = 0; i < ln; i++) {\r
32 switch (commands[i]) {\r
33 case 'M':\r
34 if (firstX !== null) {\r
35 if (solver.linesIntersection(firstX, firstY, lastX, lastY, origin.x, origin.y, x, y)) {\r
36 count += 1;\r
37 }\r
38 }\r
39 firstX = lastX = params[j];\r
40 firstY = lastY = params[j + 1];\r
41 j += 2;\r
42 break;\r
43 case 'L':\r
44 if (solver.linesIntersection(lastX, lastY, params[j], params[j + 1], origin.x, origin.y, x, y)) {\r
45 count += 1;\r
46 }\r
47 lastX = params[j];\r
48 lastY = params[j + 1];\r
49 j += 2;\r
50 break;\r
51 case 'C':\r
52 count += solver.cubicLineIntersections(\r
53 lastX, params[j], params[j + 2], params[j + 4],\r
54 lastY, params[j + 1], params[j + 3], params[j + 5],\r
55 origin.x, origin.y, x, y\r
56 ).length;\r
57 lastX = params[j + 4];\r
58 lastY = params[j + 5];\r
59 j += 6;\r
60 break;\r
61 case 'Z':\r
62 if (firstX !== null) {\r
63 if (solver.linesIntersection(firstX, firstY, lastX, lastY, origin.x, origin.y, x, y)) {\r
64 count += 1;\r
65 }\r
66 }\r
67 break;\r
68 }\r
69 }\r
70 return count % 2 === 1;\r
71 },\r
72\r
73 /**\r
74 * Tests whether the given point is on the path.\r
75 * @param {Number} x\r
76 * @param {Number} y\r
77 * @return {Boolean}\r
78 * @member Ext.draw.Path\r
79 */\r
80 isPointOnPath: function (x, y) {\r
81 var me = this,\r
82 commands = me.commands,\r
83 solver = Ext.draw.PathUtil,\r
84 params = me.params,\r
85 ln = commands.length,\r
86 firstX = null,\r
87 firstY = null,\r
88 lastX = 0,\r
89 lastY = 0,\r
90 i, j;\r
91\r
92 for (i = 0, j = 0; i < ln; i++) {\r
93 switch (commands[i]) {\r
94 case 'M':\r
95 if (firstX !== null) {\r
96 if (solver.pointOnLine(firstX, firstY, lastX, lastY, x, y)) {\r
97 return true;\r
98 }\r
99 }\r
100 firstX = lastX = params[j];\r
101 firstY = lastY = params[j + 1];\r
102 j += 2;\r
103 break;\r
104 case 'L':\r
105 if (solver.pointOnLine(lastX, lastY, params[j], params[j + 1], x, y)) {\r
106 return true;\r
107 }\r
108 lastX = params[j];\r
109 lastY = params[j + 1];\r
110 j += 2;\r
111 break;\r
112 case 'C':\r
113 if (solver.pointOnCubic(\r
114 lastX, params[j], params[j + 2], params[j + 4],\r
115 lastY, params[j + 1], params[j + 3], params[j + 5], x, y)) {\r
116 return true;\r
117 }\r
118 lastX = params[j + 4];\r
119 lastY = params[j + 5];\r
120 j += 6;\r
121 break;\r
122 case 'Z':\r
123 if (firstX !== null) {\r
124 if (solver.pointOnLine(firstX, firstY, lastX, lastY, x, y)) {\r
125 return true;\r
126 }\r
127 }\r
128 break;\r
129 }\r
130 }\r
131 return false;\r
132 },\r
133\r
134 /**\r
135 * Calculates the points where the given segment intersects the path.\r
136 * If four parameters are given then the segment is considered to be a line segment,\r
137 * where given parameters are the coordinates of the start and end points.\r
138 * If eight parameters are given then the segment is considered to be\r
139 * a cubic Bezier curve segment, where given parameters are the\r
140 * coordinates of its edge points and control points.\r
141 * @param x1\r
142 * @param y1\r
143 * @param x2\r
144 * @param y2\r
145 * @param x3\r
146 * @param y3\r
147 * @param x4\r
148 * @param y4\r
149 * @return {Array}\r
150 * @member Ext.draw.Path\r
151 */\r
152 getSegmentIntersections: function (x1, y1, x2, y2, x3, y3, x4, y4) {\r
153 var me = this,\r
154 count = arguments.length,\r
155 solver = Ext.draw.PathUtil,\r
156 commands = me.commands,\r
157 params = me.params,\r
158 ln = commands.length,\r
159 firstX = null,\r
160 firstY = null,\r
161 lastX = 0,\r
162 lastY = 0,\r
163 intersections = [],\r
164 i, j, points;\r
165\r
166 for (i = 0, j = 0; i < ln; i++) {\r
167 switch (commands[i]) {\r
168 case 'M':\r
169 if (firstX !== null) {\r
170 switch (count) {\r
171 case 4:\r
172 points = solver.linesIntersection(firstX, firstY, lastX, lastY, x1, y1, x2, y2);\r
173 if (points) {\r
174 intersections.push(points);\r
175 }\r
176 break;\r
177 case 8:\r
178 points = solver.cubicLineIntersections(x1, x2, x3, x4, y1, y2, y3, y4,\r
179 firstX, firstY, lastX, lastY);\r
180 intersections.push.apply(intersections, points);\r
181 break;\r
182 }\r
183 }\r
184 firstX = lastX = params[j];\r
185 firstY = lastY = params[j + 1];\r
186 j += 2;\r
187 break;\r
188 case 'L':\r
189 switch (count) {\r
190 case 4:\r
191 points = solver.linesIntersection(lastX, lastY, params[j], params[j + 1], x1, y1, x2, y2);\r
192 if (points) {\r
193 intersections.push(points);\r
194 }\r
195 break;\r
196 case 8:\r
197 points = solver.cubicLineIntersections(x1, x2, x3, x4, y1, y2, y3, y4,\r
198 lastX, lastY, params[j], params[j + 1]);\r
199 intersections.push.apply(intersections, points);\r
200 break;\r
201 }\r
202 lastX = params[j];\r
203 lastY = params[j + 1];\r
204 j += 2;\r
205 break;\r
206 case 'C':\r
207 switch (count) {\r
208 case 4:\r
209 points = solver.cubicLineIntersections(\r
210 lastX, params[j], params[j + 2], params[j + 4],\r
211 lastY, params[j + 1], params[j + 3], params[j + 5],\r
212 x1, y1, x2, y2);\r
213 intersections.push.apply(intersections, points);\r
214 break;\r
215 case 8:\r
216 points = solver.cubicsIntersections(\r
217 lastX, params[j], params[j + 2], params[j + 4],\r
218 lastY, params[j + 1], params[j + 3], params[j + 5],\r
219 x1, x2, x3, x4, y1, y2, y3, y4);\r
220 intersections.push.apply(intersections, points);\r
221 break;\r
222 }\r
223 lastX = params[j + 4];\r
224 lastY = params[j + 5];\r
225 j += 6;\r
226 break;\r
227 case 'Z':\r
228 if (firstX !== null) {\r
229 switch (count) {\r
230 case 4:\r
231 points = solver.linesIntersection(firstX, firstY, lastX, lastY, x1, y1, x2, y2);\r
232 if (points) {\r
233 intersections.push(points);\r
234 }\r
235 break;\r
236 case 8:\r
237 points = solver.cubicLineIntersections(x1, x2, x3, x4, y1, y2, y3, y4,\r
238 firstX, firstY, lastX, lastY);\r
239 intersections.push.apply(intersections, points);\r
240 break;\r
241 }\r
242 }\r
243 break;\r
244 }\r
245 }\r
246 return intersections;\r
247 },\r
248\r
249 getIntersections: function (path) {\r
250 var me = this,\r
251 commands = me.commands,\r
252 params = me.params,\r
253 ln = commands.length,\r
254 firstX = null,\r
255 firstY = null,\r
256 lastX = 0,\r
257 lastY = 0,\r
258 intersections = [],\r
259 i, j, points;\r
260\r
261 for (i = 0, j = 0; i < ln; i++) {\r
262 switch (commands[i]) {\r
263 case 'M':\r
264 if (firstX !== null) {\r
265 points = path.getSegmentIntersections.call(path, firstX, firstY, lastX, lastY);\r
266 intersections.push.apply(intersections, points);\r
267 }\r
268 firstX = lastX = params[j];\r
269 firstY = lastY = params[j + 1];\r
270 j += 2;\r
271 break;\r
272 case 'L':\r
273 points = path.getSegmentIntersections.call(path, lastX, lastY, params[j], params[j + 1]);\r
274 intersections.push.apply(intersections, points);\r
275 lastX = params[j];\r
276 lastY = params[j + 1];\r
277 j += 2;\r
278 break;\r
279 case 'C':\r
280 points = path.getSegmentIntersections.call(path,\r
281 lastX, lastY, params[j], params[j + 1],\r
282 params[j + 2], params[j + 3], params[j + 4], params[j + 5]\r
283 );\r
284 intersections.push.apply(intersections, points);\r
285 lastX = params[j + 4];\r
286 lastY = params[j + 5];\r
287 j += 6;\r
288 break;\r
289 case 'Z':\r
290 if (firstX !== null) {\r
291 points = path.getSegmentIntersections.call(path, firstX, firstY, lastX, lastY);\r
292 intersections.push.apply(intersections, points);\r
293 }\r
294 break;\r
295 }\r
296 }\r
297 return intersections;\r
298 }\r
299});