blob: 52c4417bb2cd955ec6a5c335eb58a42170e540ea [file] [log] [blame]
caryclarkdac1d172014-06-17 05:15:38 -07001<html>
2<head>
3<div height="0" hidden="true">
caryclarkdac1d172014-06-17 05:15:38 -07004
caryclark81a478c2016-09-09 09:37:57 -07005<div id="tiger8b_h_1">
6seg=1 {{{494.348663f, 224.583771f}, {494.375946f, 224.681992f}}}
7seg=2 {{{494.375946f, 224.681992f}, {494.376221f, 224.683273f}}}
8seg=3 {{{494.376221f, 224.683273f}, {494.375397f, 224.67955f}, {494.376495f, 224.683868f}}}
9seg=4 {{{494.376495f, 224.683868f}, {494.348663f, 224.583771f}}}
10debugShowLineIntersection wtTs[0]=1 {{{494.348663,224.583771}, {494.375946,224.681992}}} {{494.375946,224.681992}} wnTs[0]=0 {{{494.375946,224.681992}, {494.376221,224.683273}}}
11debugShowQuadLineIntersection wtTs[0]=0.228424525 {{{494.376221,224.683273}, {494.375397,224.67955}, {494.376495,224.683868}}} {{494.375946,224.681992}} wnTs[0]=1 {{{494.348663,224.583771}, {494.375946,224.681992}}}
12SkOpSegment::addT insert t=0.228424525 segID=3 spanID=9
13debugShowLineIntersection wtTs[0]=0 {{{494.348663,224.583771}, {494.375946,224.681992}}} {{494.348663,224.583771}} wtTs[1]=1 {{494.375946,224.681992}} wnTs[0]=1 {{{494.376495,224.683868}, {494.348663,224.583771}}} wnTs[1]=0.0188208188
14SkOpSegment::addT insert t=0.0188208188 segID=4 spanID=10
15debugShowQuadLineIntersection wtTs[0]=0 {{{494.376221,224.683273}, {494.375397,224.67955}, {494.376495,224.683868}}} {{494.376221,224.683273}} wtTs[1]=0.228448008 {{494.375946,224.681992}} wnTs[0]=1 {{{494.375946,224.681992}, {494.376221,224.683273}}} wnTs[1]=0
16debugShowLineIntersection wtTs[0]=0 {{{494.375946,224.681992}, {494.376221,224.683273}}} {{494.375946,224.681992}} wtTs[1]=1 {{494.376221,224.683273}} wnTs[0]=0.0188208 {{{494.376495,224.683868}, {494.348663,224.583771}}} wnTs[1]=0.00622666983
17SkOpSegment::addT insert t=0.00622666983 segID=4 spanID=11
18debugShowQuadLineIntersection wtTs[0]=0 {{{494.376221,224.683273}, {494.375397,224.67955}, {494.376495,224.683868}}} {{494.376221,224.683273}} wtTs[1]=1 {{494.376495,224.683868}} wnTs[0]=0.00622667 {{{494.376495,224.683868}, {494.348663,224.583771}}} wnTs[1]=0
19c:\puregit\src\pathops\skpathopsdebug.cpp:988: fatal error: "assert((t - fDebugBaseMin > 0) == (fDebugLastMin - fDebugBaseMin > 0))"
caryclark30b9fdd2016-08-31 14:36:29 -070020pathops_unittest.exe has triggered a breakpoint.
caryclark30b9fdd2016-08-31 14:36:29 -070021
caryclarkdac1d172014-06-17 05:15:38 -070022</div>
caryclark26ad22a2015-10-16 09:03:38 -070023
caryclarkdac1d172014-06-17 05:15:38 -070024</div>
25
26<script type="text/javascript">
27
caryclark55888e42016-07-18 10:01:36 -070028 var testDivs = [
caryclark81a478c2016-09-09 09:37:57 -070029 tiger8b_h_1,
caryclark30b9fdd2016-08-31 14:36:29 -070030 ];
caryclarkdac1d172014-06-17 05:15:38 -070031
32var decimal_places = 3; // make this 3 to show more precision
33
34var tests = [];
35var testLines = [];
36var testTitles = [];
37var testIndex = 0;
38var ctx;
39
40var xmin, xmax, focusXmin, focusXmax;
41var ymin, ymax, focusYmin, focusYmax;
42var scale;
43var mouseX, mouseY;
44var srcLeft, srcTop;
45var screenWidth, screenHeight;
caryclark1049f122015-04-20 08:31:59 -070046var drawnPts, drawnLines, drawnQuads, drawnConics, drawnCubics;
caryclarkdac1d172014-06-17 05:15:38 -070047var curveT = 0;
48
49var pt_labels = 2;
50var collect_bounds = false;
51var control_lines = 0;
52var curve_t = false;
53var debug_xy = 1;
54var focus_enabled = false;
55var focus_on_selection = false;
56var step_limit = 0;
57var draw_active = false;
58var draw_add = false;
59var draw_angle = 0;
caryclark624637c2015-05-11 07:21:27 -070060var draw_coincidence = false;
caryclarkdac1d172014-06-17 05:15:38 -070061var draw_deriviatives = 0;
62var draw_hints = false;
caryclarkdac1d172014-06-17 05:15:38 -070063var draw_id = false;
64var draw_intersection = 0;
65var draw_intersectT = false;
66var draw_legend = true;
67var draw_log = false;
68var draw_mark = false;
69var draw_midpoint = false;
70var draw_op = 0;
71var draw_sequence = false;
72var draw_sort = 0;
caryclark03b03ca2015-04-23 09:13:37 -070073var draw_top = false;
caryclarkdac1d172014-06-17 05:15:38 -070074var draw_path = 3;
75var draw_computed = 0;
76var retina_scale = !!window.devicePixelRatio;
77
78var activeCount = 0;
79var addCount = 0;
80var angleCount = 0;
caryclark624637c2015-05-11 07:21:27 -070081var coinCount = 0;
caryclarkdac1d172014-06-17 05:15:38 -070082var opCount = 0;
83var sectCount = 0;
84var sortCount = 0;
caryclark03b03ca2015-04-23 09:13:37 -070085var topCount = 0;
caryclarkdac1d172014-06-17 05:15:38 -070086var markCount = 0;
87var activeMax = 0;
88var addMax = 0;
89var angleMax = 0;
caryclark624637c2015-05-11 07:21:27 -070090var coinMax = 0;
caryclarkdac1d172014-06-17 05:15:38 -070091var sectMax = 0;
92var sectMax2 = 0;
93var sortMax = 0;
caryclark03b03ca2015-04-23 09:13:37 -070094var topMax = 0;
caryclarkdac1d172014-06-17 05:15:38 -070095var markMax = 0;
96var opMax = 0;
97var stepMax = 0;
98var lastIndex = 0;
99var hasPath = false;
caryclark26ad22a2015-10-16 09:03:38 -0700100var hasAlignedPath = false;
caryclarkdac1d172014-06-17 05:15:38 -0700101var hasComputedPath = false;
caryclark54359292015-03-26 07:52:43 -0700102var angleBetween = false;
103var afterIndex = 0;
caryclarkdac1d172014-06-17 05:15:38 -0700104
105var firstActiveSpan = -1;
106var logStart = -1;
107var logRange = 0;
108
109var SPAN_ID = 0;
110var SPAN_X1 = SPAN_ID + 1;
111var SPAN_Y1 = SPAN_X1 + 1;
112var SPAN_X2 = SPAN_Y1 + 1;
113var SPAN_Y2 = SPAN_X2 + 1;
caryclark1049f122015-04-20 08:31:59 -0700114
caryclark55888e42016-07-18 10:01:36 -0700115var SPAN_L_TX = SPAN_Y2 + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700116var SPAN_L_TY = SPAN_L_TX + 1;
caryclark55888e42016-07-18 10:01:36 -0700117var SPAN_L_OTHER = SPAN_L_TY + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700118var SPAN_L_OTHERT = SPAN_L_OTHER + 1;
119var SPAN_L_OTHERI = SPAN_L_OTHERT + 1;
120var SPAN_L_SUM = SPAN_L_OTHERI + 1;
121var SPAN_L_VAL = SPAN_L_SUM + 1;
122var SPAN_L_OPP = SPAN_L_VAL + 1;
123
124var SPAN_X3 = SPAN_Y2 + 1;
125var SPAN_Y3 = SPAN_X3 + 1;
caryclark1049f122015-04-20 08:31:59 -0700126
caryclark55888e42016-07-18 10:01:36 -0700127var SPAN_Q_TX = SPAN_Y3 + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700128var SPAN_Q_TY = SPAN_Q_TX + 1;
caryclark55888e42016-07-18 10:01:36 -0700129var SPAN_Q_OTHER = SPAN_Q_TY + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700130var SPAN_Q_OTHERT = SPAN_Q_OTHER + 1;
131var SPAN_Q_OTHERI = SPAN_Q_OTHERT + 1;
132var SPAN_Q_SUM = SPAN_Q_OTHERI + 1;
133var SPAN_Q_VAL = SPAN_Q_SUM + 1;
134var SPAN_Q_OPP = SPAN_Q_VAL + 1;
135
caryclark1049f122015-04-20 08:31:59 -0700136var SPAN_K_W = SPAN_Y3 + 1;
caryclark55888e42016-07-18 10:01:36 -0700137var SPAN_K_TX = SPAN_K_W + 1;
caryclark1049f122015-04-20 08:31:59 -0700138var SPAN_K_TY = SPAN_K_TX + 1;
caryclark55888e42016-07-18 10:01:36 -0700139var SPAN_K_OTHER = SPAN_K_TY + 1;
caryclark1049f122015-04-20 08:31:59 -0700140var SPAN_K_OTHERT = SPAN_K_OTHER + 1;
141var SPAN_K_OTHERI = SPAN_K_OTHERT + 1;
142var SPAN_K_SUM = SPAN_K_OTHERI + 1;
143var SPAN_K_VAL = SPAN_K_SUM + 1;
144var SPAN_K_OPP = SPAN_K_VAL + 1;
145
caryclarkdac1d172014-06-17 05:15:38 -0700146var SPAN_X4 = SPAN_Y3 + 1;
147var SPAN_Y4 = SPAN_X4 + 1;
caryclark1049f122015-04-20 08:31:59 -0700148
caryclark55888e42016-07-18 10:01:36 -0700149var SPAN_C_TX = SPAN_Y4 + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700150var SPAN_C_TY = SPAN_C_TX + 1;
caryclark55888e42016-07-18 10:01:36 -0700151var SPAN_C_OTHER = SPAN_C_TY + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700152var SPAN_C_OTHERT = SPAN_C_OTHER + 1;
153var SPAN_C_OTHERI = SPAN_C_OTHERT + 1;
154var SPAN_C_SUM = SPAN_C_OTHERI + 1;
155var SPAN_C_VAL = SPAN_C_SUM + 1;
156var SPAN_C_OPP = SPAN_C_VAL + 1;
157
158var ACTIVE_LINE_SPAN = 1;
159var ACTIVE_QUAD_SPAN = ACTIVE_LINE_SPAN + 1;
caryclark1049f122015-04-20 08:31:59 -0700160var ACTIVE_CONIC_SPAN = ACTIVE_QUAD_SPAN + 1;
161var ACTIVE_CUBIC_SPAN = ACTIVE_CONIC_SPAN + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700162
163var ADD_MOVETO = ACTIVE_CUBIC_SPAN + 1;
164var ADD_LINETO = ADD_MOVETO + 1;
165var ADD_QUADTO = ADD_LINETO + 1;
caryclark1049f122015-04-20 08:31:59 -0700166var ADD_CONICTO = ADD_QUADTO + 1;
167var ADD_CUBICTO = ADD_CONICTO + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700168var ADD_CLOSE = ADD_CUBICTO + 1;
169var ADD_FILL = ADD_CLOSE + 1;
170
171var PATH_LINE = ADD_FILL + 1;
172var PATH_QUAD = PATH_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700173var PATH_CONIC = PATH_QUAD + 1;
174var PATH_CUBIC = PATH_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700175
176var INTERSECT_LINE = PATH_CUBIC + 1;
177var INTERSECT_LINE_2 = INTERSECT_LINE + 1;
178var INTERSECT_LINE_NO = INTERSECT_LINE_2 + 1;
179var INTERSECT_QUAD_LINE = INTERSECT_LINE_NO + 1;
180var INTERSECT_QUAD_LINE_2 = INTERSECT_QUAD_LINE + 1;
181var INTERSECT_QUAD_LINE_NO = INTERSECT_QUAD_LINE_2 + 1;
182var INTERSECT_QUAD = INTERSECT_QUAD_LINE_NO + 1;
183var INTERSECT_QUAD_2 = INTERSECT_QUAD + 1;
184var INTERSECT_QUAD_NO = INTERSECT_QUAD_2 + 1;
caryclark1049f122015-04-20 08:31:59 -0700185var INTERSECT_CONIC_LINE = INTERSECT_QUAD_NO + 1;
186var INTERSECT_CONIC_LINE_2 = INTERSECT_CONIC_LINE + 1;
187var INTERSECT_CONIC_LINE_NO = INTERSECT_CONIC_LINE_2 + 1;
caryclark55888e42016-07-18 10:01:36 -0700188var INTERSECT_CONIC_QUAD = INTERSECT_CONIC_LINE_NO + 1;
189var INTERSECT_CONIC_QUAD_2 = INTERSECT_CONIC_QUAD + 1;
190var INTERSECT_CONIC_QUAD_NO = INTERSECT_CONIC_QUAD_2 + 1;
191var INTERSECT_CONIC = INTERSECT_CONIC_QUAD_NO + 1;
caryclark1049f122015-04-20 08:31:59 -0700192var INTERSECT_CONIC_2 = INTERSECT_CONIC + 1;
193var INTERSECT_CONIC_NO = INTERSECT_CONIC_2 + 1;
194var INTERSECT_SELF_CUBIC = INTERSECT_CONIC_NO + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700195var INTERSECT_SELF_CUBIC_NO = INTERSECT_SELF_CUBIC + 1;
196var INTERSECT_CUBIC_LINE = INTERSECT_SELF_CUBIC_NO + 1;
197var INTERSECT_CUBIC_LINE_2 = INTERSECT_CUBIC_LINE + 1;
198var INTERSECT_CUBIC_LINE_3 = INTERSECT_CUBIC_LINE_2 + 1;
199var INTERSECT_CUBIC_LINE_NO = INTERSECT_CUBIC_LINE_3 + 1;
200var INTERSECT_CUBIC_QUAD = INTERSECT_CUBIC_LINE_NO + 1;
201var INTERSECT_CUBIC_QUAD_2 = INTERSECT_CUBIC_QUAD + 1;
202var INTERSECT_CUBIC_QUAD_3 = INTERSECT_CUBIC_QUAD_2 + 1;
203var INTERSECT_CUBIC_QUAD_4 = INTERSECT_CUBIC_QUAD_3 + 1;
204var INTERSECT_CUBIC_QUAD_NO = INTERSECT_CUBIC_QUAD_4 + 1;
205var INTERSECT_CUBIC = INTERSECT_CUBIC_QUAD_NO + 1;
206var INTERSECT_CUBIC_2 = INTERSECT_CUBIC + 1;
207var INTERSECT_CUBIC_3 = INTERSECT_CUBIC_2 + 1;
208var INTERSECT_CUBIC_4 = INTERSECT_CUBIC_3 + 1;
209// FIXME: add cubic 5- 9
210var INTERSECT_CUBIC_NO = INTERSECT_CUBIC_4 + 1;
211
212var SORT_UNARY = INTERSECT_CUBIC_NO + 1;
213var SORT_BINARY = SORT_UNARY + 1;
214
215var OP_DIFFERENCE = SORT_BINARY + 1;
216var OP_INTERSECT = OP_DIFFERENCE + 1;
217var OP_UNION = OP_INTERSECT + 1;
218var OP_XOR = OP_UNION + 1;
219
220var MARK_LINE = OP_XOR + 1;
221var MARK_QUAD = MARK_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700222var MARK_CONIC = MARK_QUAD + 1;
223var MARK_CUBIC = MARK_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700224var MARK_DONE_LINE = MARK_CUBIC + 1;
225var MARK_DONE_QUAD = MARK_DONE_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700226var MARK_DONE_CONIC = MARK_DONE_QUAD + 1;
227var MARK_DONE_CUBIC = MARK_DONE_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700228var MARK_UNSORTABLE_LINE = MARK_DONE_CUBIC + 1;
229var MARK_UNSORTABLE_QUAD = MARK_UNSORTABLE_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700230var MARK_UNSORTABLE_CONIC = MARK_UNSORTABLE_QUAD + 1;
231var MARK_UNSORTABLE_CUBIC = MARK_UNSORTABLE_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700232var MARK_SIMPLE_LINE = MARK_UNSORTABLE_CUBIC + 1;
233var MARK_SIMPLE_QUAD = MARK_SIMPLE_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700234var MARK_SIMPLE_CONIC = MARK_SIMPLE_QUAD + 1;
235var MARK_SIMPLE_CUBIC = MARK_SIMPLE_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700236var MARK_SIMPLE_DONE_LINE = MARK_SIMPLE_CUBIC + 1;
237var MARK_SIMPLE_DONE_QUAD = MARK_SIMPLE_DONE_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700238var MARK_SIMPLE_DONE_CONIC = MARK_SIMPLE_DONE_QUAD + 1;
239var MARK_SIMPLE_DONE_CUBIC = MARK_SIMPLE_DONE_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700240var MARK_DONE_UNARY_LINE = MARK_SIMPLE_DONE_CUBIC + 1;
241var MARK_DONE_UNARY_QUAD = MARK_DONE_UNARY_LINE + 1;
caryclark1049f122015-04-20 08:31:59 -0700242var MARK_DONE_UNARY_CONIC = MARK_DONE_UNARY_QUAD + 1;
243var MARK_DONE_UNARY_CUBIC = MARK_DONE_UNARY_CONIC + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700244var MARK_ANGLE_LAST = MARK_DONE_UNARY_CUBIC + 1;
245
246var COMPUTED_SET_1 = MARK_ANGLE_LAST + 1;
247var COMPUTED_SET_2 = COMPUTED_SET_1 + 1;
248
caryclark624637c2015-05-11 07:21:27 -0700249var ANGLE_AFTER = COMPUTED_SET_2 + 1;
caryclark54359292015-03-26 07:52:43 -0700250var ANGLE_AFTERPART = ANGLE_AFTER + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700251
caryclark54359292015-03-26 07:52:43 -0700252var ACTIVE_OP = ANGLE_AFTERPART + 1;
caryclarkdac1d172014-06-17 05:15:38 -0700253
caryclark624637c2015-05-11 07:21:27 -0700254var COIN_MAIN_SPAN = ACTIVE_OP + 1;
255var COIN_OPP_SPAN = COIN_MAIN_SPAN + 1;
256
257var FRAG_TYPE_LAST = COIN_OPP_SPAN;
caryclarkdac1d172014-06-17 05:15:38 -0700258
259var REC_TYPE_UNKNOWN = -1;
260var REC_TYPE_PATH = 0;
caryclark54359292015-03-26 07:52:43 -0700261var REC_TYPE_PATH2 = 1;
262var REC_TYPE_SECT = 2;
263var REC_TYPE_ACTIVE = 3;
264var REC_TYPE_ADD = 4;
265var REC_TYPE_SORT = 5;
266var REC_TYPE_OP = 6;
267var REC_TYPE_MARK = 7;
268var REC_TYPE_COMPUTED = 8;
269var REC_TYPE_COIN = 9;
270var REC_TYPE_ANGLE = 10;
271var REC_TYPE_ACTIVE_OP = 11;
272var REC_TYPE_AFTERPART = 12;
caryclark03b03ca2015-04-23 09:13:37 -0700273var REC_TYPE_TOP = 13;
caryclark624637c2015-05-11 07:21:27 -0700274var REC_TYPE_COINCIDENCE = 14;
caryclark26ad22a2015-10-16 09:03:38 -0700275var REC_TYPE_ALIGNED = 15;
276var REC_TYPE_LAST = REC_TYPE_ALIGNED;
caryclarkdac1d172014-06-17 05:15:38 -0700277
278function strs_to_nums(strs) {
279 var result = [];
280 for (var idx = 1; idx < strs.length; ++idx) {
281 var str = strs[idx];
282 var num = parseFloat(str);
283 if (isNaN(num)) {
284 result.push(str);
285 } else {
286 result.push(num);
287 }
288 }
289 return result;
290}
291
292function filter_str_by(id, str, regex, array) {
293 if (regex.test(str)) {
294 var strs = regex.exec(str);
295 var result = strs_to_nums(strs);
296 array.push(id);
297 array.push(result);
298 return true;
299 }
300 return false;
301}
302
303function construct_regexp2(pattern) {
304 var escape = pattern.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
305 escape = escape.replace(/UNSORTABLE/g, "\\*\\*\\* UNSORTABLE \\*\\*\\*");
306 escape = escape.replace(/CUBIC_VAL/g, "\\(P_VAL P_VAL P_VAL P_VAL\\)");
caryclark1049f122015-04-20 08:31:59 -0700307 escape = escape.replace(/CONIC_VAL/g, "\\(P_VAL P_VAL P_VAL W_VAL\\)");
caryclarkdac1d172014-06-17 05:15:38 -0700308 escape = escape.replace(/QUAD_VAL/g, "\\(P_VAL P_VAL P_VAL\\)");
309 escape = escape.replace(/LINE_VAL/g, "\\(P_VAL P_VAL\\)");
310 escape = escape.replace(/FILL_TYPE/g, "SkPath::k[a-zA-Z]+_FillType");
caryclark54359292015-03-26 07:52:43 -0700311 escape = escape.replace(/PTR_VAL/g, "0x[0-9A-F]+");
caryclarkdac1d172014-06-17 05:15:38 -0700312 escape = escape.replace(/PT_VAL/g, "\\(P_VAL\\)");
313 escape = escape.replace(/P_VAL/g, "(-?\\d+\\.?\\d*(?:e-?\\d+)?)[Ff]?, ?(-?\\d+\\.?\\d*(?:e-?\\d+)?)[Ff]?");
314 escape = escape.replace(/T_VAL/g, "(-?\\d+\\.?\\d*(?:e-?\\d+)?)");
caryclark1049f122015-04-20 08:31:59 -0700315 escape = escape.replace(/W_VAL/g, "(-?\\d+\\.?\\d*(?:e-?\\d+)?)[Ff]?");
caryclarkdac1d172014-06-17 05:15:38 -0700316 escape = escape.replace(/PATH/g, "pathB?");
caryclark1049f122015-04-20 08:31:59 -0700317 escape = escape.replace(/IDX/g, "(-?\\d+)");
caryclarkdac1d172014-06-17 05:15:38 -0700318 escape = escape.replace(/NUM/g, "(-?\\d+)");
319 escape = escape.replace(/OPT/g, "(\\?|-?\\d+)");
320 return new RegExp(escape, 'i');
321}
322
323function construct_regexp2c(pattern) {
324 var escape = pattern.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
325 escape = escape.replace(/UNSORTABLE/g, "\\*\\*\\* UNSORTABLE \\*\\*\\*");
caryclark54359292015-03-26 07:52:43 -0700326 escape = escape.replace(/CUBIC_VAL/g, "(?:\\$\\d = )?\\{\\{\\{P_VAL\\}, \\{P_VAL\\}, \\{P_VAL\\}, \\{P_VAL\\}\\}\\}");
caryclark1049f122015-04-20 08:31:59 -0700327 escape = escape.replace(/CONIC_VAL/g, "(?:\\$\\d = )?\\{\\{\\{\\{P_VAL\\}, \\{P_VAL\\}, \\{P_VAL\\}\\}\\}, W_VAL\\}");
caryclark54359292015-03-26 07:52:43 -0700328 escape = escape.replace(/QUAD_VAL/g, "(?:\\$\\d = )?\\{\\{\\{P_VAL\\}, \\{P_VAL\\}, \\{P_VAL\\}\\}\\}");
329 escape = escape.replace(/LINE_VAL/g, "(?:\\$\\d = )?\\{\\{\\{P_VAL\\}, \\{P_VAL\\}\\}\\}");
caryclarkdac1d172014-06-17 05:15:38 -0700330 escape = escape.replace(/FILL_TYPE/g, "SkPath::k[a-zA-Z]+_FillType");
caryclark54359292015-03-26 07:52:43 -0700331 escape = escape.replace(/PTR_VAL/g, "0x[0-9A-F]+");
caryclarkdac1d172014-06-17 05:15:38 -0700332 escape = escape.replace(/PT_VAL/g, "\\{\\{P_VAL\\}\\}");
caryclark54359292015-03-26 07:52:43 -0700333 escape = escape.replace(/P_VAL/g, "(?:f?[xX] = )?(-?\\d+\\.?\\d*(?:e-?\\d+)?)[Ff]?, *(?: f?[yY] = )?(-?\\d+\\.?\\d*(?:e-?\\d+)?)[Ff]?");
caryclarkdac1d172014-06-17 05:15:38 -0700334 escape = escape.replace(/T_VAL/g, "(-?\\d+\\.?\\d*(?:e-?\\d+)?)");
caryclark1049f122015-04-20 08:31:59 -0700335 escape = escape.replace(/W_VAL/g, "(-?\\d+\\.?\\d*(?:e-?\\d+)?)[Ff]?");
caryclarkdac1d172014-06-17 05:15:38 -0700336 escape = escape.replace(/OPER/g, "[a-z]+");
337 escape = escape.replace(/PATH/g, "pathB?");
338 escape = escape.replace(/T_F/g, "([TF])");
caryclark1049f122015-04-20 08:31:59 -0700339 escape = escape.replace(/IDX/g, "(-?\\d+)");
caryclarkdac1d172014-06-17 05:15:38 -0700340 escape = escape.replace(/NUM/g, "(-?\\d+)");
341 escape = escape.replace(/OPT/g, "(\\?|-?\\d+)");
342 return new RegExp(escape, 'i');
343}
344
345function match_regexp(str, lineNo, array, id, pattern) {
346 var regex = construct_regexp2(pattern);
347 if (filter_str_by(id, str, regex, array)) {
348 return true;
349 }
350 regex = construct_regexp2c(pattern);
351 return filter_str_by(id, str, regex, array);
352}
353
354function endsWith(str, suffix) {
355 return str.indexOf(suffix, str.length - suffix.length) !== -1;
356}
357
358function parse_all(test) {
359 var lines = test.match(/[^\r\n]+/g);
360 var records = []; // a rec can be the original paths, a set of intersections, a set of active spans, a sort, or a path add
361 var record = [];
362 var recType = REC_TYPE_UNKNOWN;
363 var lastLineNo;
364 var moveX, moveY;
365 for (var lineNo = 0; lineNo < lines.length; ++lineNo) {
366 var line = lines[lineNo];
367 if (line.length == 0) {
368 continue;
369 }
370 var opStart = "SkOpSegment::";
371 if (line.lastIndexOf(opStart, 0) === 0) {
372 line = line.substr(opStart.length);
373 }
374 var angleStart = "SkOpAngle::";
375 if (line.lastIndexOf(angleStart, 0) === 0) {
376 line = line.substr(angleStart.length);
377 }
caryclark624637c2015-05-11 07:21:27 -0700378 var coinStart = "SkOpCoincidence::";
379 if (line.lastIndexOf(coinStart, 0) === 0) {
380 line = line.substr(coinStart.length);
381 }
caryclark54359292015-03-26 07:52:43 -0700382 var type = line.lastIndexOf("debugShowActiveSpans", 0) === 0 ? REC_TYPE_ACTIVE
caryclark624637c2015-05-11 07:21:27 -0700383 : line.lastIndexOf("debugShowCoincidence", 0) === 0 ? REC_TYPE_COINCIDENCE
caryclark54359292015-03-26 07:52:43 -0700384 : line.lastIndexOf("((SkOpSegment*)", 0) === 0 ? REC_TYPE_PATH2
caryclark55888e42016-07-18 10:01:36 -0700385 : line.lastIndexOf("debugShowTs", 0) === 0 ? REC_TYPE_COIN
caryclark54359292015-03-26 07:52:43 -0700386 : line.lastIndexOf("afterPart", 0) === 0 ? REC_TYPE_AFTERPART
caryclarkdac1d172014-06-17 05:15:38 -0700387 : line.lastIndexOf("debugShow", 0) === 0 ? REC_TYPE_SECT
388 : line.lastIndexOf("activeOp", 0) === 0 ? REC_TYPE_ACTIVE_OP
389 : line.lastIndexOf("computed", 0) === 0 ? REC_TYPE_COMPUTED
390 : line.lastIndexOf("debugOne", 0) === 0 ? REC_TYPE_SORT
caryclark26ad22a2015-10-16 09:03:38 -0700391 : line.lastIndexOf("aligned=", 0) === 0 ? REC_TYPE_ALIGNED
caryclarkdac1d172014-06-17 05:15:38 -0700392 : line.lastIndexOf("dumpOne", 0) === 0 ? REC_TYPE_SORT
caryclark03b03ca2015-04-23 09:13:37 -0700393 : line.lastIndexOf("findTop", 0) === 0 ? REC_TYPE_TOP
caryclarkdac1d172014-06-17 05:15:38 -0700394 : line.lastIndexOf("pathB.", 0) === 0 ? REC_TYPE_ADD
395 : line.lastIndexOf("path.", 0) === 0 ? REC_TYPE_ADD
396 : line.lastIndexOf("after", 0) === 0 ? REC_TYPE_ANGLE
397 : line.lastIndexOf("mark", 0) === 0 ? REC_TYPE_MARK
398 : line.lastIndexOf(" {{", 0) === 0 ? REC_TYPE_COMPUTED
caryclark54359292015-03-26 07:52:43 -0700399 : line.lastIndexOf("seg=", 0) === 0 ? REC_TYPE_PATH
caryclarkdac1d172014-06-17 05:15:38 -0700400 : line.lastIndexOf("op", 0) === 0 ? REC_TYPE_OP
401 : line.lastIndexOf("$", 0) === 0 ? REC_TYPE_PATH
402 : REC_TYPE_UNKNOWN;
403 if (recType != type || recType == REC_TYPE_ADD || recType == REC_TYPE_SECT
404 || recType == REC_TYPE_ACTIVE_OP || recType == REC_TYPE_ANGLE) {
405 if (recType != REC_TYPE_UNKNOWN) {
406 records.push(recType);
407 records.push(lastLineNo);
408 records.push(record);
409 }
410 record = [];
411 recType = type;
412 lastLineNo = lineNo;
413 }
414 var found = false;
415 switch (recType) {
416 case REC_TYPE_ACTIVE:
417 found = match_regexp(line, lineNo, record, ACTIVE_LINE_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700418" id=IDX LINE_VAL t=T_VAL tEnd=T_VAL windSum=OPT windValue=IDX"
caryclarkdac1d172014-06-17 05:15:38 -0700419 ) || match_regexp(line, lineNo, record, ACTIVE_QUAD_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700420" id=IDX QUAD_VAL t=T_VAL tEnd=T_VAL windSum=OPT windValue=IDX"
caryclark1049f122015-04-20 08:31:59 -0700421 ) || match_regexp(line, lineNo, record, ACTIVE_CONIC_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700422" id=IDX CONIC_VAL t=T_VAL tEnd=T_VAL windSum=OPT windValue=IDX"
caryclarkdac1d172014-06-17 05:15:38 -0700423 ) || match_regexp(line, lineNo, record, ACTIVE_CUBIC_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700424" id=IDX CUBIC_VAL t=T_VAL tEnd=T_VAL windSum=OPT windValue=IDX"
caryclark624637c2015-05-11 07:21:27 -0700425 ) || match_regexp(line, lineNo, record, ACTIVE_LINE_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700426" id=IDX LINE_VAL t=T_VAL tEnd=T_VAL windSum=OPT oppSum=OPT windValue=IDX oppValue=NUM"
caryclark624637c2015-05-11 07:21:27 -0700427 ) || match_regexp(line, lineNo, record, ACTIVE_QUAD_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700428" id=IDX QUAD_VAL t=T_VAL tEnd=T_VAL windSum=OPT oppSum=OPT windValue=IDX oppValue=NUM"
caryclark624637c2015-05-11 07:21:27 -0700429 ) || match_regexp(line, lineNo, record, ACTIVE_CONIC_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700430" id=IDX CONIC_VAL t=T_VAL tEnd=T_VAL windSum=OPT oppSum=OPT windValue=IDX oppValue=NUM"
caryclark624637c2015-05-11 07:21:27 -0700431 ) || match_regexp(line, lineNo, record, ACTIVE_CUBIC_SPAN, "debugShowActiveSpans" +
caryclark55888e42016-07-18 10:01:36 -0700432" id=IDX CUBIC_VAL t=T_VAL tEnd=T_VAL windSum=OPT oppSum=OPT windValue=IDX oppValue=NUM"
caryclarkdac1d172014-06-17 05:15:38 -0700433 );
434 break;
435 case REC_TYPE_ACTIVE_OP:
436 found = match_regexp(line, lineNo, record, ACTIVE_OP, "activeOp" +
437" id=IDX t=T_VAL tEnd=T_VAL op=OPER miFrom=NUM miTo=NUM suFrom=NUM suTo=NUM result=IDX"
438 );
439 break;
440 case REC_TYPE_ADD:
441 if (match_regexp(line, lineNo, record, ADD_MOVETO, "PATH.moveTo(P_VAL);")) {
442 moveX = record[1][0];
443 moveY = record[1][1];
444 found = true;
445 } else if (match_regexp(line, lineNo, record, ADD_LINETO, "PATH.lineTo(P_VAL);")) {
446 record[1].unshift(moveY);
447 record[1].unshift(moveX);
448 moveX = record[1][2];
449 moveY = record[1][3];
450 found = true;
451 } else if (match_regexp(line, lineNo, record, ADD_QUADTO, "PATH.quadTo(P_VAL, P_VAL);")) {
452 record[1].unshift(moveY);
453 record[1].unshift(moveX);
454 moveX = record[1][4];
455 moveY = record[1][5];
456 found = true;
caryclark1049f122015-04-20 08:31:59 -0700457 } else if (match_regexp(line, lineNo, record, ADD_CONICTO, "PATH.conicTo(P_VAL, P_VAL, T_VAL);")) {
458 record[1].unshift(moveY);
459 record[1].unshift(moveX);
460 moveX = record[1][4];
461 moveY = record[1][5];
462 found = true;
caryclarkdac1d172014-06-17 05:15:38 -0700463 } else if (match_regexp(line, lineNo, record, ADD_CUBICTO, "PATH.cubicTo(P_VAL, P_VAL, P_VAL);")) {
464 record[1].unshift(moveY);
465 record[1].unshift(moveX);
466 moveX = record[1][6];
467 moveY = record[1][7];
468 found = true;
469 } else if (match_regexp(line, lineNo, record, ADD_FILL, "PATH.setFillType(FILL_TYPE);")) {
470 found = true;
471 } else {
472 found = match_regexp(line, lineNo, record, ADD_CLOSE, "PATH.close();");
473 }
474 break;
caryclark54359292015-03-26 07:52:43 -0700475 case REC_TYPE_AFTERPART:
476 found = match_regexp(line, lineNo, record, PATH_LINE, "afterPart LINE_VAL")
477 || match_regexp(line, lineNo, record, PATH_QUAD, "afterPart QUAD_VAL")
caryclark1049f122015-04-20 08:31:59 -0700478 || match_regexp(line, lineNo, record, PATH_CONIC, "afterPart CONIC_VAL")
caryclark54359292015-03-26 07:52:43 -0700479 || match_regexp(line, lineNo, record, PATH_CUBIC, "afterPart CUBIC_VAL")
480 break;
caryclark26ad22a2015-10-16 09:03:38 -0700481 case REC_TYPE_ALIGNED:
482 found = match_regexp(line, lineNo, record, PATH_LINE, "aligned=IDX LINE_VAL"
483 ) || match_regexp(line, lineNo, record, PATH_QUAD, "aligned=IDX QUAD_VAL"
484 ) || match_regexp(line, lineNo, record, PATH_CONIC, "aligned=IDX CONIC_VAL"
485 ) || match_regexp(line, lineNo, record, PATH_CUBIC, "aligned=IDX CUBIC_VAL"
486 );
487 break;
caryclarkdac1d172014-06-17 05:15:38 -0700488 case REC_TYPE_ANGLE:
489 found = match_regexp(line, lineNo, record, ANGLE_AFTER, "after " +
caryclarkdac1d172014-06-17 05:15:38 -0700490"[IDX/IDX] NUM/NUM tStart=T_VAL tEnd=T_VAL < [IDX/IDX] NUM/NUM tStart=T_VAL tEnd=T_VAL < [IDX/IDX] NUM/NUM tStart=T_VAL tEnd=T_VAL T_F IDX");
491 break;
492 case REC_TYPE_COIN:
493 found = true;
494 break;
caryclark624637c2015-05-11 07:21:27 -0700495 case REC_TYPE_COINCIDENCE:
496 found = match_regexp(line, lineNo, record, COIN_MAIN_SPAN, "debugShowCoincidence" +
497" + id=IDX t=T_VAL tEnd=T_VAL"
498 ) || match_regexp(line, lineNo, record, COIN_OPP_SPAN, "debugShowCoincidence" +
499" - id=IDX t=T_VAL tEnd=T_VAL"
500 );
501 break;
caryclarkdac1d172014-06-17 05:15:38 -0700502 case REC_TYPE_COMPUTED:
503 found = line == "computed quadratics given"
504 || match_regexp(line, lineNo, record, COMPUTED_SET_1, "computed quadratics set 1"
505 ) || match_regexp(line, lineNo, record, COMPUTED_SET_2, "computed quadratics set 2"
506 ) || match_regexp(line, lineNo, record, PATH_QUAD, " QUAD_VAL,"
caryclark1049f122015-04-20 08:31:59 -0700507 ) || match_regexp(line, lineNo, record, PATH_CONIC, " CONIC_VAL,"
caryclarkdac1d172014-06-17 05:15:38 -0700508 ) || match_regexp(line, lineNo, record, PATH_CUBIC, " CUBIC_VAL,"
509 );
510 break;
511 case REC_TYPE_PATH:
caryclark54359292015-03-26 07:52:43 -0700512 found = match_regexp(line, lineNo, record, PATH_LINE, "seg=IDX LINE_VAL"
513 ) || match_regexp(line, lineNo, record, PATH_QUAD, "seg=IDX QUAD_VAL"
caryclark1049f122015-04-20 08:31:59 -0700514 ) || match_regexp(line, lineNo, record, PATH_CONIC, "seg=IDX CONIC_VAL"
caryclark54359292015-03-26 07:52:43 -0700515 ) || match_regexp(line, lineNo, record, PATH_CUBIC, "seg=IDX CUBIC_VAL"
516 );
517 break;
518 case REC_TYPE_PATH2:
519 found = match_regexp(line, lineNo, record, PATH_LINE, "((SkOpSegment*) PTR_VAL) [IDX] {LINE_VAL}"
520 ) || match_regexp(line, lineNo, record, PATH_QUAD, "((SkOpSegment*) PTR_VAL) [IDX] {QUAD_VAL}"
caryclark1049f122015-04-20 08:31:59 -0700521 ) || match_regexp(line, lineNo, record, PATH_CONIC, "((SkOpSegment*) PTR_VAL) [IDX] {CONIC_VAL}"
caryclark54359292015-03-26 07:52:43 -0700522 ) || match_regexp(line, lineNo, record, PATH_CUBIC, "((SkOpSegment*) PTR_VAL) [IDX] {CUBIC_VAL}"
caryclarkdac1d172014-06-17 05:15:38 -0700523 );
524 break;
525 case REC_TYPE_SECT:
526 found = match_regexp(line, lineNo, record, INTERSECT_LINE, "debugShowLineIntersection" +
527" wtTs[0]=T_VAL LINE_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL"
528 ) || match_regexp(line, lineNo, record, INTERSECT_LINE_2, "debugShowLineIntersection" +
529" wtTs[0]=T_VAL LINE_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL wnTs[1]=T_VAL"
530 ) || match_regexp(line, lineNo, record, INTERSECT_LINE_NO, "debugShowLineIntersection" +
531" no intersect LINE_VAL LINE_VAL"
532 ) || match_regexp(line, lineNo, record, INTERSECT_QUAD_LINE, "debugShowQuadLineIntersection" +
533" wtTs[0]=T_VAL QUAD_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL"
534 ) || match_regexp(line, lineNo, record, INTERSECT_QUAD_LINE_2, "debugShowQuadLineIntersection" +
535" wtTs[0]=T_VAL QUAD_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL wnTs[1]=T_VAL"
536 ) || match_regexp(line, lineNo, record, INTERSECT_QUAD_LINE_NO, "debugShowQuadLineIntersection" +
537" no intersect QUAD_VAL LINE_VAL"
538 ) || match_regexp(line, lineNo, record, INTERSECT_QUAD, "debugShowQuadIntersection" +
539" wtTs[0]=T_VAL QUAD_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL"
540 ) || match_regexp(line, lineNo, record, INTERSECT_QUAD_2, "debugShowQuadIntersection" +
541" wtTs[0]=T_VAL QUAD_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL wnTs[1]=T_VAL"
542 ) || match_regexp(line, lineNo, record, INTERSECT_QUAD_NO, "debugShowQuadIntersection" +
543" no intersect QUAD_VAL QUAD_VAL"
caryclark55888e42016-07-18 10:01:36 -0700544
caryclark1049f122015-04-20 08:31:59 -0700545 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_LINE, "debugShowConicLineIntersection" +
546" wtTs[0]=T_VAL CONIC_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL"
547 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_LINE_2, "debugShowConicLineIntersection" +
548" wtTs[0]=T_VAL CONIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL wnTs[1]=T_VAL"
549 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_LINE_NO, "debugShowConicLineIntersection" +
550" no intersect CONIC_VAL LINE_VAL"
caryclark55888e42016-07-18 10:01:36 -0700551
552 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_QUAD, "debugShowConicQuadIntersection" +
553" wtTs[0]=T_VAL CONIC_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL"
554 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_QUAD_2, "debugShowConicQuadIntersection" +
555" wtTs[0]=T_VAL CONIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL wnTs[1]=T_VAL"
556 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_QUAD_NO, "debugShowConicQuadIntersection" +
557" no intersect CONIC_VAL QUAD_VAL"
558
caryclark1049f122015-04-20 08:31:59 -0700559 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC, "debugShowConicIntersection" +
560" wtTs[0]=T_VAL CONIC_VAL PT_VAL wnTs[0]=T_VAL CONIC_VAL"
561 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_2, "debugShowConicIntersection" +
562" wtTs[0]=T_VAL CONIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL CONIC_VAL wnTs[1]=T_VAL"
563 ) || match_regexp(line, lineNo, record, INTERSECT_CONIC_NO, "debugShowConicIntersection" +
564" no intersect CONIC_VAL CONIC_VAL"
caryclarkdac1d172014-06-17 05:15:38 -0700565 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_LINE, "debugShowCubicLineIntersection" +
566" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL"
567 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_LINE_2, "debugShowCubicLineIntersection" +
568" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL wnTs[1]=T_VAL"
569 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_LINE_3, "debugShowCubicLineIntersection" +
570" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wtTs[2]=T_VAL PT_VAL wnTs[0]=T_VAL LINE_VAL wnTs[1]=T_VAL wnTs[2]=T_VAL"
571 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_LINE_NO, "debugShowCubicLineIntersection" +
572" no intersect CUBIC_VAL LINE_VAL"
573 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_QUAD, "debugShowCubicQuadIntersection" +
574" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL"
575 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_QUAD_2, "debugShowCubicQuadIntersection" +
576" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL wnTs[1]=T_VAL"
577 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_QUAD_3, "debugShowCubicQuadIntersection" +
578" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wtTs[2]=T_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL wnTs[1]=T_VAL wnTs[2]=T_VAL"
579 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_QUAD_4, "debugShowCubicQuadIntersection" +
580" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wtTs[2]=T_VAL wtTs[3]=T_VAL PT_VAL wnTs[0]=T_VAL QUAD_VAL wnTs[1]=T_VAL wnTs[2]=T_VAL wnTs[3]=T_VAL"
581 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_QUAD_NO, "debugShowCubicQuadIntersection" +
582" no intersect CUBIC_VAL QUAD_VAL"
583 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC, "debugShowCubicIntersection" +
584" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wnTs[0]=T_VAL CUBIC_VAL"
585 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_2, "debugShowCubicIntersection" +
586" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wnTs[0]=T_VAL CUBIC_VAL wnTs[1]=T_VAL"
587 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_3, "debugShowCubicIntersection" +
588" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wtTs[2]=T_VAL PT_VAL wnTs[0]=T_VAL CUBIC_VAL wnTs[1]=T_VAL wnTs[2]=T_VAL"
589 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_4, "debugShowCubicIntersection" +
590" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL PT_VAL wtTs[2]=T_VAL PT_VAL wtTs[3]=T_VAL PT_VAL wnTs[0]=T_VAL CUBIC_VAL wnTs[1]=T_VAL wnTs[2]=T_VAL wnTs[3]=T_VAL"
591 ) || match_regexp(line, lineNo, record, INTERSECT_CUBIC_NO, "debugShowCubicIntersection" +
592" no intersect CUBIC_VAL CUBIC_VAL"
593 ) || match_regexp(line, lineNo, record, INTERSECT_SELF_CUBIC, "debugShowCubicIntersection" +
594" wtTs[0]=T_VAL CUBIC_VAL PT_VAL wtTs[1]=T_VAL"
595 ) || match_regexp(line, lineNo, record, INTERSECT_SELF_CUBIC_NO, "debugShowCubicIntersection" +
596" no self intersect CUBIC_VAL"
597 );
598 break;
599 case REC_TYPE_SORT:
600 var hasDone = / done/.test(line);
601 var hasUnorderable = / unorderable/.test(line);
602 var hasSmall = / small/.test(line);
603 var hasTiny = / tiny/.test(line);
604 var hasOperand = / operand/.test(line);
605 var hasStop = / stop/.test(line);
606 line.replace(/[ a-z]+$/, "");
607 found = match_regexp(line, lineNo, record, SORT_UNARY, "debugOne" +
608" [IDX/IDX] next=IDX/IDX sect=IDX/IDX s=T_VAL [IDX] e=T_VAL [IDX] sgn=NUM windVal=IDX windSum=OPT"
609 ) || match_regexp(line, lineNo, record, SORT_BINARY, "debugOne" +
610" [IDX/IDX] next=IDX/IDX sect=IDX/IDX s=T_VAL [IDX] e=T_VAL [IDX] sgn=NUM windVal=IDX windSum=OPT oppVal=IDX oppSum=OPT"
611 ) || match_regexp(line, lineNo, record, SORT_UNARY, "dumpOne" +
612" [IDX/IDX] next=IDX/IDX sect=NUM/NUM s=T_VAL [IDX] e=T_VAL [IDX] sgn=NUM windVal=IDX windSum=OPT"
613 ) || match_regexp(line, lineNo, record, SORT_BINARY, "dumpOne" +
614" [IDX/IDX] next=IDX/IDX sect=NUM/NUM s=T_VAL [IDX] e=T_VAL [IDX] sgn=NUM windVal=IDX windSum=OPT oppVal=IDX oppSum=OPT"
615 );
616 if (found) {
617 record[1].push(hasDone);
618 record[1].push(hasUnorderable);
619 record[1].push(hasSmall);
620 record[1].push(hasTiny);
621 record[1].push(hasOperand);
622 record[1].push(hasStop);
623 }
624 break;
caryclark03b03ca2015-04-23 09:13:37 -0700625 case REC_TYPE_TOP:
626 found = match_regexp(line, lineNo, record, ACTIVE_OP, "findTop" +
627" id=IDX s=T_VAL e=T_VAL cw=NUM swap=NUM inflections=NUM monotonic=NUM"
628 ) || match_regexp(line, lineNo, record, ACTIVE_OP, "findTop" +
629" id=IDX s=T_VAL e=T_VAL (-) cw=NUM swap=NUM inflections=NUM monotonic=NUM"
630 ) || match_regexp(line, lineNo, record, ACTIVE_OP, "findTop" +
631" id=IDX s=T_VAL e=T_VAL (+) cw=NUM swap=NUM inflections=NUM monotonic=NUM"
632 );
633 break;
caryclarkdac1d172014-06-17 05:15:38 -0700634 case REC_TYPE_MARK:
635 found = match_regexp(line, lineNo, record, MARK_LINE, "markWinding" +
caryclark54359292015-03-26 07:52:43 -0700636" id=IDX LINE_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX"
caryclarkdac1d172014-06-17 05:15:38 -0700637 ) || match_regexp(line, lineNo, record, MARK_QUAD, "markWinding" +
caryclark54359292015-03-26 07:52:43 -0700638" id=IDX QUAD_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX"
caryclark1049f122015-04-20 08:31:59 -0700639 ) || match_regexp(line, lineNo, record, MARK_CONIC, "markWinding" +
640" id=IDX CONIC_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX"
caryclarkdac1d172014-06-17 05:15:38 -0700641 ) || match_regexp(line, lineNo, record, MARK_CUBIC, "markWinding" +
caryclark54359292015-03-26 07:52:43 -0700642" id=IDX CUBIC_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX"
643 ) || match_regexp(line, lineNo, record, MARK_DONE_LINE, "markDone" +
644" id=IDX LINE_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=OPT newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX oppValue=OPT"
645 ) || match_regexp(line, lineNo, record, MARK_DONE_QUAD, "markDone" +
646" id=IDX QUAD_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=OPT newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX oppValue=OPT"
caryclark1049f122015-04-20 08:31:59 -0700647 ) || match_regexp(line, lineNo, record, MARK_DONE_CONIC, "markDone" +
648" id=IDX CONIC_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=OPT newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX oppValue=OPT"
caryclark54359292015-03-26 07:52:43 -0700649 ) || match_regexp(line, lineNo, record, MARK_DONE_CUBIC, "markDone" +
650" id=IDX CUBIC_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=OPT newOppSum=OPT oppSum=OPT windSum=OPT windValue=IDX oppValue=OPT"
caryclarkdac1d172014-06-17 05:15:38 -0700651 ) || match_regexp(line, lineNo, record, MARK_SIMPLE_LINE, "markWinding" +
652" id=IDX LINE_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM windSum=OPT windValue=IDX"
653 ) || match_regexp(line, lineNo, record, MARK_SIMPLE_QUAD, "markWinding" +
654" id=IDX QUAD_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM windSum=OPT windValue=IDX"
caryclark1049f122015-04-20 08:31:59 -0700655 ) || match_regexp(line, lineNo, record, MARK_SIMPLE_CONIC, "markWinding" +
656" id=IDX CONIC_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM windSum=OPT windValue=IDX"
caryclarkdac1d172014-06-17 05:15:38 -0700657 ) || match_regexp(line, lineNo, record, MARK_SIMPLE_CUBIC, "markWinding" +
658" id=IDX CUBIC_VAL t=T_VAL [IDX] PT_VAL tEnd=T_VAL newWindSum=NUM windSum=OPT windValue=IDX"
caryclarkdac1d172014-06-17 05:15:38 -0700659 ) || match_regexp(line, lineNo, record, MARK_ANGLE_LAST, "markAngle" +
caryclark1049f122015-04-20 08:31:59 -0700660" last segment=IDX span=IDX"
caryclark54359292015-03-26 07:52:43 -0700661 ) || match_regexp(line, lineNo, record, MARK_ANGLE_LAST, "markAngle" +
caryclark55888e42016-07-18 10:01:36 -0700662" last seg=IDX span=IDX"
663 ) || match_regexp(line, lineNo, record, MARK_ANGLE_LAST, "markAngle" +
664" last segment=IDX span=IDX windSum=OPT"
665 ) || match_regexp(line, lineNo, record, MARK_ANGLE_LAST, "markAngle" +
666" last seg=IDX span=IDX windSum=OPT"
667 );
caryclarkdac1d172014-06-17 05:15:38 -0700668 break;
669 case REC_TYPE_OP:
670 if (line.lastIndexOf("oppSign oppSign=", 0) === 0
671 || line.lastIndexOf("operator<", 0) === 0) {
672 found = true;
673 break;
674 }
caryclark54359292015-03-26 07:52:43 -0700675 found = match_regexp(line, lineNo, record, OP_DIFFERENCE, "op diff"
caryclarkdac1d172014-06-17 05:15:38 -0700676 ) || match_regexp(line, lineNo, record, OP_INTERSECT, "op intersect"
caryclark54359292015-03-26 07:52:43 -0700677 ) || match_regexp(line, lineNo, record, OP_INTERSECT, "op sect"
caryclarkdac1d172014-06-17 05:15:38 -0700678 ) || match_regexp(line, lineNo, record, OP_UNION, "op union"
679 ) || match_regexp(line, lineNo, record, OP_XOR, "op xor"
680 );
681 break;
682 case REC_TYPE_UNKNOWN:
683 found = true;
684 break;
685 }
686 if (!found) {
687 console.log(line + " [" + lineNo + "] of type " + type + " not found");
688 }
689 }
690 if (recType != REC_TYPE_UNKNOWN) {
691 records.push(recType);
692 records.push(lastLineNo);
693 records.push(record);
694 }
695 if (records.length >= 1) {
696 tests[testIndex] = records;
697 testLines[testIndex] = lines;
698 }
699}
700
701function init(test) {
702 var canvas = document.getElementById('canvas');
703 if (!canvas.getContext) return;
704 ctx = canvas.getContext('2d');
705 var resScale = retina_scale && window.devicePixelRatio ? window.devicePixelRatio : 1;
706 var unscaledWidth = window.innerWidth - 20;
707 var unscaledHeight = window.innerHeight - 20;
708 screenWidth = unscaledWidth;
709 screenHeight = unscaledHeight;
710 canvas.width = unscaledWidth * resScale;
711 canvas.height = unscaledHeight * resScale;
712 canvas.style.width = unscaledWidth + 'px';
713 canvas.style.height = unscaledHeight + 'px';
714 if (resScale != 1) {
715 ctx.scale(resScale, resScale);
716 }
717 xmin = Infinity;
718 xmax = -Infinity;
719 ymin = Infinity;
720 ymax = -Infinity;
caryclark26ad22a2015-10-16 09:03:38 -0700721 hasPath = hasAlignedPath = hasComputedPath = false;
caryclarkdac1d172014-06-17 05:15:38 -0700722 firstActiveSpan = -1;
723 for (var tIndex = 0; tIndex < test.length; tIndex += 3) {
724 var recType = test[tIndex];
725 if (!typeof recType == 'number' || recType < REC_TYPE_UNKNOWN || recType > REC_TYPE_LAST) {
726 console.log("unknown rec type: " + recType);
727 throw "stop execution";
728 }
729 var records = test[tIndex + 2];
730 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
731 var fragType = records[recordIndex];
732 if (!typeof fragType == 'number' || fragType < 1 || fragType > FRAG_TYPE_LAST) {
733 console.log("unknown in range frag type: " + fragType);
734 throw "stop execution";
735 }
736 var frags = records[recordIndex + 1];
737 var first = 0;
738 var last = -1;
739 var first2 = 0;
740 var last2 = 0;
741 switch (recType) {
caryclark26ad22a2015-10-16 09:03:38 -0700742 case REC_TYPE_ALIGNED:
743 hasAlignedPath = true;
caryclarkdac1d172014-06-17 05:15:38 -0700744 case REC_TYPE_COMPUTED:
745 if (fragType == COMPUTED_SET_1 || fragType == COMPUTED_SET_2) {
746 break;
747 }
caryclark26ad22a2015-10-16 09:03:38 -0700748 if (REC_TYPE_COMPUTED == recType) {
749 hasComputedPath = true;
750 }
caryclarkdac1d172014-06-17 05:15:38 -0700751 case REC_TYPE_PATH:
caryclark54359292015-03-26 07:52:43 -0700752 first = 1;
caryclarkdac1d172014-06-17 05:15:38 -0700753 switch (fragType) {
754 case PATH_LINE:
caryclark54359292015-03-26 07:52:43 -0700755 last = 5;
caryclarkdac1d172014-06-17 05:15:38 -0700756 break;
caryclark1049f122015-04-20 08:31:59 -0700757 case PATH_CONIC:
caryclarkdac1d172014-06-17 05:15:38 -0700758 case PATH_QUAD:
caryclark54359292015-03-26 07:52:43 -0700759 last = 7;
caryclarkdac1d172014-06-17 05:15:38 -0700760 break;
761 case PATH_CUBIC:
caryclark54359292015-03-26 07:52:43 -0700762 last = 9;
caryclarkdac1d172014-06-17 05:15:38 -0700763 break;
764 default:
caryclark55888e42016-07-18 10:01:36 -0700765 console.log("unknown " + (recType == REC_TYPE_PATH ? "REC_TYPE_PATH"
caryclarkdac1d172014-06-17 05:15:38 -0700766 : "REC_TYPE_COMPUTED") + " frag type:" + fragType);
767 throw "stop execution";
768 }
769 if (recType == REC_TYPE_PATH) {
770 hasPath = true;
771 }
772 break;
caryclark54359292015-03-26 07:52:43 -0700773 case REC_TYPE_PATH2:
774 first = 1;
775 switch (fragType) {
776 case PATH_LINE:
777 last = 5;
778 break;
caryclark1049f122015-04-20 08:31:59 -0700779 case PATH_CONIC:
caryclark54359292015-03-26 07:52:43 -0700780 case PATH_QUAD:
781 last = 7;
782 break;
783 case PATH_CUBIC:
784 last = 9;
785 break;
786 default:
caryclark55888e42016-07-18 10:01:36 -0700787 console.log("unknown " + (recType == REC_TYPE_PATH2 ? "REC_TYPE_PATH2"
caryclark54359292015-03-26 07:52:43 -0700788 : "REC_TYPE_COMPUTED") + " frag type:" + fragType);
789 throw "stop execution";
790 }
791 if (recType == REC_TYPE_PATH2) {
792 hasPath = true;
793 }
794 break;
caryclarkdac1d172014-06-17 05:15:38 -0700795 case REC_TYPE_ACTIVE:
796 if (firstActiveSpan < 0) {
797 firstActiveSpan = tIndex;
798 }
799 first = 1;
800 switch (fragType) {
801 case ACTIVE_LINE_SPAN:
802 last = 5;
803 break;
caryclark1049f122015-04-20 08:31:59 -0700804 case ACTIVE_CONIC_SPAN:
caryclarkdac1d172014-06-17 05:15:38 -0700805 case ACTIVE_QUAD_SPAN:
806 last = 7;
807 break;
808 case ACTIVE_CUBIC_SPAN:
809 last = 9;
810 break;
811 default:
812 console.log("unknown REC_TYPE_ACTIVE frag type: " + fragType);
813 throw "stop execution";
814 }
815 break;
816 case REC_TYPE_ADD:
817 switch (fragType) {
818 case ADD_MOVETO:
819 break;
820 case ADD_LINETO:
821 last = 4;
822 break;
caryclark1049f122015-04-20 08:31:59 -0700823 case ADD_CONICTO:
caryclarkdac1d172014-06-17 05:15:38 -0700824 case ADD_QUADTO:
825 last = 6;
826 break;
827 case ADD_CUBICTO:
828 last = 8;
829 break;
830 case ADD_CLOSE:
831 case ADD_FILL:
832 break;
833 default:
834 console.log("unknown REC_TYPE_ADD frag type: " + fragType);
835 throw "stop execution";
836 }
837 break;
caryclark54359292015-03-26 07:52:43 -0700838 case REC_TYPE_AFTERPART:
839 switch (fragType) {
840 case PATH_LINE:
841 last = 4;
842 break;
caryclark1049f122015-04-20 08:31:59 -0700843 case PATH_CONIC:
caryclark54359292015-03-26 07:52:43 -0700844 case PATH_QUAD:
845 last = 6;
846 break;
847 case PATH_CUBIC:
848 last = 8;
849 break;
850 default:
851 console.log("unknown REC_TYPE_ACTIVEPART frag type: " + fragType);
852 throw "stop execution";
853 }
854 break;
caryclarkdac1d172014-06-17 05:15:38 -0700855 case REC_TYPE_SECT:
856 switch (fragType) {
857 case INTERSECT_LINE:
858 first = 1; last = 5; first2 = 8; last2 = 12;
859 break;
860 case INTERSECT_LINE_2:
861 first = 1; last = 5; first2 = 11; last2 = 15;
862 break;
863 case INTERSECT_LINE_NO:
864 first = 0; last = 4; first2 = 4; last2 = 8;
865 break;
caryclark1049f122015-04-20 08:31:59 -0700866 case INTERSECT_CONIC_LINE:
867 first = 1; last = 7; first2 = 11; last2 = 15;
868 break;
caryclarkdac1d172014-06-17 05:15:38 -0700869 case INTERSECT_QUAD_LINE:
870 first = 1; last = 7; first2 = 10; last2 = 14;
871 break;
caryclark1049f122015-04-20 08:31:59 -0700872 case INTERSECT_CONIC_LINE_2:
873 first = 1; last = 7; first2 = 14; last2 = 18;
874 break;
caryclarkdac1d172014-06-17 05:15:38 -0700875 case INTERSECT_QUAD_LINE_2:
876 first = 1; last = 7; first2 = 13; last2 = 17;
877 break;
caryclark1049f122015-04-20 08:31:59 -0700878 case INTERSECT_CONIC_LINE_NO:
879 first = 0; last = 6; first2 = 7; last2 = 11;
880 break;
caryclarkdac1d172014-06-17 05:15:38 -0700881 case INTERSECT_QUAD_LINE_NO:
882 first = 0; last = 6; first2 = 6; last2 = 10;
883 break;
caryclark1049f122015-04-20 08:31:59 -0700884 case INTERSECT_CONIC:
885 first = 1; last = 7; first2 = 11; last2 = 17;
886 break;
caryclarkdac1d172014-06-17 05:15:38 -0700887 case INTERSECT_QUAD:
888 first = 1; last = 7; first2 = 10; last2 = 16;
889 break;
caryclark1049f122015-04-20 08:31:59 -0700890 case INTERSECT_CONIC_2:
891 first = 1; last = 7; first2 = 14; last2 = 20;
892 break;
caryclarkdac1d172014-06-17 05:15:38 -0700893 case INTERSECT_QUAD_2:
894 first = 1; last = 7; first2 = 13; last2 = 19;
895 break;
caryclark1049f122015-04-20 08:31:59 -0700896 case INTERSECT_CONIC_NO:
897 first = 0; last = 6; first2 = 7; last2 = 13;
898 break;
caryclarkdac1d172014-06-17 05:15:38 -0700899 case INTERSECT_QUAD_NO:
900 first = 0; last = 6; first2 = 6; last2 = 12;
901 break;
902 case INTERSECT_SELF_CUBIC:
903 first = 1; last = 9;
904 break;
905 case INTERSECT_SELF_CUBIC_NO:
906 first = 0; last = 8;
907 break;
908 case INTERSECT_CUBIC_LINE:
909 first = 1; last = 9; first2 = 12; last2 = 16;
910 break;
911 case INTERSECT_CUBIC_LINE_2:
912 first = 1; last = 9; first2 = 15; last2 = 19;
913 break;
914 case INTERSECT_CUBIC_LINE_3:
915 first = 1; last = 9; first2 = 18; last2 = 22;
916 break;
917 case INTERSECT_CUBIC_LINE_NO:
918 first = 0; last = 8; first2 = 8; last2 = 12;
919 break;
caryclark55888e42016-07-18 10:01:36 -0700920 case INTERSECT_CONIC_QUAD:
921 first = 1; last = 7; first2 = 11; last2 = 17;
922 break;
923 case INTERSECT_CONIC_QUAD_2:
924 first = 1; last = 7; first2 = 14; last2 = 20;
925 break;
926 case INTERSECT_CONIC_QUAD_NO:
927 first = 0; last = 6; first2 = 7; last2 = 13;
928 break;
caryclarkdac1d172014-06-17 05:15:38 -0700929 case INTERSECT_CUBIC_QUAD:
930 first = 1; last = 9; first2 = 12; last2 = 18;
931 break;
932 case INTERSECT_CUBIC_QUAD_2:
933 first = 1; last = 9; first2 = 15; last2 = 21;
934 break;
935 case INTERSECT_CUBIC_QUAD_3:
936 first = 1; last = 9; first2 = 18; last2 = 24;
937 break;
938 case INTERSECT_CUBIC_QUAD_4:
939 first = 1; last = 9; first2 = 21; last2 = 27;
940 break;
941 case INTERSECT_CUBIC_QUAD_NO:
942 first = 0; last = 8; first2 = 8; last2 = 14;
943 break;
944 case INTERSECT_CUBIC:
945 first = 1; last = 9; first2 = 12; last2 = 20;
946 break;
947 case INTERSECT_CUBIC_2:
948 first = 1; last = 9; first2 = 15; last2 = 23;
949 break;
950 case INTERSECT_CUBIC_3:
951 first = 1; last = 9; first2 = 18; last2 = 26;
952 break;
953 case INTERSECT_CUBIC_4:
954 first = 1; last = 9; first2 = 21; last2 = 29;
955 break;
956 case INTERSECT_CUBIC_NO:
957 first = 0; last = 8; first2 = 8; last2 = 16;
958 break;
959 default:
960 console.log("unknown REC_TYPE_SECT frag type: " + fragType);
961 throw "stop execution";
962 }
963 break;
964 default:
965 continue;
966 }
967 for (var idx = first; idx < last; idx += 2) {
968 xmin = Math.min(xmin, frags[idx]);
969 xmax = Math.max(xmax, frags[idx]);
970 ymin = Math.min(ymin, frags[idx + 1]);
971 ymax = Math.max(ymax, frags[idx + 1]);
972 }
973 for (var idx = first2; idx < last2; idx += 2) {
974 xmin = Math.min(xmin, frags[idx]);
975 xmax = Math.max(xmax, frags[idx]);
976 ymin = Math.min(ymin, frags[idx + 1]);
977 ymax = Math.max(ymax, frags[idx + 1]);
978 }
979 }
980 }
981 var angleBounds = [Infinity, Infinity, -Infinity, -Infinity];
982 for (var tIndex = 0; tIndex < test.length; tIndex += 3) {
983 var recType = test[tIndex];
984 var records = test[tIndex + 2];
985 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
986 var fragType = records[recordIndex];
987 var frags = records[recordIndex + 1];
988 switch (recType) {
989 case REC_TYPE_ACTIVE_OP:
990 if (!draw_op) {
991 break;
992 }
993 {
994 var curve = curvePartialByID(test, frags[0], frags[1], frags[2]);
995 curve_extremes(curve, angleBounds);
996 }
997 break;
998 case REC_TYPE_ANGLE:
999 if (!draw_angle) {
1000 break;
1001 }
caryclark54359292015-03-26 07:52:43 -07001002 {
caryclarkdac1d172014-06-17 05:15:38 -07001003 var curve = curvePartialByID(test, frags[0], frags[4], frags[5]);
1004 curve_extremes(curve, angleBounds);
1005 curve = curvePartialByID(test, frags[6], frags[10], frags[11]);
1006 curve_extremes(curve, angleBounds);
1007 curve = curvePartialByID(test, frags[12], frags[16], frags[17]);
1008 }
1009 break;
caryclark624637c2015-05-11 07:21:27 -07001010 case REC_TYPE_COINCIDENCE:
1011 if (!draw_coincidence) {
1012 break;
1013 }
1014 {
1015 var curve = curvePartialByID(test, frags[0], frags[1], frags[2]);
1016 curve_extremes(curve, angleBounds);
1017 }
1018 break;
caryclarkdac1d172014-06-17 05:15:38 -07001019 case REC_TYPE_SORT:
1020 if (!draw_sort) {
1021 break;
1022 }
1023 if (fragType == SORT_UNARY || fragType == SORT_BINARY) {
1024 var curve = curvePartialByID(test, frags[0], frags[6], frags[8]);
1025 curve_extremes(curve, angleBounds);
1026 }
1027 break;
caryclark03b03ca2015-04-23 09:13:37 -07001028 case REC_TYPE_TOP:
1029 if (!draw_top) {
1030 break;
1031 }
1032 {
1033 var curve = curvePartialByID(test, frags[0], frags[1], frags[2]);
1034 curve_extremes(curve, angleBounds);
1035 }
1036 break;
caryclarkdac1d172014-06-17 05:15:38 -07001037 }
1038 }
1039 }
1040 xmin = Math.min(xmin, angleBounds[0]);
1041 ymin = Math.min(ymin, angleBounds[1]);
1042 xmax = Math.max(xmax, angleBounds[2]);
1043 ymax = Math.max(ymax, angleBounds[3]);
1044 setScale(xmin, xmax, ymin, ymax);
1045 if (hasPath == false && hasComputedPath == true && !draw_computed) {
caryclark1049f122015-04-20 08:31:59 -07001046 draw_computed = 7; // show quadratics, conics, and cubics
caryclarkdac1d172014-06-17 05:15:38 -07001047 }
1048 if (hasPath == true && hasComputedPath == false && draw_computed) {
1049 draw_computed = 0;
1050 }
1051}
1052
caryclark26ad22a2015-10-16 09:03:38 -07001053function curveByIDMatch(test, id, recMatch) {
caryclark54359292015-03-26 07:52:43 -07001054 var tIndex = -3;
1055 while ((tIndex += 3) < test.length) {
caryclarkdac1d172014-06-17 05:15:38 -07001056 var recType = test[tIndex];
caryclark54359292015-03-26 07:52:43 -07001057 if (recType == REC_TYPE_OP) {
1058 continue;
1059 }
caryclark26ad22a2015-10-16 09:03:38 -07001060 if (recType != recMatch) {
caryclarkdac1d172014-06-17 05:15:38 -07001061 return [];
1062 }
1063 var records = test[tIndex + 2];
1064 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
1065 var fragType = records[recordIndex];
1066 var frags = records[recordIndex + 1];
1067 if (frags[0] == id) {
1068 switch (fragType) {
caryclark54359292015-03-26 07:52:43 -07001069 case PATH_LINE:
caryclarkdac1d172014-06-17 05:15:38 -07001070 return [frags[1], frags[2], frags[3], frags[4]];
caryclark54359292015-03-26 07:52:43 -07001071 case PATH_QUAD:
caryclarkdac1d172014-06-17 05:15:38 -07001072 return [frags[1], frags[2], frags[3], frags[4],
1073 frags[5], frags[6]];
caryclark1049f122015-04-20 08:31:59 -07001074 case PATH_CONIC:
1075 return [frags[1], frags[2], frags[3], frags[4],
1076 frags[5], frags[6], frags[7]];
caryclark54359292015-03-26 07:52:43 -07001077 case PATH_CUBIC:
caryclarkdac1d172014-06-17 05:15:38 -07001078 return [frags[1], frags[2], frags[3], frags[4],
1079 frags[5], frags[6], frags[7], frags[8]];
1080 }
1081 }
1082 }
caryclarkdac1d172014-06-17 05:15:38 -07001083 }
1084 return [];
1085}
1086
caryclark26ad22a2015-10-16 09:03:38 -07001087function curveByID(test, id) {
1088 var result = draw_path >= 4 ? curveByIDMatch(test, id, REC_TYPE_ALIGNED) : [];
1089 if (!result.length) {
1090 result = curveByIDMatch(test, id, REC_TYPE_PATH);
1091 }
1092 return result;
1093}
1094
1095function curvePartialByIDMatch(test, id, t0, t1, recMatch) {
caryclark54359292015-03-26 07:52:43 -07001096 var tIndex = -3;
1097 while ((tIndex += 3) < test.length) {
caryclarkdac1d172014-06-17 05:15:38 -07001098 var recType = test[tIndex];
caryclark54359292015-03-26 07:52:43 -07001099 if (recType == REC_TYPE_OP) {
1100 continue;
1101 }
caryclark26ad22a2015-10-16 09:03:38 -07001102 if (recType != recMatch) {
caryclarkdac1d172014-06-17 05:15:38 -07001103 return [];
1104 }
1105 var records = test[tIndex + 2];
1106 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
1107 var fragType = records[recordIndex];
1108 var frags = records[recordIndex + 1];
1109 if (frags[0] == id) {
1110 switch (fragType) {
caryclark54359292015-03-26 07:52:43 -07001111 case PATH_LINE:
caryclarkdac1d172014-06-17 05:15:38 -07001112 return linePartial(frags[1], frags[2], frags[3], frags[4], t0, t1);
caryclark54359292015-03-26 07:52:43 -07001113 case PATH_QUAD:
caryclarkdac1d172014-06-17 05:15:38 -07001114 return quadPartial(frags[1], frags[2], frags[3], frags[4],
1115 frags[5], frags[6], t0, t1);
caryclark1049f122015-04-20 08:31:59 -07001116 case PATH_CONIC:
1117 return conicPartial(frags[1], frags[2], frags[3], frags[4],
1118 frags[5], frags[6], frags[7], t0, t1);
caryclark54359292015-03-26 07:52:43 -07001119 case PATH_CUBIC:
caryclarkdac1d172014-06-17 05:15:38 -07001120 return cubicPartial(frags[1], frags[2], frags[3], frags[4],
1121 frags[5], frags[6], frags[7], frags[8], t0, t1);
1122 }
1123 }
1124 }
caryclarkdac1d172014-06-17 05:15:38 -07001125 }
1126 return [];
1127}
1128
caryclark26ad22a2015-10-16 09:03:38 -07001129function curvePartialByID(test, id, t0, t1) {
1130 var result = draw_path >= 4 ? curvePartialByIDMatch(test, id, t0, t1, REC_TYPE_ALIGNED) : [];
1131 if (!result.length) {
1132 result = curvePartialByIDMatch(test, id, t0, t1, REC_TYPE_PATH);
1133 }
1134 return result;
1135}
1136
1137function idByCurveIDMatch(test, frag, type, recMatch) {
caryclark54359292015-03-26 07:52:43 -07001138 var tIndex = 0;
caryclarkdac1d172014-06-17 05:15:38 -07001139 while (tIndex < test.length) {
1140 var recType = test[tIndex];
caryclark26ad22a2015-10-16 09:03:38 -07001141 if (recType != recMatch) {
caryclark54359292015-03-26 07:52:43 -07001142 ++tIndex;
1143 continue;
caryclarkdac1d172014-06-17 05:15:38 -07001144 }
1145 var records = test[tIndex + 2];
1146 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
1147 var fragType = records[recordIndex];
1148 var frags = records[recordIndex + 1];
caryclark54359292015-03-26 07:52:43 -07001149 if (frag.length != frags.length - 1) {
1150 continue;
1151 }
caryclarkdac1d172014-06-17 05:15:38 -07001152 switch (fragType) {
caryclark54359292015-03-26 07:52:43 -07001153 case PATH_LINE:
caryclarkdac1d172014-06-17 05:15:38 -07001154 if (frag[0] != frags[1] || frag[1] != frags[2]
1155 || frag[2] != frags[3] || frag[3] != frags[4]) {
1156 continue;
1157 }
1158 return frags[0];
caryclark54359292015-03-26 07:52:43 -07001159 case PATH_QUAD:
caryclarkdac1d172014-06-17 05:15:38 -07001160 if (frag[0] != frags[1] || frag[1] != frags[2]
1161 || frag[2] != frags[3] || frag[3] != frags[4]
1162 || frag[4] != frags[5] || frag[5] != frags[6]) {
1163 continue;
1164 }
1165 return frags[0];
caryclark1049f122015-04-20 08:31:59 -07001166 case PATH_CONIC:
1167 if (frag[0] != frags[1] || frag[1] != frags[2]
1168 || frag[2] != frags[3] || frag[3] != frags[4]
1169 || frag[4] != frags[5] || frag[5] != frags[6]
1170 || frag[6] != frags[7]) {
1171 continue;
1172 }
1173 return frags[0];
caryclark54359292015-03-26 07:52:43 -07001174 case PATH_CUBIC:
caryclarkdac1d172014-06-17 05:15:38 -07001175 if (frag[0] != frags[1] || frag[1] != frags[2]
1176 || frag[2] != frags[3] || frag[3] != frags[4]
1177 || frag[4] != frags[5] || frag[5] != frags[6]
1178 || frag[6] != frags[7] || frag[7] != frags[8]) {
1179 continue;
1180 }
1181 return frags[0];
1182 }
1183 }
1184 ++tIndex;
1185 }
1186 return -1;
1187}
1188
caryclark26ad22a2015-10-16 09:03:38 -07001189function idByCurve(test, frag, type) {
1190 var result = draw_path >= 4 ? idByCurveIDMatch(test, frag, type, REC_TYPE_ALIGNED) : [];
1191 if (!result.length) {
1192 result = idByCurveIDMatch(test, frag, type, REC_TYPE_PATH);
1193 }
1194 return result;
1195}
1196
caryclarkdac1d172014-06-17 05:15:38 -07001197function curve_extremes(curve, bounds) {
caryclark1049f122015-04-20 08:31:59 -07001198 var length = curve.length == 7 ? 6 : curve.length;
caryclarked0935a2015-10-22 07:23:52 -07001199 for (var index = 0; index < length; index += 2) {
caryclarkdac1d172014-06-17 05:15:38 -07001200 var x = curve[index];
1201 var y = curve[index + 1];
1202 bounds[0] = Math.min(bounds[0], x);
1203 bounds[1] = Math.min(bounds[1], y);
1204 bounds[2] = Math.max(bounds[2], x);
1205 bounds[3] = Math.max(bounds[3], y);
1206 }
1207}
1208
1209function setScale(x0, x1, y0, y1) {
1210 var srcWidth = x1 - x0;
1211 var srcHeight = y1 - y0;
1212 var usableWidth = screenWidth;
1213 var xDigits = Math.ceil(Math.log(Math.abs(xmax)) / Math.log(10));
1214 var yDigits = Math.ceil(Math.log(Math.abs(ymax)) / Math.log(10));
1215 usableWidth -= (xDigits + yDigits) * 10;
1216 usableWidth -= decimal_places * 10;
1217 if (draw_legend) {
1218 usableWidth -= 40;
1219 }
1220 var hscale = usableWidth / srcWidth;
1221 var vscale = screenHeight / srcHeight;
1222 scale = Math.min(hscale, vscale);
1223 var invScale = 1 / scale;
1224 var sxmin = x0 - invScale * 5;
1225 var symin = y0 - invScale * 10;
1226 var sxmax = x1 + invScale * (6 * decimal_places + 10);
1227 var symax = y1 + invScale * 10;
1228 srcWidth = sxmax - sxmin;
1229 srcHeight = symax - symin;
1230 hscale = usableWidth / srcWidth;
1231 vscale = screenHeight / srcHeight;
1232 scale = Math.min(hscale, vscale);
1233 srcLeft = sxmin;
1234 srcTop = symin;
1235}
1236
1237function drawArc(curve, op, from, to) {
1238 var type = PATH_LINE + (curve.length / 2 - 2);
1239 var pt = pointAtT(curve, type, op ? 0.4 : 0.6);
1240 var dy = pt.y - curve[1];
1241 var dx = pt.x - curve[0];
1242 var dist = Math.sqrt(dy * dy + dx * dx);
1243 var _dist = dist * scale;
1244 var angle = Math.atan2(dy, dx);
1245 var _px = (curve[0] - srcLeft) * scale;
1246 var _py = (curve[1] - srcTop) * scale;
1247 var divisor = 4;
1248 var endDist;
1249 do {
1250 var ends = [];
1251 for (var index = -1; index <= 1; index += 2) {
1252 var px = Math.cos(index * Math.PI / divisor);
1253 var py = Math.sin(index * Math.PI / divisor);
1254 ends.push(px);
1255 ends.push(py);
1256 }
1257 var endDx = (ends[2] - ends[0]) * scale * dist;
1258 var endDy = (ends[3] - ends[1]) * scale * dist;
1259 endDist = Math.sqrt(endDx * endDx + endDy * endDy);
1260 if (endDist < 100) {
1261 break;
1262 }
1263 divisor *= 2;
1264 } while (true);
1265 if (endDist < 30) {
1266 return;
1267 }
1268 if (op) {
1269 divisor *= 2;
1270 }
1271 ctx.strokeStyle = op ? "rgba(210,0,45, 0.4)" : "rgba(90,90,90, 0.5)";
1272 ctx.beginPath();
1273 ctx.arc(_px, _py, _dist, angle - Math.PI / divisor, angle + Math.PI / divisor, false);
1274 ctx.stroke();
1275 var saveAlign = ctx.textAlign;
1276 var saveStyle = ctx.fillStyle;
1277 var saveFont = ctx.font;
1278 ctx.textAlign = "center";
1279 ctx.fillStyle = "black";
1280 ctx.font = "normal 24px Arial";
1281 divisor *= 0.8;
1282 for (var index = -1; index <= 1; index += 2) {
1283 var px = curve[0] + Math.cos(angle + index * Math.PI / divisor) * dist;
1284 var py = curve[1] + Math.sin(angle + index * Math.PI / divisor) * dist;
1285 var _px = (px - srcLeft) * scale;
1286 var _py = (py - srcTop) * scale;
1287 ctx.fillText(index < 0 ? to.toString() : from.toString(), _px, _py + 8);
1288 }
1289 ctx.textAlign = saveAlign;
1290 ctx.fillStyle = saveStyle;
1291 ctx.font = saveFont;
1292}
1293
1294function drawPoint(px, py, end) {
caryclark1049f122015-04-20 08:31:59 -07001295 var length = drawnPts.length == 7 ? 6 : drawnPts.length;
1296 for (var pts = 0; pts < length; pts += 2) {
caryclarkdac1d172014-06-17 05:15:38 -07001297 var x = drawnPts[pts];
1298 var y = drawnPts[pts + 1];
1299 if (px == x && py == y) {
1300 return;
1301 }
1302 }
1303 drawnPts.push(px);
1304 drawnPts.push(py);
1305 var label = px.toFixed(decimal_places) + ", " + py.toFixed(decimal_places);
1306 var _px = (px - srcLeft) * scale;
1307 var _py = (py - srcTop) * scale;
1308 ctx.beginPath();
1309 ctx.arc(_px, _py, 3, 0, Math.PI*2, true);
1310 ctx.closePath();
1311 if (end) {
1312 ctx.fill();
1313 } else {
1314 ctx.stroke();
1315 }
1316 if (debug_xy) {
1317 ctx.textAlign = "left";
1318 ctx.fillText(label, _px + 5, _py);
1319 }
1320}
1321
caryclark1049f122015-04-20 08:31:59 -07001322function coordCount(curveType) {
1323 switch (curveType) {
1324 case PATH_LINE:
1325 return 4;
1326 case PATH_QUAD:
1327 return 6;
1328 case PATH_CONIC:
1329 return 6;
1330 case PATH_CUBIC:
1331 return 8;
1332 }
1333 return -1;
1334}
1335
caryclarkdac1d172014-06-17 05:15:38 -07001336function drawPoints(ptArray, curveType, drawControls) {
caryclark1049f122015-04-20 08:31:59 -07001337 var count = coordCount(curveType);
caryclarkdac1d172014-06-17 05:15:38 -07001338 for (var idx = 0; idx < count; idx += 2) {
1339 if (!drawControls && idx != 0 && idx != count - 2) {
1340 continue;
1341 }
1342 drawPoint(ptArray[idx], ptArray[idx + 1], idx == 0 || idx == count - 2);
1343 }
1344}
1345
1346function drawControlLines(curve, curveType, drawEnd) {
1347 if (curveType == PATH_LINE) {
1348 return;
1349 }
1350 ctx.strokeStyle = "rgba(0,0,0, 0.3)";
1351 drawLine(curve[0], curve[1], curve[2], curve[3]);
1352 drawLine(curve[2], curve[3], curve[4], curve[5]);
1353 if (curveType == PATH_CUBIC) {
1354 drawLine(curve[4], curve[5], curve[6], curve[7]);
1355 if (drawEnd > 1) {
1356 drawLine(curve[6], curve[7], curve[0], curve[1]);
1357 if (drawEnd > 2) {
1358 drawLine(curve[0], curve[1], curve[4], curve[5]);
1359 drawLine(curve[6], curve[7], curve[2], curve[3]);
1360 }
1361 }
1362 } else if (drawEnd > 1) {
1363 drawLine(curve[4], curve[5], curve[0], curve[1]);
1364 }
1365}
1366
1367function pointAtT(curve, curveType, t) {
1368 var xy = {};
1369 switch (curveType) {
1370 case PATH_LINE:
1371 var a = 1 - t;
1372 var b = t;
1373 xy.x = a * curve[0] + b * curve[2];
1374 xy.y = a * curve[1] + b * curve[3];
1375 break;
1376 case PATH_QUAD:
1377 var one_t = 1 - t;
1378 var a = one_t * one_t;
1379 var b = 2 * one_t * t;
1380 var c = t * t;
1381 xy.x = a * curve[0] + b * curve[2] + c * curve[4];
1382 xy.y = a * curve[1] + b * curve[3] + c * curve[5];
1383 break;
caryclark1049f122015-04-20 08:31:59 -07001384 case PATH_CONIC:
1385 var one_t = 1 - t;
1386 var a = one_t * one_t;
1387 var b = 2 * one_t * t;
1388 var c = t * t;
1389 xy.x = a * curve[0] + b * curve[2] * curve[6] + c * curve[4];
1390 xy.y = a * curve[1] + b * curve[3] * curve[6] + c * curve[5];
1391 var d = a + b * curve[6] + c;
1392 xy.x /= d;
1393 xy.y /= d;
1394 break;
caryclarkdac1d172014-06-17 05:15:38 -07001395 case PATH_CUBIC:
1396 var one_t = 1 - t;
1397 var one_t2 = one_t * one_t;
1398 var a = one_t2 * one_t;
1399 var b = 3 * one_t2 * t;
1400 var t2 = t * t;
1401 var c = 3 * one_t * t2;
1402 var d = t2 * t;
1403 xy.x = a * curve[0] + b * curve[2] + c * curve[4] + d * curve[6];
1404 xy.y = a * curve[1] + b * curve[3] + c * curve[5] + d * curve[7];
1405 break;
1406 }
1407 return xy;
1408}
caryclark55888e42016-07-18 10:01:36 -07001409
caryclarkdac1d172014-06-17 05:15:38 -07001410function drawPointAtT(curve, curveType) {
1411 var x, y;
1412 var xy = pointAtT(curve, curveType, curveT);
1413 drawPoint(xy.x, xy.y, true);
1414 if (!draw_intersectT) {
1415 return;
1416 }
1417 ctx.fillStyle = "red";
1418 drawTAtPointUp(xy.x, xy.y, curveT);
1419}
1420
1421function drawTAtPointUp(px, py, t) {
1422 var label = t.toFixed(decimal_places);
1423 var _px = (px - srcLeft)* scale;
1424 var _py = (py - srcTop) * scale;
1425 ctx.fillText(label, _px + 5, _py - 10);
1426}
1427
1428function drawTAtPointDown(px, py, t) {
1429 var label = t.toFixed(decimal_places);
1430 var _px = (px - srcLeft)* scale;
1431 var _py = (py - srcTop) * scale;
1432 ctx.fillText(label, _px + 5, _py + 10);
1433}
1434
1435function alreadyDrawnLine(x1, y1, x2, y2) {
1436 if (collect_bounds) {
1437 if (focus_enabled) {
1438 focusXmin = Math.min(focusXmin, x1, x2);
1439 focusYmin = Math.min(focusYmin, y1, y2);
1440 focusXmax = Math.max(focusXmax, x1, x2);
1441 focusYmax = Math.max(focusYmax, y1, y2);
1442 }
1443 return true;
1444 }
1445 for (var pts = 0; pts < drawnLines.length; pts += 4) {
1446 if (x1 == drawnLines[pts] && y1 == drawnLines[pts + 1]
1447 && x2 == drawnLines[pts + 2] && y2 == drawnLines[pts + 3]) {
1448 return true;
1449 }
1450 }
1451 drawnLines.push(x1);
1452 drawnLines.push(y1);
1453 drawnLines.push(x2);
1454 drawnLines.push(y2);
1455 return false;
1456}
1457
1458function drawLine(x1, y1, x2, y2) {
1459 if (alreadyDrawnLine(x1, y1, x2, y2)) {
1460 return;
1461 }
1462 ctx.beginPath();
1463 ctx.moveTo((x1 - srcLeft) * scale,
1464 (y1 - srcTop) * scale);
1465 ctx.lineTo((x2 - srcLeft) * scale,
1466 (y2 - srcTop) * scale);
1467 ctx.stroke();
1468}
1469
1470function linePartial(x1, y1, x2, y2, t1, t2) {
1471 var dx = x1 - x2;
1472 var dy = y1 - y2;
1473 var array = [
1474 x1 - t1 * dx,
1475 y1 - t1 * dy,
1476 x1 - t2 * dx,
1477 y1 - t2 * dy
1478 ];
1479 return array;
1480}
1481
1482function drawLinePartial(x1, y1, x2, y2, t1, t2) {
1483 var a = linePartial(x1, y1, x2, y2, t1, t2);
1484 var ax = a[0];
1485 var ay = a[1];
1486 var bx = a[2];
1487 var by = a[3];
1488 if (alreadyDrawnLine(ax, ay, bx, by)) {
1489 return;
1490 }
1491 ctx.beginPath();
1492 ctx.moveTo((ax - srcLeft) * scale,
1493 (ay - srcTop) * scale);
1494 ctx.lineTo((bx - srcLeft) * scale,
1495 (by - srcTop) * scale);
1496 ctx.stroke();
1497}
1498
1499function alreadyDrawnQuad(x1, y1, x2, y2, x3, y3) {
1500 if (collect_bounds) {
1501 if (focus_enabled) {
1502 focusXmin = Math.min(focusXmin, x1, x2, x3);
1503 focusYmin = Math.min(focusYmin, y1, y2, y3);
1504 focusXmax = Math.max(focusXmax, x1, x2, x3);
1505 focusYmax = Math.max(focusYmax, y1, y2, y3);
1506 }
1507 return true;
1508 }
1509 for (var pts = 0; pts < drawnQuads.length; pts += 6) {
1510 if (x1 == drawnQuads[pts] && y1 == drawnQuads[pts + 1]
1511 && x2 == drawnQuads[pts + 2] && y2 == drawnQuads[pts + 3]
1512 && x3 == drawnQuads[pts + 4] && y3 == drawnQuads[pts + 5]) {
1513 return true;
1514 }
1515 }
1516 drawnQuads.push(x1);
1517 drawnQuads.push(y1);
1518 drawnQuads.push(x2);
1519 drawnQuads.push(y2);
1520 drawnQuads.push(x3);
1521 drawnQuads.push(y3);
1522 return false;
1523}
1524
1525function drawQuad(x1, y1, x2, y2, x3, y3) {
1526 if (alreadyDrawnQuad(x1, y1, x2, y2, x3, y3)) {
1527 return;
1528 }
1529 ctx.beginPath();
1530 ctx.moveTo((x1 - srcLeft) * scale,
1531 (y1 - srcTop) * scale);
1532 ctx.quadraticCurveTo((x2 - srcLeft) * scale,
1533 (y2 - srcTop) * scale,
1534 (x3 - srcLeft) * scale,
1535 (y3 - srcTop) * scale);
1536 ctx.stroke();
1537}
1538
1539function interp(A, B, t) {
1540 return A + (B - A) * t;
1541}
1542
1543function interp_quad_coords(x1, x2, x3, t)
1544{
1545 var ab = interp(x1, x2, t);
1546 var bc = interp(x2, x3, t);
1547 var abc = interp(ab, bc, t);
1548 return abc;
1549}
1550
1551function quadPartial(x1, y1, x2, y2, x3, y3, t1, t2) {
1552 var ax = interp_quad_coords(x1, x2, x3, t1);
1553 var ay = interp_quad_coords(y1, y2, y3, t1);
1554 var dx = interp_quad_coords(x1, x2, x3, (t1 + t2) / 2);
1555 var dy = interp_quad_coords(y1, y2, y3, (t1 + t2) / 2);
1556 var cx = interp_quad_coords(x1, x2, x3, t2);
1557 var cy = interp_quad_coords(y1, y2, y3, t2);
1558 var bx = 2*dx - (ax + cx)/2;
1559 var by = 2*dy - (ay + cy)/2;
1560 var array = [
1561 ax, ay, bx, by, cx, cy
1562 ];
1563 return array;
1564}
1565
1566function drawQuadPartial(x1, y1, x2, y2, x3, y3, t1, t2) {
1567 var a = quadPartial(x1, y1, x2, y2, x3, y3, t1, t2);
1568 var ax = a[0];
1569 var ay = a[1];
1570 var bx = a[2];
1571 var by = a[3];
1572 var cx = a[4];
1573 var cy = a[5];
1574 if (alreadyDrawnQuad(ax, ay, bx, by, cx, cy)) {
1575 return;
1576 }
1577 ctx.beginPath();
1578 ctx.moveTo((ax - srcLeft) * scale,
1579 (ay - srcTop) * scale);
1580 ctx.quadraticCurveTo((bx - srcLeft) * scale,
1581 (by - srcTop) * scale,
1582 (cx - srcLeft) * scale,
1583 (cy - srcTop) * scale);
1584 ctx.stroke();
1585}
1586
caryclark1049f122015-04-20 08:31:59 -07001587function alreadyDrawnConic(x1, y1, x2, y2, x3, y3, w) {
1588 if (collect_bounds) {
1589 if (focus_enabled) {
1590 focusXmin = Math.min(focusXmin, x1, x2, x3);
1591 focusYmin = Math.min(focusYmin, y1, y2, y3);
1592 focusXmax = Math.max(focusXmax, x1, x2, x3);
1593 focusYmax = Math.max(focusYmax, y1, y2, y3);
1594 }
1595 return true;
1596 }
1597 for (var pts = 0; pts < drawnConics.length; pts += 8) {
1598 if (x1 == drawnConics[pts] && y1 == drawnCubics[pts + 1]
caryclark55888e42016-07-18 10:01:36 -07001599 && x2 == drawnCubics[pts + 2] && y2 == drawnCubics[pts + 3]
1600 && x3 == drawnCubics[pts + 4] && y3 == drawnCubics[pts + 5]
caryclark1049f122015-04-20 08:31:59 -07001601 && w == drawnCubics[pts + 6]) {
1602 return true;
1603 }
1604 }
1605 drawnConics.push(x1);
1606 drawnConics.push(y1);
1607 drawnConics.push(x2);
1608 drawnConics.push(y2);
1609 drawnConics.push(x3);
1610 drawnConics.push(y3);
1611 drawnCubics.push(w);
1612 return false;
1613}
1614
1615var kMaxConicToQuadPOW2 = 5;
1616
1617function computeQuadPOW2(curve, tol) {
1618 var a = curve[6] - 1;
1619 var k = a / (4 * (2 + a));
1620 var x = k * (curve[0] - 2 * curve[2] + curve[4]);
1621 var y = k * (curve[1] - 2 * curve[3] + curve[5]);
1622
1623 var error = Math.sqrt(x * x + y * y);
1624 var pow2;
1625 for (pow2 = 0; pow2 < kMaxConicToQuadPOW2; ++pow2) {
1626 if (error <= tol) {
1627 break;
1628 }
1629 error *= 0.25;
1630 }
1631 return pow2;
1632}
1633
1634function subdivide_w_value(w) {
1635 return Math.sqrt(0.5 + w * 0.5);
1636}
1637
1638function chop(curve, part1, part2) {
1639 var w = curve[6];
1640 var scale = 1 / (1 + w);
1641 part1[0] = curve[0];
1642 part1[1] = curve[1];
1643 part1[2] = (curve[0] + curve[2] * w) * scale;
1644 part1[3] = (curve[1] + curve[3] * w) * scale;
1645 part1[4] = part2[0] = (curve[0] + (curve[2] * w) * 2 + curve[4]) * scale * 0.5;
1646 part1[5] = part2[1] = (curve[1] + (curve[3] * w) * 2 + curve[5]) * scale * 0.5;
1647 part2[2] = (curve[2] * w + curve[4]) * scale;
1648 part2[3] = (curve[3] * w + curve[5]) * scale;
1649 part2[4] = curve[4];
1650 part2[5] = curve[5];
1651 part1[6] = part2[6] = subdivide_w_value(w);
1652}
1653
1654function subdivide(curve, level, pts) {
1655 if (0 == level) {
1656 pts.push(curve[2]);
1657 pts.push(curve[3]);
1658 pts.push(curve[4]);
1659 pts.push(curve[5]);
1660 } else {
1661 var part1 = [], part2 = [];
1662 chop(curve, part1, part2);
1663 --level;
1664 subdivide(part1, level, pts);
1665 subdivide(part2, level, pts);
1666 }
1667}
1668
1669function chopIntoQuadsPOW2(curve, pow2, pts) {
1670 subdivide(curve, pow2, pts);
1671 return 1 << pow2;
1672}
1673
1674function drawConicWithQuads(x1, y1, x2, y2, x3, y3, w) {
1675 if (alreadyDrawnConic(x1, y1, x2, y2, x3, y3, w)) {
1676 return;
1677 }
1678 ctx.beginPath();
1679 ctx.moveTo((x1 - srcLeft) * scale,
1680 (y1 - srcTop) * scale);
1681 var tol = 1 / scale;
1682 var curve = [x1, y1, x2, y2, x3, y3, w];
1683 var pow2 = computeQuadPOW2(curve, tol);
1684 var pts = [];
1685 chopIntoQuadsPOW2(curve, pow2, pts);
1686 for (var i = 0; i < pts.length; i += 4) {
1687 ctx.quadraticCurveTo(
1688 (pts[i + 0] - srcLeft) * scale, (pts[i + 1] - srcTop) * scale,
1689 (pts[i + 2] - srcLeft) * scale, (pts[i + 3] - srcTop) * scale);
1690 }
1691 ctx.stroke();
1692}
1693
1694function conic_eval_numerator(x1, x2, x3, w, t) {
1695 var src2w = x2 * w;
1696 var C = x1;
1697 var A = x3 - 2 * src2w + C;
1698 var B = 2 * (src2w - C);
1699 return (A * t + B) * t + C;
1700}
1701
1702
1703function conic_eval_denominator(w, t) {
1704 var B = 2 * (w - 1);
1705 var C = 1;
1706 var A = -B;
1707 return (A * t + B) * t + C;
1708}
1709
1710function conicPartial(x1, y1, x2, y2, x3, y3, w, t1, t2) {
1711 var ax = conic_eval_numerator(x1, x2, x3, w, t1);
1712 var ay = conic_eval_numerator(y1, y2, y3, w, t1);
1713 var az = conic_eval_denominator(w, t1);
1714 var midT = (t1 + t2) / 2;
1715 var dx = conic_eval_numerator(x1, x2, x3, w, midT);
1716 var dy = conic_eval_numerator(y1, y2, y3, w, midT);
1717 var dz = conic_eval_denominator(w, midT);
1718 var cx = conic_eval_numerator(x1, x2, x3, w, t2);
1719 var cy = conic_eval_numerator(y1, y2, y3, w, t2);
1720 var cz = conic_eval_denominator(w, t2);
1721 var bx = 2 * dx - (ax + cx) / 2;
1722 var by = 2 * dy - (ay + cy) / 2;
1723 var bz = 2 * dz - (az + cz) / 2;
1724 var dt = t2 - t1;
1725 var dt_1 = 1 - dt;
caryclark1049f122015-04-20 08:31:59 -07001726 var array = [
caryclarked0935a2015-10-22 07:23:52 -07001727 ax / az, ay / az, bx / bz, by / bz, cx / cz, cy / cz, 0
caryclark1049f122015-04-20 08:31:59 -07001728 ];
caryclarked0935a2015-10-22 07:23:52 -07001729 var dMidAC = { x:(array[0] + array[4]) / 2, y:(array[1] + array[5]) / 2 };
1730 var dMid = { x:dx / dz, y:dy / dz };
1731 var dWNumer = { x:dMidAC.x - dMid.x, y:dMidAC.y - dMid.y };
1732 var dWDenom = { x:dMid.x - array[2], y:dMid.y - array[3] };
1733 var partW = Math.sqrt(dWNumer.x * dWNumer.x + dWNumer.y * dWNumer.y)
1734 / Math.sqrt(dWDenom.x * dWDenom.x + dWDenom.y * dWDenom.y);
1735 array[6] = partW;
caryclark1049f122015-04-20 08:31:59 -07001736 return array;
1737}
caryclark55888e42016-07-18 10:01:36 -07001738
caryclark1049f122015-04-20 08:31:59 -07001739function drawConicPartial(x1, y1, x2, y2, x3, y3, w, t1, t2) {
1740 var a = conicPartial(x1, y1, x2, y2, x3, y3, w, t1, t2);
1741 var ax = a[0];
1742 var ay = a[1];
1743 var bx = a[2];
1744 var by = a[3];
1745 var cx = a[4];
1746 var cy = a[5];
1747 var w_ = a[6];
1748 drawConicWithQuads(ax, ay, bx, by, cx, cy, w_);
1749}
1750
caryclarkdac1d172014-06-17 05:15:38 -07001751function alreadyDrawnCubic(x1, y1, x2, y2, x3, y3, x4, y4) {
1752 if (collect_bounds) {
1753 if (focus_enabled) {
1754 focusXmin = Math.min(focusXmin, x1, x2, x3, x4);
1755 focusYmin = Math.min(focusYmin, y1, y2, y3, y4);
1756 focusXmax = Math.max(focusXmax, x1, x2, x3, x4);
1757 focusYmax = Math.max(focusYmax, y1, y2, y3, y4);
1758 }
1759 return true;
1760 }
1761 for (var pts = 0; pts < drawnCubics.length; pts += 8) {
1762 if (x1 == drawnCubics[pts] && y1 == drawnCubics[pts + 1]
caryclark55888e42016-07-18 10:01:36 -07001763 && x2 == drawnCubics[pts + 2] && y2 == drawnCubics[pts + 3]
1764 && x3 == drawnCubics[pts + 4] && y3 == drawnCubics[pts + 5]
caryclarkdac1d172014-06-17 05:15:38 -07001765 && x4 == drawnCubics[pts + 6] && y4 == drawnCubics[pts + 7]) {
1766 return true;
1767 }
1768 }
1769 drawnCubics.push(x1);
1770 drawnCubics.push(y1);
1771 drawnCubics.push(x2);
1772 drawnCubics.push(y2);
1773 drawnCubics.push(x3);
1774 drawnCubics.push(y3);
1775 drawnCubics.push(x4);
1776 drawnCubics.push(y4);
1777 return false;
1778}
1779
1780function drawCubic(x1, y1, x2, y2, x3, y3, x4, y4) {
1781 if (alreadyDrawnCubic(x1, y1, x2, y2, x3, y3, x4, y4)) {
1782 return;
1783 }
1784 ctx.beginPath();
1785 ctx.moveTo((x1 - srcLeft) * scale,
1786 (y1 - srcTop) * scale);
1787 ctx.bezierCurveTo((x2 - srcLeft) * scale,
1788 (y2 - srcTop) * scale,
1789 (x3 - srcLeft) * scale,
1790 (y3 - srcTop) * scale,
1791 (x4 - srcLeft) * scale,
1792 (y4 - srcTop) * scale);
1793 ctx.stroke();
1794}
1795
1796function interp_cubic_coords(x1, x2, x3, x4, t)
1797{
1798 var ab = interp(x1, x2, t);
1799 var bc = interp(x2, x3, t);
1800 var cd = interp(x3, x4, t);
1801 var abc = interp(ab, bc, t);
1802 var bcd = interp(bc, cd, t);
1803 var abcd = interp(abc, bcd, t);
1804 return abcd;
1805}
1806
1807function cubicPartial(x1, y1, x2, y2, x3, y3, x4, y4, t1, t2) {
1808 var ax = interp_cubic_coords(x1, x2, x3, x4, t1);
1809 var ay = interp_cubic_coords(y1, y2, y3, y4, t1);
1810 var ex = interp_cubic_coords(x1, x2, x3, x4, (t1*2+t2)/3);
1811 var ey = interp_cubic_coords(y1, y2, y3, y4, (t1*2+t2)/3);
1812 var fx = interp_cubic_coords(x1, x2, x3, x4, (t1+t2*2)/3);
1813 var fy = interp_cubic_coords(y1, y2, y3, y4, (t1+t2*2)/3);
1814 var dx = interp_cubic_coords(x1, x2, x3, x4, t2);
1815 var dy = interp_cubic_coords(y1, y2, y3, y4, t2);
1816 var mx = ex * 27 - ax * 8 - dx;
1817 var my = ey * 27 - ay * 8 - dy;
1818 var nx = fx * 27 - ax - dx * 8;
1819 var ny = fy * 27 - ay - dy * 8;
1820 var bx = (mx * 2 - nx) / 18;
1821 var by = (my * 2 - ny) / 18;
1822 var cx = (nx * 2 - mx) / 18;
1823 var cy = (ny * 2 - my) / 18;
1824 var array = [
1825 ax, ay, bx, by, cx, cy, dx, dy
1826 ];
1827 return array;
1828}
caryclark55888e42016-07-18 10:01:36 -07001829
caryclarkdac1d172014-06-17 05:15:38 -07001830function drawCubicPartial(x1, y1, x2, y2, x3, y3, x4, y4, t1, t2) {
1831 var a = cubicPartial(x1, y1, x2, y2, x3, y3, x4, y4, t1, t2);
1832 var ax = a[0];
1833 var ay = a[1];
1834 var bx = a[2];
1835 var by = a[3];
1836 var cx = a[4];
1837 var cy = a[5];
1838 var dx = a[6];
1839 var dy = a[7];
1840 if (alreadyDrawnCubic(ax, ay, bx, by, cx, cy, dx, dy)) {
1841 return;
1842 }
1843 ctx.beginPath();
1844 ctx.moveTo((ax - srcLeft) * scale,
1845 (ay - srcTop) * scale);
1846 ctx.bezierCurveTo((bx - srcLeft) * scale,
1847 (by - srcTop) * scale,
1848 (cx - srcLeft) * scale,
1849 (cy - srcTop) * scale,
1850 (dx - srcLeft) * scale,
1851 (dy - srcTop) * scale);
1852 ctx.stroke();
1853}
1854
1855function drawCurve(c) {
1856 switch (c.length) {
1857 case 4:
1858 drawLine(c[0], c[1], c[2], c[3]);
1859 break;
1860 case 6:
1861 drawQuad(c[0], c[1], c[2], c[3], c[4], c[5]);
1862 break;
caryclark1049f122015-04-20 08:31:59 -07001863 case 7:
1864 drawConicWithQuads(c[0], c[1], c[2], c[3], c[4], c[5], c[6]);
1865 break;
caryclarkdac1d172014-06-17 05:15:38 -07001866 case 8:
1867 drawCubic(c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]);
1868 break;
1869 }
1870}
1871
1872function boundsWidth(pts) {
1873 var min = pts[0];
1874 var max = pts[0];
caryclark1049f122015-04-20 08:31:59 -07001875 var length = pts.length == 7 ? 6 : pts.length;
1876 for (var idx = 2; idx < length; idx += 2) {
caryclarkdac1d172014-06-17 05:15:38 -07001877 min = Math.min(min, pts[idx]);
1878 max = Math.max(max, pts[idx]);
1879 }
1880 return max - min;
1881}
1882
1883function boundsHeight(pts) {
1884 var min = pts[1];
1885 var max = pts[1];
caryclark1049f122015-04-20 08:31:59 -07001886 var length = pts.length == 7 ? 6 : pts.length;
1887 for (var idx = 3; idx < length; idx += 2) {
caryclarkdac1d172014-06-17 05:15:38 -07001888 min = Math.min(min, pts[idx]);
1889 max = Math.max(max, pts[idx]);
1890 }
1891 return max - min;
1892}
1893
1894function tangent(pts) {
1895 var dx = pts[2] - pts[0];
1896 var dy = pts[3] - pts[1];
1897 if (dx == 0 && dy == 0 && pts.length > 4) {
1898 dx = pts[4] - pts[0];
1899 dy = pts[5] - pts[1];
caryclark1049f122015-04-20 08:31:59 -07001900 if (dx == 0 && dy == 0 && pts.length == 8) {
caryclarkdac1d172014-06-17 05:15:38 -07001901 dx = pts[6] - pts[0];
1902 dy = pts[7] - pts[1];
1903 }
1904 }
1905 return Math.atan2(-dy, dx);
1906}
1907
1908function hodograph(cubic) {
1909 var hodo = [];
1910 hodo[0] = 3 * (cubic[2] - cubic[0]);
1911 hodo[1] = 3 * (cubic[3] - cubic[1]);
1912 hodo[2] = 3 * (cubic[4] - cubic[2]);
1913 hodo[3] = 3 * (cubic[5] - cubic[3]);
1914 hodo[4] = 3 * (cubic[6] - cubic[4]);
1915 hodo[5] = 3 * (cubic[7] - cubic[5]);
1916 return hodo;
1917}
1918
1919function hodograph2(cubic) {
1920 var quad = hodograph(cubic);
1921 var hodo = [];
1922 hodo[0] = 2 * (quad[2] - quad[0]);
1923 hodo[1] = 2 * (quad[3] - quad[1]);
1924 hodo[2] = 2 * (quad[4] - quad[2]);
1925 hodo[3] = 2 * (quad[5] - quad[3]);
1926 return hodo;
1927}
1928
1929function quadraticRootsReal(A, B, C, s) {
1930 if (A == 0) {
1931 if (B == 0) {
1932 s[0] = 0;
1933 return C == 0;
1934 }
1935 s[0] = -C / B;
1936 return 1;
1937 }
1938 /* normal form: x^2 + px + q = 0 */
1939 var p = B / (2 * A);
1940 var q = C / A;
1941 var p2 = p * p;
1942 if (p2 < q) {
1943 return 0;
1944 }
1945 var sqrt_D = 0;
1946 if (p2 > q) {
1947 sqrt_D = sqrt(p2 - q);
1948 }
1949 s[0] = sqrt_D - p;
1950 s[1] = -sqrt_D - p;
1951 return 1 + s[0] != s[1];
1952}
1953
1954function add_valid_ts(s, realRoots, t) {
1955 var foundRoots = 0;
1956 for (var index = 0; index < realRoots; ++index) {
1957 var tValue = s[index];
1958 if (tValue >= 0 && tValue <= 1) {
1959 for (var idx2 = 0; idx2 < foundRoots; ++idx2) {
1960 if (t[idx2] != tValue) {
1961 t[foundRoots++] = tValue;
1962 }
1963 }
1964 }
1965 }
1966 return foundRoots;
1967}
1968
1969function quadraticRootsValidT(a, b, c, t) {
1970 var s = [];
1971 var realRoots = quadraticRootsReal(A, B, C, s);
1972 var foundRoots = add_valid_ts(s, realRoots, t);
1973 return foundRoots != 0;
1974}
1975
1976function find_cubic_inflections(cubic, tValues) {
1977 var Ax = src[2] - src[0];
1978 var Ay = src[3] - src[1];
1979 var Bx = src[4] - 2 * src[2] + src[0];
1980 var By = src[5] - 2 * src[3] + src[1];
1981 var Cx = src[6] + 3 * (src[2] - src[4]) - src[0];
1982 var Cy = src[7] + 3 * (src[3] - src[5]) - src[1];
1983 return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx),
1984 Ax * By - Ay * Bx, tValues);
1985}
1986
1987function dxy_at_t(curve, type, t) {
1988 var dxy = {};
1989 if (type == PATH_QUAD) {
1990 var a = t - 1;
1991 var b = 1 - 2 * t;
1992 var c = t;
1993 dxy.x = a * curve[0] + b * curve[2] + c * curve[4];
1994 dxy.y = a * curve[1] + b * curve[3] + c * curve[5];
caryclark1049f122015-04-20 08:31:59 -07001995 } else if (type == PATH_CONIC) {
1996 var p20x = curve[4] - curve[0];
1997 var p20y = curve[5] - curve[1];
1998 var p10xw = (curve[2] - curve[0]) * curve[6];
1999 var p10yw = (curve[3] - curve[1]) * curve[6];
2000 var coeff0x = curve[6] * p20x - p20x;
2001 var coeff0y = curve[6] * p20y - p20y;
2002 var coeff1x = p20x - 2 * p10xw;
2003 var coeff1y = p20y - 2 * p10yw;
2004 dxy.x = t * (t * coeff0x + coeff1x) + p10xw;
2005 dxy.y = t * (t * coeff0y + coeff1y) + p10yw;
caryclarkdac1d172014-06-17 05:15:38 -07002006 } else if (type == PATH_CUBIC) {
2007 var one_t = 1 - t;
2008 var a = curve[0];
2009 var b = curve[2];
2010 var c = curve[4];
2011 var d = curve[6];
2012 dxy.x = 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
2013 a = curve[1];
2014 b = curve[3];
2015 c = curve[5];
2016 d = curve[7];
2017 dxy.y = 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
2018 }
2019 return dxy;
2020}
2021
2022function drawLabel(num, px, py) {
2023 ctx.beginPath();
2024 ctx.arc(px, py, 8, 0, Math.PI*2, true);
2025 ctx.closePath();
2026 ctx.strokeStyle = "rgba(0,0,0, 0.4)";
2027 ctx.lineWidth = num == 0 || num == 3 ? 2 : 1;
2028 ctx.stroke();
2029 ctx.fillStyle = "black";
2030 ctx.font = "normal 10px Arial";
2031 // ctx.rotate(0.001);
2032 ctx.fillText(num, px - 2, py + 3);
2033 // ctx.rotate(-0.001);
2034}
2035
2036function drawLabelX(ymin, num, loc) {
2037 var px = (loc - srcLeft) * scale;
2038 var py = (ymin - srcTop) * scale - 20;
2039 drawLabel(num, px, py);
2040}
2041
2042function drawLabelY(xmin, num, loc) {
2043 var px = (xmin - srcLeft) * scale - 20;
2044 var py = (loc - srcTop) * scale;
2045 drawLabel(num, px, py);
2046}
2047
2048function drawHodoOrigin(hx, hy, hMinX, hMinY, hMaxX, hMaxY) {
2049 ctx.beginPath();
2050 ctx.moveTo(hx, hy - 100);
2051 ctx.lineTo(hx, hy);
2052 ctx.strokeStyle = hMinY < 0 ? "green" : "blue";
2053 ctx.stroke();
2054 ctx.beginPath();
2055 ctx.moveTo(hx, hy);
2056 ctx.lineTo(hx, hy + 100);
2057 ctx.strokeStyle = hMaxY > 0 ? "green" : "blue";
2058 ctx.stroke();
2059 ctx.beginPath();
2060 ctx.moveTo(hx - 100, hy);
2061 ctx.lineTo(hx, hy);
2062 ctx.strokeStyle = hMinX < 0 ? "green" : "blue";
2063 ctx.stroke();
2064 ctx.beginPath();
2065 ctx.moveTo(hx, hy);
2066 ctx.lineTo(hx + 100, hy);
2067 ctx.strokeStyle = hMaxX > 0 ? "green" : "blue";
2068 ctx.stroke();
2069}
2070
2071function scalexy(x, y, mag) {
2072 var length = Math.sqrt(x * x + y * y);
2073 return mag / length;
2074}
2075
caryclark03b03ca2015-04-23 09:13:37 -07002076function drawArrow(x, y, dx, dy, s) {
2077 var dscale = scalexy(dx, dy, 1 / scale * 100 * s);
caryclarkdac1d172014-06-17 05:15:38 -07002078 dx *= dscale;
2079 dy *= dscale;
2080 ctx.beginPath();
2081 ctx.moveTo((x - srcLeft) * scale, (y - srcTop) * scale);
2082 x += dx;
2083 y += dy;
2084 ctx.lineTo((x - srcLeft) * scale, (y - srcTop) * scale);
2085 dx /= 10;
2086 dy /= 10;
2087 ctx.lineTo((x - dy - srcLeft) * scale, (y + dx - srcTop) * scale);
2088 ctx.lineTo((x + dx * 2 - srcLeft) * scale, (y + dy * 2 - srcTop) * scale);
2089 ctx.lineTo((x + dy - srcLeft) * scale, (y - dx - srcTop) * scale);
2090 ctx.lineTo((x - srcLeft) * scale, (y - srcTop) * scale);
2091 ctx.strokeStyle = "rgba(0,75,0, 0.4)";
2092 ctx.stroke();
2093}
2094
2095function x_at_t(curve, t) {
2096 var one_t = 1 - t;
2097 if (curve.length == 4) {
2098 return one_t * curve[0] + t * curve[2];
2099 }
2100 var one_t2 = one_t * one_t;
2101 var t2 = t * t;
2102 if (curve.length == 6) {
2103 return one_t2 * curve[0] + 2 * one_t * t * curve[2] + t2 * curve[4];
2104 }
caryclark1049f122015-04-20 08:31:59 -07002105 if (curve.length == 7) {
2106 return (one_t2 * curve[0] + 2 * one_t * t * curve[2] * curve[6] + t2 * curve[4])
2107 / (one_t2 +2 * one_t * t * curve[6] + t2);
2108 }
caryclarkdac1d172014-06-17 05:15:38 -07002109 var a = one_t2 * one_t;
2110 var b = 3 * one_t2 * t;
2111 var c = 3 * one_t * t2;
2112 var d = t2 * t;
2113 return a * curve[0] + b * curve[2] + c * curve[4] + d * curve[6];
2114}
2115
2116function y_at_t(curve, t) {
2117 var one_t = 1 - t;
2118 if (curve.length == 4) {
2119 return one_t * curve[1] + t * curve[3];
2120 }
2121 var one_t2 = one_t * one_t;
2122 var t2 = t * t;
2123 if (curve.length == 6) {
2124 return one_t2 * curve[1] + 2 * one_t * t * curve[3] + t2 * curve[5];
2125 }
caryclark1049f122015-04-20 08:31:59 -07002126 if (curve.length == 7) {
2127 return (one_t2 * curve[1] + 2 * one_t * t * curve[3] * curve[6] + t2 * curve[5])
2128 / (one_t2 +2 * one_t * t * curve[6] + t2);
2129 }
caryclarkdac1d172014-06-17 05:15:38 -07002130 var a = one_t2 * one_t;
2131 var b = 3 * one_t2 * t;
2132 var c = 3 * one_t * t2;
2133 var d = t2 * t;
2134 return a * curve[1] + b * curve[3] + c * curve[5] + d * curve[7];
2135}
2136
2137function drawOrder(curve, label) {
2138 var px = x_at_t(curve, 0.75);
2139 var py = y_at_t(curve, 0.75);
2140 var _px = (px - srcLeft) * scale;
2141 var _py = (py - srcTop) * scale;
2142 ctx.beginPath();
2143 ctx.arc(_px, _py, 15, 0, Math.PI * 2, true);
2144 ctx.closePath();
2145 ctx.fillStyle = "white";
2146 ctx.fill();
2147 if (label == 'L') {
2148 ctx.strokeStyle = "rgba(255,0,0, 1)";
2149 ctx.fillStyle = "rgba(255,0,0, 1)";
2150 } else {
2151 ctx.strokeStyle = "rgba(0,0,255, 1)";
2152 ctx.fillStyle = "rgba(0,0,255, 1)";
2153 }
2154 ctx.stroke();
2155 ctx.font = "normal 16px Arial";
2156 ctx.textAlign = "center";
2157 ctx.fillText(label, _px, _py + 5);
2158 ctx.font = "normal 10px Arial";
2159}
2160
2161function drawID(curve, id) {
2162 var px = x_at_t(curve, 0.5);
2163 var py = y_at_t(curve, 0.5);
2164 var _px = (px - srcLeft) * scale;
2165 var _py = (py - srcTop) * scale;
2166 draw_id_at(id, _px, _py);
2167}
2168
2169function draw_id_at(id, _px, _py) {
2170 ctx.beginPath();
2171 ctx.arc(_px, _py, 15, 0, Math.PI * 2, true);
2172 ctx.closePath();
2173 ctx.fillStyle = "white";
2174 ctx.fill();
2175 ctx.strokeStyle = "rgba(127,127,0, 1)";
2176 ctx.fillStyle = "rgba(127,127,0, 1)";
2177 ctx.stroke();
2178 ctx.font = "normal 16px Arial";
2179 ctx.textAlign = "center";
2180 ctx.fillText(id, _px, _py + 5);
2181 ctx.font = "normal 10px Arial";
2182}
2183
2184function drawLinePartialID(id, x1, y1, x2, y2, t1, t2) {
2185 var curve = [x1, y1, x2, y2];
2186 drawCurvePartialID(id, curve, t1, t2);
2187}
2188
caryclark55888e42016-07-18 10:01:36 -07002189function drawLineID(id, x1, y1, x2, y2) {
2190 drawLinePartialID(id, x1, y1, x2, y2, 0, 1);
2191}
2192
caryclarkdac1d172014-06-17 05:15:38 -07002193function drawQuadPartialID(id, x1, y1, x2, y2, x3, y3, t1, t2) {
2194 var curve = [x1, y1, x2, y2, x3, y3];
2195 drawCurvePartialID(id, curve, t1, t2);
2196}
2197
caryclark55888e42016-07-18 10:01:36 -07002198function drawQuadID(id, x1, y1, x2, y2, x3, y3) {
2199 drawQuadPartialID(id, x1, y1, x2, y2, x3, y3, 0, 1);
2200}
2201
caryclark1049f122015-04-20 08:31:59 -07002202function drawConicPartialID(id, x1, y1, x2, y2, x3, y3, w, t1, t2) {
2203 var curve = [x1, y1, x2, y2, x3, y3, w];
2204 drawCurvePartialID(id, curve, t1, t2);
2205}
2206
caryclark55888e42016-07-18 10:01:36 -07002207function drawConicID(id, x1, y1, x2, y2, x3, y3, w) {
2208 drawConicPartialID(id, x1, y1, x2, y2, x3, y3, w, 0, 1);
2209}
2210
caryclarkdac1d172014-06-17 05:15:38 -07002211function drawCubicPartialID(id, x1, y1, x2, y2, x3, y3, x4, y4, t1, t2) {
2212 var curve = [x1, y1, x2, y2, x3, y3, x4, y4];
2213 drawCurvePartialID(id, curve, t1, t2);
2214}
2215
caryclark55888e42016-07-18 10:01:36 -07002216function drawCubicID(id, x1, y1, x2, y2, x3, y3, x4, y4) {
2217 drawCubicPartialID(id, x1, y1, x2, y2, x3, y3, x4, y4, 0, 1);
2218}
2219
caryclarkdac1d172014-06-17 05:15:38 -07002220function drawCurvePartialID(id, curve, t1, t2) {
2221 var px = x_at_t(curve, (t1 + t2) / 2);
2222 var py = y_at_t(curve, (t1 + t2) / 2);
2223 var _px = (px - srcLeft) * scale;
2224 var _py = (py - srcTop) * scale;
2225 draw_id_at(id, _px, _py);
2226}
2227
2228function drawCurveSpecials(test, curve, type) {
2229 if (pt_labels) {
2230 drawPoints(curve, type, pt_labels == 2);
2231 }
2232 if (control_lines != 0) {
2233 drawControlLines(curve, type, control_lines);
2234 }
2235 if (curve_t) {
2236 drawPointAtT(curve, type);
2237 }
2238 if (draw_midpoint) {
2239 var mid = pointAtT(curve, type, 0.5);
2240 drawPoint(mid.x, mid.y, true);
2241 }
2242 if (draw_id) {
2243 var id = idByCurve(test, curve, type);
2244 if (id >= 0) {
2245 drawID(curve, id);
2246 }
2247 }
2248 if (type == PATH_LINE) {
2249 return;
2250 }
2251 if (draw_deriviatives > 0) {
2252 var d = dxy_at_t(curve, type, 0);
caryclark03b03ca2015-04-23 09:13:37 -07002253 drawArrow(curve[0], curve[1], d.x, d.y, 1);
caryclarkdac1d172014-06-17 05:15:38 -07002254 if (draw_deriviatives == 2) {
2255 d = dxy_at_t(curve, type, 1);
2256 if (type == PATH_CUBIC) {
caryclark03b03ca2015-04-23 09:13:37 -07002257 drawArrow(curve[6], curve[7], d.x, d.y, 1);
caryclarkdac1d172014-06-17 05:15:38 -07002258 } else {
caryclark03b03ca2015-04-23 09:13:37 -07002259 drawArrow(curve[4], curve[5], d.x, d.y, 1);
caryclarkdac1d172014-06-17 05:15:38 -07002260 }
2261 }
2262 if (draw_midpoint) {
2263 var mid = pointAtT(curve, type, 0.5);
2264 d = dxy_at_t(curve, type, 0.5);
caryclark03b03ca2015-04-23 09:13:37 -07002265 drawArrow(mid.x, mid.y, d.x, d.y, 1);
caryclarkdac1d172014-06-17 05:15:38 -07002266 }
2267 }
2268 if (type != PATH_CUBIC) {
2269 return;
2270 }
caryclarkdac1d172014-06-17 05:15:38 -07002271 if (draw_sequence) {
2272 var ymin = Math.min(curve[1], curve[3], curve[5], curve[7]);
2273 for (var i = 0; i < 8; i+= 2) {
2274 drawLabelX(ymin, i >> 1, curve[i]);
2275 }
2276 var xmin = Math.min(curve[0], curve[2], curve[4], curve[6]);
2277 for (var i = 1; i < 8; i+= 2) {
2278 drawLabelY(xmin, i >> 1, curve[i]);
2279 }
2280 }
2281}
2282
2283function logCurves(test) {
2284 for (curves in test) {
2285 var curve = test[curves];
2286 dumpCurve(curve);
2287 }
2288}
2289
2290function curveToString(curve) {
2291 var str = "{{";
caryclark1049f122015-04-20 08:31:59 -07002292 var length = curve.length == 7 ? 6 : curve.length;
2293 if (curve.length == 7) {
2294 str += "{";
2295 }
2296 for (i = 0; i < length; i += 2) {
caryclarkdac1d172014-06-17 05:15:38 -07002297 str += curve[i].toFixed(decimal_places) + "," + curve[i + 1].toFixed(decimal_places);
2298 if (i < curve.length - 2) {
2299 str += "}, {";
2300 }
2301 }
caryclark1049f122015-04-20 08:31:59 -07002302 str += "}";
2303 if (curve.length == 7) {
2304 str += "}, " + curve[6].toFixed(decimal_places);
2305 }
2306 str += "}";
caryclarkdac1d172014-06-17 05:15:38 -07002307 return str;
2308}
2309
2310function dumpCurve(curve) {
2311 console.log(curveToString(curve));
2312}
2313
2314function draw(test, lines, title) {
2315 ctx.fillStyle = "rgba(0,0,0, 0.1)";
2316 ctx.font = "normal 50px Arial";
2317 ctx.textAlign = "left";
2318 ctx.fillText(title, 50, 50);
2319 ctx.font = "normal 10px Arial";
2320 ctx.lineWidth = "1.001"; "0.999";
2321 var secondPath = test.length;
2322 var closeCount = 0;
2323 logStart = -1;
2324 logRange = 0;
2325 // find last active rec type at this step
2326 var curType = test[0];
2327 var curStep = 0;
2328 var hasOp = false;
2329 var lastActive = 0;
2330 var lastAdd = 0;
caryclark624637c2015-05-11 07:21:27 -07002331 var lastCoin = 0;
caryclarkdac1d172014-06-17 05:15:38 -07002332 var lastSect = 0;
2333 var lastSort = 0;
2334 var lastMark = 0;
caryclark03b03ca2015-04-23 09:13:37 -07002335 var lastTop = 0;
caryclarkdac1d172014-06-17 05:15:38 -07002336 activeCount = 0;
2337 addCount = 0;
2338 angleCount = 0;
2339 opCount = 0;
2340 sectCount = 0;
2341 sortCount = 0;
caryclark03b03ca2015-04-23 09:13:37 -07002342 topCount = 0;
caryclarkdac1d172014-06-17 05:15:38 -07002343 markCount = 0;
2344 activeMax = 0;
2345 addMax = 0;
2346 angleMax = 0;
caryclark624637c2015-05-11 07:21:27 -07002347 coinMax = 0;
caryclarkdac1d172014-06-17 05:15:38 -07002348 opMax = 0;
2349 sectMax = 0;
2350 sectMax2 = 0;
2351 sortMax = 0;
caryclark03b03ca2015-04-23 09:13:37 -07002352 topMax = 0;
caryclarkdac1d172014-06-17 05:15:38 -07002353 markMax = 0;
2354 lastIndex = test.length - 3;
2355 for (var tIndex = 0; tIndex < test.length; tIndex += 3) {
2356 var recType = test[tIndex];
2357 if (!typeof recType == 'number' || recType < REC_TYPE_UNKNOWN || recType > REC_TYPE_LAST) {
2358 console.log("unknown rec type: " + recType);
2359 throw "stop execution";
2360 }
2361 // if (curType == recType && curType != REC_TYPE_ADD) {
2362 // continue;
2363 // }
2364 var inStepRange = step_limit == 0 || curStep < step_limit;
2365 curType = recType;
2366 if (recType == REC_TYPE_OP) {
2367 hasOp = true;
2368 continue;
2369 }
2370 if (recType == REC_TYPE_UNKNOWN) {
2371 // these types do not advance step
2372 continue;
2373 }
2374 var bumpStep = false;
2375 var records = test[tIndex + 2];
2376 var fragType = records[0];
2377 if (recType == REC_TYPE_ADD) {
2378 if (records.length != 2) {
2379 console.log("expect only two elements: " + records.length);
2380 throw "stop execution";
2381 }
2382 if (fragType == ADD_MOVETO || fragType == ADD_CLOSE) {
2383 continue;
2384 }
2385 ++addMax;
2386 if (!draw_add || !inStepRange) {
2387 continue;
2388 }
2389 lastAdd = tIndex;
2390 ++addCount;
2391 bumpStep = true;
2392 }
2393 if (recType == REC_TYPE_PATH && hasOp) {
2394 secondPath = tIndex;
2395 }
caryclark54359292015-03-26 07:52:43 -07002396 if (recType == REC_TYPE_PATH2 && hasOp) {
2397 secondPath = tIndex;
2398 }
caryclarkdac1d172014-06-17 05:15:38 -07002399 if (recType == REC_TYPE_ACTIVE) {
2400 ++activeMax;
2401 if (!draw_active || !inStepRange) {
2402 continue;
2403 }
2404 lastActive = tIndex;
2405 ++activeCount;
2406 bumpStep = true;
2407 }
2408 if (recType == REC_TYPE_ACTIVE_OP) {
2409 ++opMax;
2410 if (!draw_op || !inStepRange) {
2411 continue;
2412 }
2413 lastOp = tIndex;
2414 ++opCount;
2415 bumpStep = true;
2416 }
caryclark54359292015-03-26 07:52:43 -07002417 if (recType == REC_TYPE_AFTERPART) {
2418 if (draw_angle != 3 || !inStepRange) {
2419 continue;
2420 }
2421 lastAngle = tIndex;
2422 ++angleCount;
2423 bumpStep = true;
2424 }
caryclarkdac1d172014-06-17 05:15:38 -07002425 if (recType == REC_TYPE_ANGLE) {
2426 ++angleMax;
caryclark54359292015-03-26 07:52:43 -07002427 if (draw_angle == 0 || draw_angle == 3 || !inStepRange) {
caryclarkdac1d172014-06-17 05:15:38 -07002428 continue;
2429 }
2430 lastAngle = tIndex;
2431 ++angleCount;
2432 bumpStep = true;
2433 }
caryclark624637c2015-05-11 07:21:27 -07002434 if (recType == REC_TYPE_COINCIDENCE) {
2435 ++coinMax;
2436 if (!draw_coincidence || !inStepRange) {
2437 continue;
2438 }
2439 lastCoin = tIndex;
2440 ++coinCount;
2441 bumpStep = true;
2442 }
caryclarkdac1d172014-06-17 05:15:38 -07002443 if (recType == REC_TYPE_SECT) {
2444 if (records.length != 2) {
2445 console.log("expect only two elements: " + records.length);
2446 throw "stop execution";
2447 }
2448 ++sectMax;
2449 var sectBump = 1;
2450 switch (fragType) {
2451 case INTERSECT_LINE:
2452 case INTERSECT_QUAD_LINE:
2453 case INTERSECT_QUAD:
caryclark1049f122015-04-20 08:31:59 -07002454 case INTERSECT_CONIC_LINE:
caryclark55888e42016-07-18 10:01:36 -07002455 case INTERSECT_CONIC_QUAD:
caryclark1049f122015-04-20 08:31:59 -07002456 case INTERSECT_CONIC:
caryclarkdac1d172014-06-17 05:15:38 -07002457 case INTERSECT_SELF_CUBIC:
2458 case INTERSECT_CUBIC_LINE:
2459 case INTERSECT_CUBIC_QUAD:
2460 case INTERSECT_CUBIC:
2461 sectBump = 1;
2462 break;
2463 case INTERSECT_LINE_2:
2464 case INTERSECT_QUAD_LINE_2:
2465 case INTERSECT_QUAD_2:
caryclark1049f122015-04-20 08:31:59 -07002466 case INTERSECT_CONIC_LINE_2:
caryclark55888e42016-07-18 10:01:36 -07002467 case INTERSECT_CONIC_QUAD_2:
caryclark1049f122015-04-20 08:31:59 -07002468 case INTERSECT_CONIC_2:
caryclarkdac1d172014-06-17 05:15:38 -07002469 case INTERSECT_CUBIC_LINE_2:
2470 case INTERSECT_CUBIC_QUAD_2:
2471 case INTERSECT_CUBIC_2:
2472 sectBump = 2;
2473 break;
2474 case INTERSECT_LINE_NO:
2475 case INTERSECT_QUAD_LINE_NO:
2476 case INTERSECT_QUAD_NO:
caryclark1049f122015-04-20 08:31:59 -07002477 case INTERSECT_CONIC_LINE_NO:
caryclark55888e42016-07-18 10:01:36 -07002478 case INTERSECT_CONIC_QUAD_NO:
caryclark1049f122015-04-20 08:31:59 -07002479 case INTERSECT_CONIC_NO:
caryclarkdac1d172014-06-17 05:15:38 -07002480 case INTERSECT_SELF_CUBIC_NO:
2481 case INTERSECT_CUBIC_LINE_NO:
2482 case INTERSECT_CUBIC_QUAD_NO:
2483 case INTERSECT_CUBIC_NO:
2484 sectBump = 0;
2485 break;
2486 case INTERSECT_CUBIC_LINE_3:
2487 case INTERSECT_CUBIC_QUAD_3:
2488 case INTERSECT_CUBIC_3:
2489 sectBump = 3;
2490 break;
2491 case INTERSECT_CUBIC_QUAD_4:
2492 case INTERSECT_CUBIC_4:
2493 sectBump = 4;
2494 break;
2495 default:
2496 console.log("missing case " + records.length);
2497 throw "stop execution";
2498 }
2499 sectMax2 += sectBump;
2500 if (draw_intersection <= 1 || !inStepRange) {
2501 continue;
2502 }
2503 lastSect = tIndex;
2504 sectCount += sectBump;
2505 bumpStep = true;
2506 }
2507 if (recType == REC_TYPE_SORT) {
2508 ++sortMax;
2509 if (!draw_sort || !inStepRange) {
2510 continue;
2511 }
2512 lastSort = tIndex;
2513 ++sortCount;
2514 bumpStep = true;
2515 }
caryclark03b03ca2015-04-23 09:13:37 -07002516 if (recType == REC_TYPE_TOP) {
2517 ++topMax;
2518 if (!draw_top || !inStepRange) {
2519 continue;
2520 }
2521 lastTop = tIndex;
2522 ++topCount;
2523 bumpStep = true;
2524 }
caryclarkdac1d172014-06-17 05:15:38 -07002525 if (recType == REC_TYPE_MARK) {
2526 ++markMax;
2527 if (!draw_mark || !inStepRange) {
2528 continue;
2529 }
2530 lastMark = tIndex;
2531 ++markCount;
2532 bumpStep = true;
2533 }
2534 if (bumpStep) {
2535 lastIndex = tIndex;
2536 logStart = test[tIndex + 1];
2537 logRange = records.length / 2;
2538 ++curStep;
2539 }
2540 }
2541 stepMax = (draw_add ? addMax : 0)
2542 + (draw_active ? activeMax : 0)
reed0dc4dd62015-03-24 13:55:33 -07002543 + (draw_angle ? angleMax : 0)
caryclark624637c2015-05-11 07:21:27 -07002544 + (draw_coincidence ? coinMax : 0)
caryclark54359292015-03-26 07:52:43 -07002545 + (draw_op ? opMax : 0)
caryclarkdac1d172014-06-17 05:15:38 -07002546 + (draw_sort ? sortMax : 0)
caryclark03b03ca2015-04-23 09:13:37 -07002547 + (draw_top ? topMax : 0)
caryclarkdac1d172014-06-17 05:15:38 -07002548 + (draw_mark ? markMax : 0)
2549 + (draw_intersection == 2 ? sectMax : draw_intersection == 3 ? sectMax2 : 0);
2550 if (stepMax == 0) {
caryclark624637c2015-05-11 07:21:27 -07002551 stepMax = addMax + activeMax + angleMax + coinMax + opMax + sortMax + topMax + markMax;
caryclarkdac1d172014-06-17 05:15:38 -07002552 }
2553 drawnPts = [];
2554 drawnLines = [];
2555 drawnQuads = [];
caryclark1049f122015-04-20 08:31:59 -07002556 drawnConics = [];
caryclarkdac1d172014-06-17 05:15:38 -07002557 drawnCubics = [];
2558 focusXmin = focusYmin = Infinity;
2559 focusXmax = focusYmax = -Infinity;
2560 var pathIndex = 0;
2561 var opLetter = 'S';
2562 for (var tIndex = lastIndex; tIndex >= 0; tIndex -= 3) {
2563 var recType = test[tIndex];
2564 var records = test[tIndex + 2];
2565 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
2566 var fragType = records[recordIndex];
2567 if (!typeof fragType == 'number' || fragType < 1 || fragType > FRAG_TYPE_LAST) {
2568 console.log("unknown in range frag type: " + fragType);
2569 throw "stop execution";
2570 }
2571 var frags = records[recordIndex + 1];
2572 focus_enabled = false;
2573 switch (recType) {
2574 case REC_TYPE_COMPUTED:
2575 if (draw_computed == 0) {
2576 continue;
2577 }
2578 ctx.lineWidth = 1;
2579 ctx.strokeStyle = pathIndex == 0 ? "black" : "red";
2580 ctx.fillStyle = "blue";
2581 var drawThis = false;
2582 switch (fragType) {
2583 case PATH_QUAD:
caryclark1049f122015-04-20 08:31:59 -07002584 if ((draw_computed & 0x9) == 1 || ((draw_computed & 8) != 0
2585 && (draw_computed & 7) == pathIndex)) {
caryclarkdac1d172014-06-17 05:15:38 -07002586 drawQuad(frags[0], frags[1], frags[2], frags[3],
2587 frags[4], frags[5]);
2588 drawThis = true;
2589 }
2590 break;
caryclark1049f122015-04-20 08:31:59 -07002591 case PATH_CONIC:
2592 if ((draw_computed & 0xA) == 2 || ((draw_computed & 8) != 0
2593 && (draw_computed & 7) == pathIndex)) {
2594 drawConicWithQuads(frags[0], frags[1], frags[2], frags[3],
2595 frags[4], frags[5], frags[6]);
2596 drawThis = true;
2597 }
2598 break;
caryclarkdac1d172014-06-17 05:15:38 -07002599 case PATH_CUBIC:
caryclark1049f122015-04-20 08:31:59 -07002600 if ((draw_computed & 0xC) == 4 || ((draw_computed & 8) != 0
2601 && (draw_computed & 7) == pathIndex)) {
caryclarkdac1d172014-06-17 05:15:38 -07002602 drawCubic(frags[0], frags[1], frags[2], frags[3],
2603 frags[4], frags[5], frags[6], frags[7]);
2604 drawThis = true;
2605 }
2606 ++pathIndex;
2607 break;
2608 case COMPUTED_SET_1:
2609 pathIndex = 0;
2610 break;
2611 case COMPUTED_SET_2:
2612 pathIndex = 1;
2613 break;
2614 default:
2615 console.log("unknown REC_TYPE_COMPUTED frag type: " + fragType);
2616 throw "stop execution";
2617 }
2618 if (!drawThis || collect_bounds) {
2619 break;
2620 }
2621 drawCurveSpecials(test, frags, fragType);
2622 break;
caryclark26ad22a2015-10-16 09:03:38 -07002623 case REC_TYPE_ALIGNED:
2624 if (draw_path < 4) {
2625 continue;
2626 }
caryclarkdac1d172014-06-17 05:15:38 -07002627 case REC_TYPE_PATH:
caryclark54359292015-03-26 07:52:43 -07002628 case REC_TYPE_PATH2:
caryclark26ad22a2015-10-16 09:03:38 -07002629 if (REC_TYPE_ALIGNED != recType && draw_path >= 4) {
2630 continue;
2631 }
caryclarkdac1d172014-06-17 05:15:38 -07002632 if (!draw_path) {
2633 continue;
2634 }
2635 var firstPath = tIndex < secondPath;
2636 if ((draw_path & (firstPath ? 1 : 2)) == 0) {
2637 continue;
2638 }
2639 ctx.lineWidth = 1;
2640 ctx.strokeStyle = firstPath ? "black" : "red";
2641 ctx.fillStyle = "blue";
caryclark55888e42016-07-18 10:01:36 -07002642 var frags2 = [];
caryclarkdac1d172014-06-17 05:15:38 -07002643 switch (fragType) {
2644 case PATH_LINE:
caryclark54359292015-03-26 07:52:43 -07002645 for (var i = 0; i < 4; ++ i) { frags2[i] = frags[i + 1]; }
2646 drawLine(frags2[0], frags2[1], frags2[2], frags2[3]);
caryclarkdac1d172014-06-17 05:15:38 -07002647 break;
2648 case PATH_QUAD:
caryclark54359292015-03-26 07:52:43 -07002649 for (var i = 0; i < 6; ++ i) { frags2[i] = frags[i + 1]; }
2650 drawQuad(frags2[0], frags2[1], frags2[2], frags2[3],
2651 frags2[4], frags2[5]);
caryclarkdac1d172014-06-17 05:15:38 -07002652 break;
caryclark1049f122015-04-20 08:31:59 -07002653 case PATH_CONIC:
2654 for (var i = 0; i < 7; ++ i) { frags2[i] = frags[i + 1]; }
2655 drawConicWithQuads(frags2[0], frags2[1], frags2[2], frags2[3],
2656 frags2[4], frags2[5], frags2[6]);
2657 break;
caryclarkdac1d172014-06-17 05:15:38 -07002658 case PATH_CUBIC:
caryclark54359292015-03-26 07:52:43 -07002659 for (var i = 0; i < 8; ++ i) { frags2[i] = frags[i + 1]; }
2660 drawCubic(frags2[0], frags2[1], frags2[2], frags2[3],
2661 frags2[4], frags2[5], frags2[6], frags2[7]);
caryclarkdac1d172014-06-17 05:15:38 -07002662 break;
2663 default:
caryclark26ad22a2015-10-16 09:03:38 -07002664 console.log("unknown " + recType + " frag type: " + fragType);
caryclarkdac1d172014-06-17 05:15:38 -07002665 throw "stop execution";
2666 }
2667 if (collect_bounds) {
2668 break;
2669 }
caryclark54359292015-03-26 07:52:43 -07002670 drawCurveSpecials(test, frags2, fragType);
caryclarkdac1d172014-06-17 05:15:38 -07002671 break;
2672 case REC_TYPE_OP:
2673 switch (fragType) {
2674 case OP_INTERSECT: opLetter = 'I'; break;
2675 case OP_DIFFERENCE: opLetter = 'D'; break;
2676 case OP_UNION: opLetter = 'U'; break;
2677 case OP_XOR: opLetter = 'X'; break;
2678 default:
2679 console.log("unknown REC_TYPE_OP frag type: " + fragType);
2680 throw "stop execution";
2681 }
2682 break;
2683 case REC_TYPE_ACTIVE:
2684 if (!draw_active || (step_limit > 0 && tIndex < lastActive)) {
2685 continue;
2686 }
2687 var x1 = frags[SPAN_X1];
2688 var y1 = frags[SPAN_Y1];
2689 var x2 = frags[SPAN_X2];
2690 var y2 = frags[SPAN_Y2];
caryclark55888e42016-07-18 10:01:36 -07002691 var x3, y3, x3, y4, w;
caryclarkdac1d172014-06-17 05:15:38 -07002692 ctx.lineWidth = 3;
2693 ctx.strokeStyle = "rgba(0,0,255, 0.3)";
2694 focus_enabled = true;
2695 switch (fragType) {
2696 case ACTIVE_LINE_SPAN:
caryclark55888e42016-07-18 10:01:36 -07002697 drawLine(x1, y1, x2, y2);
caryclarkdac1d172014-06-17 05:15:38 -07002698 if (draw_id) {
caryclark55888e42016-07-18 10:01:36 -07002699 drawLineID(frags[0], x1, y1, x2, y2);
2700 }
2701 if (pt_labels) {
2702 var curve = [x1, y1, x2, y2];
2703 ctx.fillStyle = "blue";
2704 drawPoints(curve, PATH_LINE, pt_labels == 2);
caryclarkdac1d172014-06-17 05:15:38 -07002705 }
2706 break;
2707 case ACTIVE_QUAD_SPAN:
2708 x3 = frags[SPAN_X3];
2709 y3 = frags[SPAN_Y3];
caryclark55888e42016-07-18 10:01:36 -07002710 drawQuad(x1, y1, x2, y2, x3, y3);
caryclarkdac1d172014-06-17 05:15:38 -07002711 if (draw_id) {
caryclark55888e42016-07-18 10:01:36 -07002712 drawQuadID(frags[0], x1, y1, x2, y2, x3, y3);
2713 }
2714 if (pt_labels) {
2715 var curve = [x1, y1, x2, y2, x3, y3];
2716 ctx.fillStyle = "blue";
2717 drawPoints(curve, PATH_QUAD, pt_labels == 2);
caryclarkdac1d172014-06-17 05:15:38 -07002718 }
2719 break;
caryclark1049f122015-04-20 08:31:59 -07002720 case ACTIVE_CONIC_SPAN:
2721 x3 = frags[SPAN_X3];
2722 y3 = frags[SPAN_Y3];
caryclark1049f122015-04-20 08:31:59 -07002723 w = frags[SPAN_K_W];
caryclark55888e42016-07-18 10:01:36 -07002724 drawConicWithQuads(x1, y1, x2, y2, x3, y3, w);
caryclark1049f122015-04-20 08:31:59 -07002725 if (draw_id) {
caryclark55888e42016-07-18 10:01:36 -07002726 drawConicID(frags[0], x1, y1, x2, y2, x3, y3, w);
2727 }
2728 if (pt_labels) {
2729 var curve = [x1, y1, x2, y2, x3, y3, w];
2730 ctx.fillStyle = "blue";
2731 drawPoints(curve, PATH_CONIC, pt_labels == 2);
caryclark1049f122015-04-20 08:31:59 -07002732 }
2733 break;
caryclarkdac1d172014-06-17 05:15:38 -07002734 case ACTIVE_CUBIC_SPAN:
2735 x3 = frags[SPAN_X3];
2736 y3 = frags[SPAN_Y3];
2737 x4 = frags[SPAN_X4];
2738 y4 = frags[SPAN_Y4];
caryclark55888e42016-07-18 10:01:36 -07002739 drawCubic(x1, y1, x2, y2, x3, y3, x4, y4);
caryclarkdac1d172014-06-17 05:15:38 -07002740 if (draw_id) {
caryclark55888e42016-07-18 10:01:36 -07002741 drawCubicID(frags[0], x1, y1, x2, y2, x3, y3, x4, y4);
2742 }
2743 if (pt_labels) {
2744 var curve = [x1, y1, x2, y2, x3, y3, x4, y4];
2745 ctx.fillStyle = "blue";
2746 drawPoints(curve, PATH_CUBIC, pt_labels == 2);
caryclarkdac1d172014-06-17 05:15:38 -07002747 }
2748 break;
2749 default:
2750 console.log("unknown REC_TYPE_ACTIVE frag type: " + fragType);
2751 throw "stop execution";
2752 }
2753 break;
2754 case REC_TYPE_ACTIVE_OP:
2755 if (!draw_op || (step_limit > 0 && tIndex < lastOp)) {
2756 continue;
2757 }
2758 focus_enabled = true;
2759 ctx.lineWidth = 3;
2760 var activeSpan = frags[7] == "1";
2761 ctx.strokeStyle = activeSpan ? "rgba(45,160,0, 0.3)" : "rgba(255,45,0, 0.5)";
2762 var curve = curvePartialByID(test, frags[0], frags[1], frags[2]);
2763 drawCurve(curve);
2764 if (draw_op > 1) {
2765 drawArc(curve, false, frags[3], frags[4]);
2766 drawArc(curve, true, frags[5], frags[6]);
2767 }
2768 break;
2769 case REC_TYPE_ADD:
2770 if (!draw_add) {
2771 continue;
2772 }
2773 ctx.lineWidth = 3;
2774 ctx.strokeStyle = closeCount == 0 ? "rgba(0,0,255, 0.3)"
2775 : closeCount == 1 ? "rgba(0,127,0, 0.3)"
2776 : closeCount == 2 ? "rgba(0,127,127, 0.3)"
2777 : closeCount == 3 ? "rgba(127,127,0, 0.3)"
2778 : "rgba(127,0,127, 0.3)";
2779 focus_enabled = true;
2780 switch (fragType) {
2781 case ADD_MOVETO:
2782 break;
2783 case ADD_LINETO:
2784 if (step_limit == 0 || tIndex >= lastAdd) {
2785 drawLine(frags[0], frags[1], frags[2], frags[3]);
2786 }
2787 break;
2788 case ADD_QUADTO:
2789 if (step_limit == 0 || tIndex >= lastAdd) {
2790 drawQuad(frags[0], frags[1], frags[2], frags[3], frags[4], frags[5]);
2791 }
2792 break;
caryclark1049f122015-04-20 08:31:59 -07002793 case ADD_CONICTO:
2794 if (step_limit == 0 || tIndex >= lastAdd) {
2795 drawConicWithQuads(frags[0], frags[1], frags[2], frags[3],
2796 frags[4], frags[5], frags[6]);
2797 }
2798 break;
caryclarkdac1d172014-06-17 05:15:38 -07002799 case ADD_CUBICTO:
2800 if (step_limit == 0 || tIndex >= lastAdd) {
2801 drawCubic(frags[0], frags[1], frags[2], frags[3],
2802 frags[4], frags[5], frags[6], frags[7]);
2803 }
2804 break;
2805 case ADD_CLOSE:
2806 ++closeCount;
2807 break;
2808 case ADD_FILL:
2809 break;
2810 default:
2811 console.log("unknown REC_TYPE_ADD frag type: " + fragType);
2812 throw "stop execution";
2813 }
2814 break;
2815 case REC_TYPE_ANGLE:
caryclark54359292015-03-26 07:52:43 -07002816 angleBetween = frags[18] == "T";
2817 afterIndex = 0;
2818 if (draw_angle == 0 || draw_angle == 3 || (step_limit > 0 && tIndex < lastAngle)) {
caryclarkdac1d172014-06-17 05:15:38 -07002819 continue;
2820 }
2821 focus_enabled = true;
2822 ctx.lineWidth = 3;
2823 ctx.strokeStyle = "rgba(127,45,127, 0.3)";
caryclark54359292015-03-26 07:52:43 -07002824 var leftCurve = curvePartialByID(test, frags[0], frags[4], frags[5]);
2825 var midCurve = curvePartialByID(test, frags[6], frags[10], frags[11]);
2826 var rightCurve = curvePartialByID(test, frags[12], frags[16], frags[17]);
caryclarkdac1d172014-06-17 05:15:38 -07002827 drawCurve(leftCurve);
2828 drawCurve(rightCurve);
caryclark54359292015-03-26 07:52:43 -07002829 ctx.strokeStyle = angleBetween ? "rgba(0,160,45, 0.3)" : "rgba(255,0,45, 0.5)";
caryclarkdac1d172014-06-17 05:15:38 -07002830 drawCurve(midCurve);
2831 if (draw_angle > 1) {
2832 drawOrder(leftCurve, 'L');
2833 drawOrder(rightCurve, 'R');
2834 }
2835 break;
caryclark54359292015-03-26 07:52:43 -07002836 case REC_TYPE_AFTERPART:
2837 if (draw_angle != 3 || (step_limit > 0 && tIndex < lastAngle)) {
2838 continue;
2839 }
2840 ctx.strokeStyle = afterIndex == 0 ? "rgba(255,0,0, 1.0)"
2841 : (afterIndex == 1) == angleBetween ? "rgba(0,128,0, 1.0)"
2842 : "rgba(0,0,255, 1.0)";
2843 switch (fragType) {
2844 case PATH_LINE:
2845 drawLine(frags[0], frags[1], frags[2], frags[3]);
2846 break;
2847 case PATH_QUAD:
2848 drawQuad(frags[0], frags[1], frags[2], frags[3],
2849 frags[4], frags[5]);
2850 break;
caryclark1049f122015-04-20 08:31:59 -07002851 case PATH_CONIC:
2852 drawConicWithQuads(frags[0], frags[1], frags[2], frags[3],
2853 frags[4], frags[5], frags[6]);
2854 break;
caryclark54359292015-03-26 07:52:43 -07002855 case PATH_CUBIC:
2856 drawCubic(frags[0], frags[1], frags[2], frags[3],
caryclark1049f122015-04-20 08:31:59 -07002857 frags[4], frags[5], frags[6], frags[7]);
caryclark54359292015-03-26 07:52:43 -07002858 break;
2859 default:
2860 console.log("unknown REC_TYPE_AFTERPART frag type: " + fragType);
2861 throw "stop execution";
2862 }
2863 ++afterIndex;
2864 break;
caryclark624637c2015-05-11 07:21:27 -07002865 case REC_TYPE_COINCIDENCE:
2866 if (!draw_coincidence || (step_limit > 0 && tIndex < lastCoin)) {
2867 continue;
2868 }
2869 focus_enabled = true;
2870 ctx.lineWidth = 3;
2871 ctx.strokeStyle = "rgba(127,45,63, 0.3)";
2872 var curve = curvePartialByID(test, frags[0], frags[1], frags[2]);
2873 drawCurve(curve);
2874 break;
caryclarkdac1d172014-06-17 05:15:38 -07002875 case REC_TYPE_SECT:
2876 if (!draw_intersection) {
2877 continue;
2878 }
2879 if (draw_intersection != 1 && (step_limit > 0 && tIndex < lastSect)) {
2880 continue;
2881 }
2882 // draw_intersection == 1 : show all
2883 // draw_intersection == 2 : step == 0 ? show all : show intersection line #step
2884 // draw_intersection == 3 : step == 0 ? show all : show intersection #step
2885 ctx.lineWidth = 1;
2886 ctx.strokeStyle = "rgba(0,0,255, 0.3)";
2887 ctx.fillStyle = "blue";
2888 focus_enabled = true;
2889 var f = [];
2890 var c1s;
2891 var c1l;
2892 var c2s;
2893 var c2l;
2894 switch (fragType) {
2895 case INTERSECT_LINE:
2896 f.push(5, 6, 0, 7);
2897 c1s = 1; c1l = 4; c2s = 8; c2l = 4;
2898 break;
2899 case INTERSECT_LINE_2:
2900 f.push(5, 6, 0, 10);
2901 f.push(8, 9, 7, 15);
2902 c1s = 1; c1l = 4; c2s = 11; c2l = 4;
2903 break;
2904 case INTERSECT_LINE_NO:
2905 c1s = 0; c1l = 4; c2s = 4; c2l = 4;
2906 break;
2907 case INTERSECT_QUAD_LINE:
2908 f.push(7, 8, 0, 9);
2909 c1s = 1; c1l = 6; c2s = 10; c2l = 4;
2910 break;
2911 case INTERSECT_QUAD_LINE_2:
2912 f.push(7, 8, 0, 12);
2913 f.push(10, 11, 9, 17);
2914 c1s = 1; c1l = 6; c2s = 13; c2l = 4;
2915 break;
2916 case INTERSECT_QUAD_LINE_NO:
2917 c1s = 0; c1l = 6; c2s = 6; c2l = 4;
2918 break;
2919 case INTERSECT_QUAD:
2920 f.push(7, 8, 0, 9);
2921 c1s = 1; c1l = 6; c2s = 10; c2l = 6;
2922 break;
2923 case INTERSECT_QUAD_2:
2924 f.push(7, 8, 0, 12);
2925 f.push(10, 11, 9, 19);
2926 c1s = 1; c1l = 6; c2s = 13; c2l = 6;
2927 break;
2928 case INTERSECT_QUAD_NO:
2929 c1s = 0; c1l = 6; c2s = 6; c2l = 6;
2930 break;
caryclark1049f122015-04-20 08:31:59 -07002931 case INTERSECT_CONIC_LINE:
2932 f.push(8, 9, 0, 10);
2933 c1s = 1; c1l = 7; c2s = 11; c2l = 4;
2934 break;
2935 case INTERSECT_CONIC_LINE_2:
2936 f.push(8, 9, 0, 12);
2937 f.push(11, 12, 10, 18);
2938 c1s = 1; c1l = 7; c2s = 14; c2l = 4;
2939 break;
2940 case INTERSECT_CONIC_LINE_NO:
2941 c1s = 0; c1l = 7; c2s = 7; c2l = 4;
2942 break;
caryclark55888e42016-07-18 10:01:36 -07002943 case INTERSECT_CONIC_QUAD:
2944 f.push(8, 9, 0, 10);
2945 c1s = 1; c1l = 7; c2s = 11; c2l = 6;
2946 break;
2947 case INTERSECT_CONIC_QUAD_2:
2948 f.push(8, 9, 0, 12);
2949 f.push(11, 12, 10, 18);
2950 c1s = 1; c1l = 7; c2s = 14; c2l = 6;
2951 break;
2952 case INTERSECT_CONIC_QUAD_NO:
2953 c1s = 0; c1l = 7; c2s = 7; c2l = 6;
2954 break;
caryclark1049f122015-04-20 08:31:59 -07002955 case INTERSECT_CONIC:
2956 f.push(8, 9, 0, 10);
2957 c1s = 1; c1l = 7; c2s = 11; c2l = 7;
2958 break;
2959 case INTERSECT_CONIC_2:
2960 f.push(8, 9, 0, 13);
2961 f.push(11, 12, 10, 21);
2962 c1s = 1; c1l = 7; c2s = 14; c2l = 7;
2963 break;
2964 case INTERSECT_CONIC_NO:
2965 c1s = 0; c1l = 7; c2s = 7; c2l = 7;
2966 break;
caryclarkdac1d172014-06-17 05:15:38 -07002967 case INTERSECT_SELF_CUBIC:
2968 f.push(9, 10, 0, 11);
2969 c1s = 1; c1l = 8; c2s = 0; c2l = 0;
2970 break;
2971 case INTERSECT_SELF_CUBIC_NO:
2972 c1s = 0; c1l = 8; c2s = 0; c2l = 0;
2973 break;
2974 case INTERSECT_CUBIC_LINE:
2975 f.push(9, 10, 0, 11);
2976 c1s = 1; c1l = 8; c2s = 12; c2l = 4;
2977 break;
2978 case INTERSECT_CUBIC_LINE_2:
2979 f.push(9, 10, 0, 14);
2980 f.push(12, 13, 11, 19);
2981 c1s = 1; c1l = 8; c2s = 15; c2l = 4;
2982 break;
2983 case INTERSECT_CUBIC_LINE_3:
2984 f.push(9, 10, 0, 17);
2985 f.push(12, 13, 11, 22);
2986 f.push(15, 16, 14, 23);
2987 c1s = 1; c1l = 8; c2s = 18; c2l = 4;
2988 break;
2989 case INTERSECT_CUBIC_QUAD_NO:
2990 c1s = 0; c1l = 8; c2s = 8; c2l = 6;
2991 break;
2992 case INTERSECT_CUBIC_QUAD:
2993 f.push(9, 10, 0, 11);
2994 c1s = 1; c1l = 8; c2s = 12; c2l = 6;
2995 break;
2996 case INTERSECT_CUBIC_QUAD_2:
2997 f.push(9, 10, 0, 14);
2998 f.push(12, 13, 11, 21);
2999 c1s = 1; c1l = 8; c2s = 15; c2l = 6;
3000 break;
3001 case INTERSECT_CUBIC_QUAD_3:
3002 f.push(9, 10, 0, 17);
3003 f.push(12, 13, 11, 24);
3004 f.push(15, 16, 14, 25);
3005 c1s = 1; c1l = 8; c2s = 18; c2l = 6;
3006 break;
3007 case INTERSECT_CUBIC_QUAD_4:
3008 f.push(9, 10, 0, 20);
3009 f.push(12, 13, 11, 27);
3010 f.push(15, 16, 14, 28);
3011 f.push(18, 19, 17, 29);
3012 c1s = 1; c1l = 8; c2s = 21; c2l = 6;
3013 break;
3014 case INTERSECT_CUBIC_LINE_NO:
3015 c1s = 0; c1l = 8; c2s = 8; c2l = 4;
3016 break;
3017 case INTERSECT_CUBIC:
3018 f.push(9, 10, 0, 11);
3019 c1s = 1; c1l = 8; c2s = 12; c2l = 8;
3020 break;
3021 case INTERSECT_CUBIC_2:
3022 f.push(9, 10, 0, 14);
3023 f.push(12, 13, 11, 23);
3024 c1s = 1; c1l = 8; c2s = 15; c2l = 8;
3025 break;
3026 case INTERSECT_CUBIC_3:
3027 f.push(9, 10, 0, 17);
3028 f.push(12, 13, 11, 26);
3029 f.push(15, 16, 14, 27);
3030 c1s = 1; c1l = 8; c2s = 18; c2l = 8;
3031 break;
3032 case INTERSECT_CUBIC_4:
3033 f.push(9, 10, 0, 20);
3034 f.push(12, 13, 11, 29);
3035 f.push(15, 16, 14, 30);
3036 f.push(18, 19, 17, 31);
3037 c1s = 1; c1l = 8; c2s = 21; c2l = 8;
3038 break;
3039 case INTERSECT_CUBIC_NO:
3040 c1s = 0; c1l = 8; c2s = 8; c2l = 8;
3041 break;
3042 default:
3043 console.log("unknown REC_TYPE_SECT frag type: " + fragType);
3044 throw "stop execution";
3045 }
3046 if (draw_intersection != 1) {
3047 var id = -1;
3048 var curve;
3049 switch (c1l) {
caryclark55888e42016-07-18 10:01:36 -07003050 case 4:
caryclarkdac1d172014-06-17 05:15:38 -07003051 drawLine(frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3]);
3052 if (draw_id) {
3053 curve = [frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3]];
3054 id = idByCurve(test, curve, PATH_LINE);
3055 }
3056 break;
3057 case 6:
3058 drawQuad(frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3],
3059 frags[c1s + 4], frags[c1s + 5]);
3060 if (draw_id) {
3061 curve = [frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3],
3062 frags[c1s + 4], frags[c1s + 5]];
3063 id = idByCurve(test, curve, PATH_QUAD);
3064 }
3065 break;
caryclark1049f122015-04-20 08:31:59 -07003066 case 7:
3067 drawConicWithQuads(frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3],
3068 frags[c1s + 4], frags[c1s + 5], frags[c1s + 6]);
3069 if (draw_id) {
3070 curve = [frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3],
3071 frags[c1s + 4], frags[c1s + 5], frags[c1s + 6]];
3072 id = idByCurve(test, curve, PATH_CONIC);
3073 }
3074 break;
caryclarkdac1d172014-06-17 05:15:38 -07003075 case 8:
3076 drawCubic(frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3],
3077 frags[c1s + 4], frags[c1s + 5], frags[c1s + 6], frags[c1s + 7]);
3078 if (draw_id) {
3079 curve = [frags[c1s], frags[c1s + 1], frags[c1s + 2], frags[c1s + 3],
3080 frags[c1s + 4], frags[c1s + 5], frags[c1s + 6], frags[c1s + 7]];
3081 id = idByCurve(test, curve, PATH_CUBIC);
3082 }
3083 break;
3084 }
3085 if (id >= 0) {
3086 drawID(curve, id);
3087 }
3088 id = -1;
3089 switch (c2l) {
3090 case 0:
3091 break;
caryclark55888e42016-07-18 10:01:36 -07003092 case 4:
caryclarkdac1d172014-06-17 05:15:38 -07003093 drawLine(frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3]);
3094 if (draw_id) {
3095 curve = [frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3]];
3096 id = idByCurve(test, curve, PATH_LINE);
3097 }
3098 break;
3099 case 6:
3100 drawQuad(frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3],
3101 frags[c2s + 4], frags[c2s + 5]);
3102 if (draw_id) {
3103 curve = [frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3],
3104 frags[c2s + 4], frags[c2s + 5]];
3105 id = idByCurve(test, curve, PATH_QUAD);
3106 }
3107 break;
caryclark1049f122015-04-20 08:31:59 -07003108 case 7:
3109 drawConicWithQuads(frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3],
3110 frags[c2s + 4], frags[c2s + 5], frags[c2s + 6]);
3111 if (draw_id) {
3112 curve = [frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3],
3113 frags[c2s + 4], frags[c2s + 5], frags[c2s + 6]];
3114 id = idByCurve(test, curve, PATH_CONIC);
3115 }
3116 break;
caryclarkdac1d172014-06-17 05:15:38 -07003117 case 8:
3118 drawCubic(frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3],
3119 frags[c2s + 4], frags[c2s + 5], frags[c2s + 6], frags[c2s + 7]);
3120 if (draw_id) {
3121 curve = [frags[c2s], frags[c2s + 1], frags[c2s + 2], frags[c2s + 3],
3122 frags[c2s + 4], frags[c2s + 5], frags[c2s + 6], frags[c2s + 7]];
3123 id = idByCurve(test, curve, PATH_CUBIC);
3124 }
3125 break;
3126 }
3127 if (id >= 0) {
3128 drawID(curve, id);
3129 }
3130 }
3131 if (collect_bounds) {
3132 break;
3133 }
caryclark54359292015-03-26 07:52:43 -07003134 if (draw_intersection != 3 || step_limit == 0 || tIndex >= lastSect) {
3135 for (var idx = 0; idx < f.length; idx += 4) {
caryclarkdac1d172014-06-17 05:15:38 -07003136 drawPoint(frags[f[idx]], frags[f[idx + 1]], true);
3137 }
3138 }
3139 if (!draw_intersectT) {
3140 break;
3141 }
3142 ctx.fillStyle = "red";
caryclark54359292015-03-26 07:52:43 -07003143 if (draw_intersection != 3 || step_limit == 0 || tIndex >= lastSect) {
3144 for (var idx = 0; idx < f.length; idx += 4) {
caryclarkdac1d172014-06-17 05:15:38 -07003145 drawTAtPointUp(frags[f[idx]], frags[f[idx + 1]], frags[f[idx + 2]]);
3146 drawTAtPointDown(frags[f[idx]], frags[f[idx + 1]], frags[f[idx + 3]]);
3147 }
3148 }
3149 break;
3150 case REC_TYPE_SORT:
3151 if (!draw_sort || (step_limit > 0 && tIndex < lastSort)) {
3152 continue;
3153 }
3154 ctx.lineWidth = 3;
3155 ctx.strokeStyle = "rgba(127,127,0, 0.5)";
3156 focus_enabled = true;
3157 switch (fragType) {
3158 case SORT_UNARY:
3159 case SORT_BINARY:
3160 var curve = curvePartialByID(test, frags[0], frags[6], frags[8]);
3161 drawCurve(curve);
3162 break;
3163 default:
3164 console.log("unknown REC_TYPE_SORT frag type: " + fragType);
3165 throw "stop execution";
3166 }
3167 break;
caryclark03b03ca2015-04-23 09:13:37 -07003168 case REC_TYPE_TOP:
3169 if (!draw_top || (step_limit > 0 && tIndex < lastTop)) {
3170 continue;
3171 }
3172 ctx.lineWidth = 3;
3173 ctx.strokeStyle = "rgba(127,127,0, 0.5)";
3174 focus_enabled = true;
3175 {
3176 var curve = curvePartialByID(test, frags[0], frags[1], frags[2]);
3177 drawCurve(curve);
3178 var type = PATH_LINE + (curve.length / 2 - 2);
3179 var mid = pointAtT(curve, type, 0.5);
3180 var d = dxy_at_t(curve, type, 0.5);
3181 drawArrow(mid.x, mid.y, d.x, d.y, 0.3);
3182 }
3183 break;
caryclarkdac1d172014-06-17 05:15:38 -07003184 case REC_TYPE_MARK:
3185 if (!draw_mark || (step_limit > 0 && tIndex < lastMark)) {
3186 continue;
3187 }
3188 ctx.lineWidth = 3;
3189 ctx.strokeStyle = fragType >= MARK_DONE_LINE ?
3190 "rgba(127,0,127, 0.5)" : "rgba(127,127,0, 0.5)";
3191 focus_enabled = true;
3192 switch (fragType) {
3193 case MARK_LINE:
3194 case MARK_DONE_LINE:
3195 case MARK_UNSORTABLE_LINE:
3196 case MARK_SIMPLE_LINE:
3197 case MARK_SIMPLE_DONE_LINE:
3198 case MARK_DONE_UNARY_LINE:
3199 drawLinePartial(frags[1], frags[2], frags[3], frags[4],
3200 frags[5], frags[9]);
3201 if (draw_id) {
3202 drawLinePartialID(frags[0], frags[1], frags[2], frags[3], frags[4],
3203 frags[5], frags[9]);
3204 }
3205 break;
3206 case MARK_QUAD:
3207 case MARK_DONE_QUAD:
3208 case MARK_UNSORTABLE_QUAD:
3209 case MARK_SIMPLE_QUAD:
3210 case MARK_SIMPLE_DONE_QUAD:
3211 case MARK_DONE_UNARY_QUAD:
3212 drawQuadPartial(frags[1], frags[2], frags[3], frags[4],
3213 frags[5], frags[6], frags[7], frags[11]);
3214 if (draw_id) {
3215 drawQuadPartialID(frags[0], frags[1], frags[2], frags[3], frags[4],
3216 frags[5], frags[6], frags[7], frags[11]);
3217 }
3218 break;
3219 case MARK_CUBIC:
3220 case MARK_DONE_CUBIC:
3221 case MARK_UNSORTABLE_CUBIC:
3222 case MARK_SIMPLE_CUBIC:
3223 case MARK_SIMPLE_DONE_CUBIC:
3224 case MARK_DONE_UNARY_CUBIC:
3225 drawCubicPartial(frags[1], frags[2], frags[3], frags[4],
3226 frags[5], frags[6], frags[7], frags[8], frags[9], frags[13]);
3227 if (draw_id) {
3228 drawCubicPartialID(frags[0], frags[1], frags[2], frags[3], frags[4],
3229 frags[5], frags[6], frags[7], frags[8], frags[9], frags[13]);
3230 }
3231 break;
3232 case MARK_ANGLE_LAST:
3233 // FIXME: ignored for now
3234 break;
3235 default:
3236 console.log("unknown REC_TYPE_MARK frag type: " + fragType);
3237 throw "stop execution";
3238 }
3239 break;
3240 default:
3241 continue;
3242 }
3243 }
3244 switch (recType) {
3245 case REC_TYPE_SORT:
3246 if (!draw_sort || (step_limit > 0 && tIndex < lastSort)) {
3247 break;
3248 }
3249 var angles = []; // use tangent lines to describe arcs
3250 var windFrom = [];
3251 var windTo = [];
3252 var opp = [];
3253 var minXY = Number.MAX_VALUE;
3254 var partial;
3255 focus_enabled = true;
3256 var someUnsortable = false;
3257 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
3258 var fragType = records[recordIndex];
3259 var frags = records[recordIndex + 1];
3260 var unsortable = (fragType == SORT_UNARY && frags[14]) ||
3261 (fragType == SORT_BINARY && frags[16]);
3262 someUnsortable |= unsortable;
3263 switch (fragType) {
3264 case SORT_UNARY:
3265 case SORT_BINARY:
3266 partial = curvePartialByID(test, frags[0], frags[6], frags[8]);
3267 break;
3268 default:
3269 console.log("unknown REC_TYPE_SORT frag type: " + fragType);
3270 throw "stop execution";
3271 }
3272 var dx = boundsWidth(partial);
3273 var dy = boundsHeight(partial);
3274 minXY = Math.min(minXY, dx * dx + dy * dy);
3275 if (collect_bounds) {
3276 continue;
3277 }
3278 angles.push(tangent(partial));
3279 var from = frags[12];
3280 var to = frags[12];
3281 var sgn = frags[10];
3282 if (sgn < 0) {
3283 from -= frags[11];
3284 } else if (sgn > 0) {
3285 to -= frags[11];
3286 }
3287 windFrom.push(from + (unsortable ? "!" : ""));
3288 windTo.push(to + (unsortable ? "!" : ""));
3289 opp.push(fragType == SORT_BINARY);
3290 if (draw_sort == 1) {
3291 drawOrder(partial, frags[12]);
3292 } else {
3293 drawOrder(partial, (recordIndex / 2) + 1);
3294 }
3295 }
3296 var radius = Math.sqrt(minXY) / 2 * scale;
3297 radius = Math.min(50, radius);
3298 var scaledRadius = radius / scale;
3299 var centerX = partial[0];
3300 var centerY = partial[1];
3301 if (collect_bounds) {
3302 if (focus_enabled) {
3303 focusXmin = Math.min(focusXmin, centerX - scaledRadius);
3304 focusYmin = Math.min(focusYmin, centerY - scaledRadius);
3305 focusXmax = Math.max(focusXmax, centerX + scaledRadius);
3306 focusYmax = Math.max(focusYmax, centerY + scaledRadius);
3307 }
3308 break;
3309 }
3310 break;
3311 default:
3312 break;
3313 }
3314 }
3315 if (collect_bounds) {
3316 return;
3317 }
3318 if (draw_log && logStart >= 0) {
3319 ctx.font = "normal 10px Arial";
3320 ctx.textAlign = "left";
3321 ctx.beginPath();
3322 var top = screenHeight - 20 - (logRange + 2) * 10;
3323 ctx.rect(50, top, screenWidth - 100, (logRange + 2) * 10);
3324 ctx.fillStyle = "white";
3325 ctx.fill();
3326 ctx.fillStyle = "rgba(0,0,0, 0.5)";
3327 if (logStart > 0) {
3328 ctx.fillText(lines[logStart - 1], 50, top + 8);
3329 }
3330 ctx.fillStyle = "black";
3331 for (var idx = 0; idx < logRange; ++idx) {
3332 ctx.fillText(lines[logStart + idx], 50, top + 18 + 10 * idx);
3333 }
3334 ctx.fillStyle = "rgba(0,0,0, 0.5)";
3335 if (logStart + logRange < lines.length) {
3336 ctx.fillText(lines[logStart + logRange], 50, top + 18 + 10 * logRange);
3337 }
3338 }
3339 if (draw_legend) {
3340 var pos = 0;
caryclark624637c2015-05-11 07:21:27 -07003341 var drawSomething = draw_add | draw_active | draw_angle | draw_coincidence | draw_sort | draw_mark;
caryclarkdac1d172014-06-17 05:15:38 -07003342 // drawBox(pos++, "yellow", "black", opLetter, true, '');
3343 drawBox(pos++, "rgba(0,0,255, 0.3)", "black", draw_intersection > 1 ? sectCount : sectMax2, draw_intersection, intersectionKey);
3344 drawBox(pos++, "rgba(0,0,255, 0.3)", "black", draw_add ? addCount : addMax, draw_add, addKey);
3345 drawBox(pos++, "rgba(0,0,255, 0.3)", "black", draw_active ? activeCount : activeMax, draw_active, activeKey);
3346 drawBox(pos++, "rgba(127,127,0, 0.3)", "black", draw_angle ? angleCount : angleMax, draw_angle, angleKey);
caryclark624637c2015-05-11 07:21:27 -07003347 drawBox(pos++, "rgba(127,127,0, 0.3)", "black", draw_coincidence ? coinCount : coinMax, draw_coincidence, coincidenceKey);
caryclarkdac1d172014-06-17 05:15:38 -07003348 drawBox(pos++, "rgba(127,127,0, 0.3)", "black", draw_op ? opCount : opMax, draw_op, opKey);
3349 drawBox(pos++, "rgba(127,127,0, 0.3)", "black", draw_sort ? sortCount : sortMax, draw_sort, sortKey);
caryclark03b03ca2015-04-23 09:13:37 -07003350 drawBox(pos++, "rgba(127,127,0, 0.3)", "black", draw_top ? topCount : topMax, draw_top, topKey);
caryclarkdac1d172014-06-17 05:15:38 -07003351 drawBox(pos++, "rgba(127,0,127, 0.3)", "black", draw_mark ? markCount : markMax, draw_mark, markKey);
caryclark55888e42016-07-18 10:01:36 -07003352 drawBox(pos++, "black", "white",
caryclark26ad22a2015-10-16 09:03:38 -07003353 (new Array('P', 'P1', 'P2', 'P', 'p', 'p1', 'p2'))[draw_path], draw_path != 0, pathKey);
caryclarkdac1d172014-06-17 05:15:38 -07003354 drawBox(pos++, "rgba(0,63,0, 0.7)", "white",
3355 (new Array('Q', 'Q', 'C', 'QC', 'Qc', 'Cq'))[draw_computed],
3356 draw_computed != 0, computedKey);
3357 drawBox(pos++, "green", "black", step_limit, drawSomething, '');
3358 drawBox(pos++, "green", "black", stepMax, drawSomething, '');
3359 drawBox(pos++, "rgba(255,0,0, 0.6)", "black", lastIndex, drawSomething & draw_log, '');
3360 drawBox(pos++, "rgba(255,0,0, 0.6)", "black", test.length - 1, drawSomething & draw_log, '');
3361 if (curve_t) {
3362 drawCurveTControl();
3363 }
3364 ctx.font = "normal 20px Arial";
3365 ctx.fillStyle = "rgba(0,0,0, 0.3)";
3366 ctx.textAlign = "right";
3367 ctx.fillText(scale.toFixed(decimal_places) + 'x' , screenWidth - 10, screenHeight - 5);
3368 }
3369 if (draw_hints) {
3370 ctx.font = "normal 10px Arial";
3371 ctx.fillStyle = "rgba(0,0,0, 0.5)";
3372 ctx.textAlign = "right";
3373 var y = 4;
3374 ctx.fillText("control lines : " + controlLinesKey, ctx.screenWidthwidth - 10, pos * 50 + y++ * 10);
3375 ctx.fillText("curve t : " + curveTKey, screenWidth - 10, pos * 50 + y++ * 10);
3376 ctx.fillText("deriviatives : " + deriviativesKey, screenWidth - 10, pos * 50 + y++ * 10);
3377 ctx.fillText("intersect t : " + intersectTKey, screenWidth - 10, pos * 50 + y++ * 10);
caryclarkdac1d172014-06-17 05:15:38 -07003378 ctx.fillText("log : " + logKey, screenWidth - 10, pos * 50 + y++ * 10);
3379 ctx.fillText("log curve : " + logCurvesKey, screenWidth - 10, pos * 50 + y++ * 10);
3380 ctx.fillText("mid point : " + midpointKey, screenWidth - 10, pos * 50 + y++ * 10);
3381 ctx.fillText("points : " + ptsKey, screenWidth - 10, pos * 50 + y++ * 10);
3382 ctx.fillText("sequence : " + sequenceKey, screenWidth - 10, pos * 50 + y++ * 10);
3383 ctx.fillText("xy : " + xyKey, screenWidth - 10, pos * 50 + y++ * 10);
3384 }
3385}
3386
3387function drawBox(y, backC, foreC, str, enable, label) {
3388 ctx.beginPath();
3389 ctx.fillStyle = backC;
3390 ctx.rect(screenWidth - 40, y * 50 + 10, 40, 30);
3391 ctx.fill();
3392 ctx.font = "normal 16px Arial";
3393 ctx.fillStyle = foreC;
3394 ctx.textAlign = "center";
3395 ctx.fillText(str, screenWidth - 20, y * 50 + 32);
3396 if (!enable) {
3397 ctx.fillStyle = "rgba(255,255,255, 0.5)";
3398 ctx.fill();
3399 }
3400 if (label != '') {
3401 ctx.font = "normal 9px Arial";
3402 ctx.fillStyle = "black";
3403 ctx.fillText(label, screenWidth - 47, y * 50 + 40);
3404 }
3405}
3406
3407function drawCurveTControl() {
3408 ctx.lineWidth = 2;
3409 ctx.strokeStyle = "rgba(0,0,0, 0.3)";
3410 ctx.beginPath();
3411 ctx.rect(screenWidth - 80, 40, 28, screenHeight - 80);
3412 ctx.stroke();
3413 var ty = 40 + curveT * (screenHeight - 80);
3414 ctx.beginPath();
3415 ctx.moveTo(screenWidth - 80, ty);
3416 ctx.lineTo(screenWidth - 85, ty - 5);
3417 ctx.lineTo(screenWidth - 85, ty + 5);
3418 ctx.lineTo(screenWidth - 80, ty);
3419 ctx.fillStyle = "rgba(0,0,0, 0.6)";
3420 ctx.fill();
3421 var num = curveT.toFixed(decimal_places);
3422 ctx.font = "normal 10px Arial";
3423 ctx.textAlign = "left";
3424 ctx.fillText(num, screenWidth - 78, ty);
3425}
3426
3427function ptInTControl() {
3428 var e = window.event;
caryclark55888e42016-07-18 10:01:36 -07003429 var tgt = e.target || e.srcElement;
caryclarkdac1d172014-06-17 05:15:38 -07003430 var left = tgt.offsetLeft;
3431 var top = tgt.offsetTop;
3432 var x = (e.clientX - left);
3433 var y = (e.clientY - top);
3434 if (x < screenWidth - 80 || x > screenWidth - 50) {
3435 return false;
3436 }
3437 if (y < 40 || y > screenHeight - 80) {
3438 return false;
3439 }
3440 curveT = (y - 40) / (screenHeight - 120);
3441 if (curveT < 0 || curveT > 1) {
3442 throw "stop execution";
3443 }
3444 return true;
3445}
3446
3447function drawTop() {
3448 if (tests[testIndex] == null) {
3449 var str = testDivs[testIndex].textContent;
3450 parse_all(str);
3451 var title = testDivs[testIndex].id.toString();
3452 testTitles[testIndex] = title;
3453 }
3454 init(tests[testIndex]);
3455 redraw();
3456}
3457
3458function redraw() {
3459 if (focus_on_selection) {
3460 collect_bounds = true;
3461 draw(tests[testIndex], testLines[testIndex], testTitles[testIndex]);
3462 collect_bounds = false;
3463 if (focusXmin < focusXmax && focusYmin < focusYmax) {
3464 setScale(focusXmin, focusXmax, focusYmin, focusYmax);
3465 }
3466 }
3467 ctx.beginPath();
3468 ctx.fillStyle = "white";
3469 ctx.rect(0, 0, screenWidth, screenHeight);
3470 ctx.fill();
3471 draw(tests[testIndex], testLines[testIndex], testTitles[testIndex]);
3472}
3473
3474function dumpCurvePartial(test, id, t0, t1) {
3475 var curve = curveByID(test, id);
3476 var name = ["line", "quad", "cubic"][curve.length / 2 - 2];
3477 console.log("id=" + id + " " + name + "=" + curveToString(curve)
3478 + " t0=" + t0 + " t1=" + t1
3479 + " partial=" + curveToString(curvePartialByID(test, id, t0, t1)));
3480}
3481
3482function dumpAngleTest(test, id, t0, t1) {
3483 var curve = curveByID(test, id);
caryclark55888e42016-07-18 10:01:36 -07003484 console.log(" { {" + curveToString(curve) + "}, "
caryclarkdac1d172014-06-17 05:15:38 -07003485 + curve.length / 2 + ", " + t0 + ", " + t1 + ", {} }, //");
3486}
3487
3488function dumpLogToConsole() {
3489 if (logStart < 0) {
3490 return;
3491 }
3492 var test = tests[testIndex];
3493 var recType = REC_TYPE_UNKNOWN;
3494 var records;
3495 for (var index = 0; index < test.length; index += 3) {
3496 var lastLineNo = test[index + 1];
3497 if (lastLineNo >= logStart && lastLineNo < logStart + logRange) {
3498 recType = test[index];
3499 records = test[index + 2];
3500 break;
3501 }
3502 }
3503 if (recType == REC_TYPE_UNKNOWN) {
3504 return;
3505 }
3506 var lines = testLines[testIndex];
3507 for (var idx = 0; idx < logRange; ++idx) {
3508 var line = lines[logStart + idx];
3509 console.log(line);
3510 for (var recordIndex = 0; recordIndex < records.length; recordIndex += 2) {
3511 var fragType = records[recordIndex];
3512 var frags = records[recordIndex + 1];
3513 if (recType == REC_TYPE_ANGLE && fragType == ANGLE_AFTER) {
caryclarkdac1d172014-06-17 05:15:38 -07003514 dumpCurvePartial(test, frags[0], frags[4], frags[5]);
3515 dumpCurvePartial(test, frags[6], frags[10], frags[11]);
3516 dumpCurvePartial(test, frags[12], frags[16], frags[17]);
3517 console.log("\nstatic IntersectData intersectDataSet[] = { //");
3518 dumpAngleTest(test, frags[0], frags[4], frags[5]);
3519 dumpAngleTest(test, frags[6], frags[10], frags[11]);
3520 dumpAngleTest(test, frags[12], frags[16], frags[17]);
3521 console.log("}; //");
3522 }
3523 }
3524 }
3525}
3526
3527var activeKey = 'a';
3528var pathKey = 'b';
3529var pathBackKey = 'B';
3530var centerKey = 'c';
caryclark624637c2015-05-11 07:21:27 -07003531var coincidenceKey = 'C';
caryclarkdac1d172014-06-17 05:15:38 -07003532var addKey = 'd';
3533var deriviativesKey = 'f';
3534var angleKey = 'g';
3535var angleBackKey = 'G';
caryclarkdac1d172014-06-17 05:15:38 -07003536var intersectionKey = 'i';
3537var intersectionBackKey = 'I';
3538var sequenceKey = 'j';
3539var midpointKey = 'k';
3540var logKey = 'l';
3541var logToConsoleKey = 'L';
3542var markKey = 'm';
3543var sortKey = 'o';
3544var opKey = 'p';
3545var opBackKey = 'P';
3546var computedKey = 'q';
3547var computedBackKey = 'Q';
3548var stepKey = 's';
3549var stepBackKey = 'S';
3550var intersectTKey = 't';
caryclark03b03ca2015-04-23 09:13:37 -07003551var topKey = 'T';
caryclarkdac1d172014-06-17 05:15:38 -07003552var curveTKey = 'u';
3553var controlLinesBackKey = 'V';
3554var controlLinesKey = 'v';
3555var ptsKey = 'x';
3556var xyKey = 'y';
3557var logCurvesKey = 'z';
3558var focusKey = '`';
3559var idKey = '.';
3560var retinaKey = '\\';
3561
3562function doKeyPress(evt) {
3563 var char = String.fromCharCode(evt.charCode);
3564 var focusWasOn = false;
3565 switch (char) {
3566 case '0':
3567 case '1':
3568 case '2':
3569 case '3':
3570 case '4':
3571 case '5':
3572 case '6':
3573 case '7':
3574 case '8':
3575 case '9':
3576 decimal_places = char - '0';
3577 redraw();
3578 break;
3579 case activeKey:
3580 draw_active ^= true;
caryclark55888e42016-07-18 10:01:36 -07003581 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003582 break;
3583 case addKey:
3584 draw_add ^= true;
caryclark55888e42016-07-18 10:01:36 -07003585 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003586 break;
3587 case angleKey:
caryclark54359292015-03-26 07:52:43 -07003588 draw_angle = (draw_angle + 1) % 4;
caryclarkdac1d172014-06-17 05:15:38 -07003589 redraw();
3590 break;
3591 case angleBackKey:
3592 draw_angle = (draw_angle + 2) % 3;
3593 redraw();
3594 break;
3595 case centerKey:
3596 setScale(xmin, xmax, ymin, ymax);
caryclark55888e42016-07-18 10:01:36 -07003597 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003598 break;
caryclark624637c2015-05-11 07:21:27 -07003599 case coincidenceKey:
3600 draw_coincidence ^= true;
3601 redraw();
3602 break;
caryclarkdac1d172014-06-17 05:15:38 -07003603 case controlLinesBackKey:
3604 control_lines = (control_lines + 3) % 4;
caryclark55888e42016-07-18 10:01:36 -07003605 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003606 break;
3607 case controlLinesKey:
3608 control_lines = (control_lines + 1) % 4;
caryclark55888e42016-07-18 10:01:36 -07003609 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003610 break;
3611 case computedBackKey:
3612 draw_computed = (draw_computed + 5) % 6;
caryclark55888e42016-07-18 10:01:36 -07003613 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003614 break;
3615 case computedKey:
3616 draw_computed = (draw_computed + 1) % 6;
caryclark55888e42016-07-18 10:01:36 -07003617 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003618 break;
3619 case curveTKey:
3620 curve_t ^= true;
3621 if (curve_t) {
3622 draw_legend = true;
3623 }
3624 redraw();
3625 break;
3626 case deriviativesKey:
3627 draw_deriviatives = (draw_deriviatives + 1) % 3;
3628 redraw();
3629 break;
3630 case focusKey:
3631 focus_on_selection ^= true;
3632 setScale(xmin, xmax, ymin, ymax);
3633 redraw();
3634 break;
caryclarkdac1d172014-06-17 05:15:38 -07003635 case idKey:
3636 draw_id ^= true;
3637 redraw();
3638 break;
3639 case intersectionBackKey:
3640 draw_intersection = (draw_intersection + 3) % 4;
caryclark55888e42016-07-18 10:01:36 -07003641 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003642 break;
3643 case intersectionKey:
3644 draw_intersection = (draw_intersection + 1) % 4;
caryclark55888e42016-07-18 10:01:36 -07003645 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003646 break;
3647 case intersectTKey:
3648 draw_intersectT ^= true;
3649 redraw();
3650 break;
3651 case logCurvesKey:
3652 logCurves(tests[testIndex]);
3653 break;
3654 case logKey:
3655 draw_log ^= true;
3656 redraw();
3657 break;
3658 case logToConsoleKey:
3659 if (draw_log) {
3660 dumpLogToConsole();
3661 }
3662 break;
3663 case markKey:
3664 draw_mark ^= true;
3665 redraw();
3666 break;
3667 case midpointKey:
3668 draw_midpoint ^= true;
3669 redraw();
3670 break;
3671 case opKey:
3672 draw_op = (draw_op + 1) % 3;
3673 redraw();
3674 break;
3675 case opBackKey:
3676 draw_op = (draw_op + 2) % 3;
3677 redraw();
3678 break;
3679 case pathKey:
caryclark26ad22a2015-10-16 09:03:38 -07003680 draw_path = (draw_path + 1) % (4 + (hasAlignedPath ? 3 : 0));
caryclark55888e42016-07-18 10:01:36 -07003681 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003682 break;
3683 case pathBackKey:
caryclark26ad22a2015-10-16 09:03:38 -07003684 draw_path = (draw_path + 3 + (hasAlignedPath ? 3 : 0)) % (4 + (hasAlignedPath ? 3 : 0));
caryclark55888e42016-07-18 10:01:36 -07003685 redraw();
caryclarkdac1d172014-06-17 05:15:38 -07003686 break;
3687 case ptsKey:
3688 pt_labels = (pt_labels + 1) % 3;
3689 redraw();
3690 break;
3691 case retinaKey:
3692 retina_scale ^= true;
3693 drawTop();
3694 break;
3695 case sequenceKey:
3696 draw_sequence ^= true;
3697 redraw();
3698 break;
3699 case sortKey:
3700 draw_sort = (draw_sort + 1) % 3;
3701 drawTop();
3702 break;
3703 case stepKey:
3704 step_limit++;
3705 if (step_limit > stepMax) {
3706 step_limit = stepMax;
3707 }
3708 redraw();
3709 break;
3710 case stepBackKey:
3711 step_limit--;
3712 if (step_limit < 0) {
3713 step_limit = 0;
3714 }
3715 redraw();
3716 break;
caryclark03b03ca2015-04-23 09:13:37 -07003717 case topKey:
3718 draw_top ^= true;
3719 redraw();
3720 break;
caryclarkdac1d172014-06-17 05:15:38 -07003721 case xyKey:
3722 debug_xy = (debug_xy + 1) % 3;
3723 redraw();
3724 break;
3725 case '-':
3726 focusWasOn = focus_on_selection;
3727 if (focusWasOn) {
3728 focus_on_selection = false;
3729 scale /= 1.2;
3730 } else {
3731 scale /= 2;
3732 calcLeftTop();
3733 }
3734 redraw();
3735 focus_on_selection = focusWasOn;
3736 break;
3737 case '=':
3738 case '+':
3739 focusWasOn = focus_on_selection;
3740 if (focusWasOn) {
3741 focus_on_selection = false;
3742 scale *= 1.2;
3743 } else {
3744 scale *= 2;
3745 calcLeftTop();
3746 }
3747 redraw();
3748 focus_on_selection = focusWasOn;
3749 break;
3750 case '?':
3751 draw_hints ^= true;
3752 if (draw_hints && !draw_legend) {
3753 draw_legend = true;
3754 }
3755 redraw();
3756 break;
3757 case '/':
3758 draw_legend ^= true;
3759 redraw();
3760 break;
3761 }
3762}
3763
3764function doKeyDown(evt) {
3765 var char = evt.keyCode;
3766 var preventDefault = false;
3767 switch (char) {
3768 case 37: // left arrow
3769 if (evt.shiftKey) {
3770 testIndex -= 9;
3771 }
3772 if (--testIndex < 0)
3773 testIndex = tests.length - 1;
3774 drawTop();
3775 preventDefault = true;
3776 break;
3777 case 39: // right arrow
3778 if (evt.shiftKey) {
3779 testIndex += 9;
3780 }
3781 if (++testIndex >= tests.length)
3782 testIndex = 0;
3783 drawTop();
3784 preventDefault = true;
3785 break;
3786 }
3787 if (preventDefault) {
3788 evt.preventDefault();
3789 return false;
3790 }
3791 return true;
3792}
3793
3794(function() {
3795 var hidden = "hidden";
3796
3797 // Standards:
3798 if (hidden in document)
3799 document.addEventListener("visibilitychange", onchange);
3800 else if ((hidden = "mozHidden") in document)
3801 document.addEventListener("mozvisibilitychange", onchange);
3802 else if ((hidden = "webkitHidden") in document)
3803 document.addEventListener("webkitvisibilitychange", onchange);
3804 else if ((hidden = "msHidden") in document)
3805 document.addEventListener("msvisibilitychange", onchange);
3806 // IE 9 and lower:
3807 else if ('onfocusin' in document)
3808 document.onfocusin = document.onfocusout = onchange;
3809 // All others:
3810 else
caryclark55888e42016-07-18 10:01:36 -07003811 window.onpageshow = window.onpagehide
caryclarkdac1d172014-06-17 05:15:38 -07003812 = window.onfocus = window.onblur = onchange;
3813
3814 function onchange (evt) {
3815 var v = 'visible', h = 'hidden',
caryclark55888e42016-07-18 10:01:36 -07003816 evtMap = {
3817 focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
caryclarkdac1d172014-06-17 05:15:38 -07003818 };
3819
3820 evt = evt || window.event;
3821 if (evt.type in evtMap)
3822 document.body.className = evtMap[evt.type];
caryclark55888e42016-07-18 10:01:36 -07003823 else
caryclarkdac1d172014-06-17 05:15:38 -07003824 document.body.className = this[hidden] ? "hidden" : "visible";
3825 }
3826})();
3827
3828function calcXY() {
3829 var e = window.event;
caryclark55888e42016-07-18 10:01:36 -07003830 var tgt = e.target || e.srcElement;
caryclarkdac1d172014-06-17 05:15:38 -07003831 var left = tgt.offsetLeft;
3832 var top = tgt.offsetTop;
3833 mouseX = (e.clientX - left) / scale + srcLeft;
3834 mouseY = (e.clientY - top) / scale + srcTop;
3835}
3836
3837function calcLeftTop() {
3838 srcLeft = mouseX - screenWidth / 2 / scale;
3839 srcTop = mouseY - screenHeight / 2 / scale;
3840}
3841
3842var disableClick = false;
3843
3844function handleMouseClick() {
3845 if (disableClick) {
3846 return;
3847 }
3848 if (!curve_t || !ptInTControl()) {
3849 calcXY();
3850 calcLeftTop();
3851 }
3852 redraw();
3853// if (!curve_t || !ptInTControl()) {
3854// mouseX = screenWidth / 2 / scale + srcLeft;
3855// mouseY = screenHeight / 2 / scale + srcTop;
3856// }
3857}
3858
3859function handleMouseOver() {
3860 calcXY();
3861 if (debug_xy != 2) {
3862 return;
3863 }
3864 var num = mouseX.toFixed(decimal_places) + ", " + mouseY.toFixed(decimal_places);
3865 ctx.beginPath();
3866 ctx.rect(300,100,num.length * 6,10);
3867 ctx.fillStyle="white";
3868 ctx.fill();
3869 ctx.font = "normal 10px Arial";
3870 ctx.fillStyle="black";
3871 ctx.textAlign = "left";
3872 ctx.fillText(num, 300, 108);
3873}
3874
3875function start() {
3876 for (var i = 0; i < testDivs.length; ++i) {
3877 tests[i] = null;
3878 }
3879 testIndex = 0;
3880 drawTop();
3881 window.addEventListener('keypress', doKeyPress, true);
3882 window.addEventListener('keydown', doKeyDown, true);
3883 window.onresize = function() {
3884 drawTop();
3885 }
3886 /*
3887 window.onpagehide = function() {
3888 disableClick = true;
3889 }
3890 */
3891 window.onpageshow = function () {
3892 disableClick = false;
3893 }
3894}
3895
3896</script>
3897</head>
3898
3899<body onLoad="start();">
3900<canvas id="canvas" width="750" height="500"
3901 onmousemove="handleMouseOver()"
3902 onclick="handleMouseClick()"
3903 ></canvas >
3904</body>
3905</html>