blob: e0784bdbbf5b04962edd0775660edddb3daa881c [file] [log] [blame]
caryclark@google.coma7e483d2012-08-28 20:44:43 +00001<!-- bezier clip visualizer -->
2<html>
3<head>
4<div style="height:0">
5
6<div id="clip1">
7(gdb) p smaller
8$2 = {{
9 x = 0.91292418204644155,
10 y = 0.41931201426549197
11 }, {
12 x = 0.70491388044579517,
13 y = 0.64754305977710236
14 }, {
15 x = 0,
16 y = 1
17 }}
18(gdb) p larger
19$3 = {{
20 x = 0.21875,
21 y = 0.765625
22 }, {
23 x = 0.125,
24 y = 0.875
25 }, {
26 x = 0,
27 y = 1
28 }}
29(gdb) p distance2y
30$1 = {{
31 x = 0,
32 y = 0.080355482722450078
33 }, {
34 x = 0.5,
35 y = 0.038383741101172597
36 }, {
37 x = 1,
38 y = 0
39 }}
40</div>
41
caryclark@google.com235f56a2012-09-14 14:19:30 +000042<div id="quad21a">
43bezier_clip q1=(0,0 1,0 0,2) q2=(0.5,0.25 0.5,0.5 0,1) minT=0 maxT=1
44</div>
45<div id="quad21b">
46bezier_clip q1=(0.5,0.25 0.5,0.375 0.375,0.5625) q2=(0,0 1,0 0,2) minT=0.3 maxT=0.78125
47</div>
48<div id="quad21c">
49bezier_clip q1=(0.42,0.18 0.6125,0.46875 0.341796875,1.22070312) q2=(0.5,0.25 0.5,0.375 0.375,0.5625) minT=0 maxT=0.926710098
50</div>
51<div id="quad21d">
52bezier_clip q1=(0.5,0.25 0.5,0.307919381 0.473162762,0.379257381) q2=(0.42,0.18 0.6125,0.46875 0.341796875,1.22070312) minT=0.187231244 maxT=0.729263299
53</div>
54<div id="quad21e">
55bezier_clip q1=(0.475846194,0.304363878 0.53317904,0.507883959 0.454423387,0.847492538) q2=(0.5,0.25 0.5,0.307919381 0.473162762,0.379257381) minT=0 maxT=1
56</div>
57<div id="quad21f">
58bezier_clip q1=(0.493290691,0.311274036 0.486581381,0.343588381 0.473162762,0.379257381) q2=(0.475846194,0.304363878 0.53317904,0.507883959 0.454423387,0.847492538) minT=0.0828748517 maxT=0.150086861
59</div>
60
61<div id="quad21g">
62(gdb) p smaller
63$1 = {{
64 x = 0.48441440743366754,
65 y = 0.33903196011243797
66 }, {
67 x = 0.48750982503868118,
68 y = 0.35346899178071778
69 }, {
70 x = 0.48999046908865357,
71 y = 0.368520797004039
72 }}
73(gdb) p larger
74$2 = {{
75 x = 0.49329069058425024,
76 y = 0.31127403581536672
77 }, {
78 x = 0.48658138116850047,
79 y = 0.34358838107698753
80 }, {
81 x = 0.47316276233700094,
82 y = 0.37925738104648321
83 }}
84</div>
85
caryclark@google.comd1688742012-09-18 20:08:37 +000086<div id="quad36">
87(gdb) p fQ
88$2 = {{
89 x = 1.8883839294261275,
90 y = 2.1108590606904345
91 }, {
92 x = 1.888463903363252,
93 y = 2.1111576060205435
94 }, {
95 x = 1.8885438199983176,
96 y = 2.1114561800016824
97 }}
98(gdb) p rh.fQ
99$3 = {{
100 x = 1.8883839294260976,
101 y = 2.1108590606903377
102 }, {
103 x = 1.8886366953645748,
104 y = 2.1109850143489544
105 }, {
106 x = 1.8888888888888888,
107 y = 2.1111111111111112
108 }}
109(gdb)
110</div>
111
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000112<div id="quad37">
113 {{x = 360.048828125, y = 229.2578125}, {x = 360.048828125, y = 224.4140625}, {x = 362.607421875, y = 221.3671875}}
114 {{x = 362.607421875, y = 221.3671875}, {x = 365.166015625, y = 218.3203125}, {x = 369.228515625, y = 218.3203125}}
115</div>
116
117
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000118</div>
119
120<script type="text/javascript">
121
122var testDivs = [
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000123 quad37,
caryclark@google.comd1688742012-09-18 20:08:37 +0000124 quad36,
caryclark@google.com235f56a2012-09-14 14:19:30 +0000125 quad21g,
126 quad21a,
127 quad21b,
128 quad21c,
129 quad21d,
130 quad21e,
131 quad21f,
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000132 clip1,
133];
134
135var scale, columns, rows, xStart, yStart;
136
caryclark@google.comd1688742012-09-18 20:08:37 +0000137var ticks = 10;
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000138var at_x = 13 + 0.5;
139var at_y = 13 + 0.5;
caryclark@google.comd1688742012-09-18 20:08:37 +0000140var init_decimal_places = 1; // make this 3 to show more precision
141var decimal_places;
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000142var tests = [];
143var testTitles = [];
144var testIndex = 0;
145var ctx;
146var fat1 = true;
147var fat2 = false;
caryclark@google.comd1688742012-09-18 20:08:37 +0000148var ctl1 = true;
149var ctl2 = false;
150var ctlPts1 = true;
151var ctlPts2 = false;
152var minScale = 1;
153var subscale = 1;
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000154
155function parse(test, title) {
156 var curveStrs = test.split("{{");
caryclark@google.com235f56a2012-09-14 14:19:30 +0000157 if (curveStrs.length == 1)
158 curveStrs = test.split("=(");
159 var pattern = /[a-z$=]?-?\d+\.*\d*/g;
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000160 var curves = [];
161 for (var c in curveStrs) {
162 var curveStr = curveStrs[c];
163 var points = curveStr.match(pattern);
164 var pts = [];
165 for (var wd in points) {
166 var num = parseFloat(points[wd]);
167 if (isNaN(num)) continue;
168 pts.push(num);
169 }
170 if (pts.length > 0)
171 curves.push(pts);
172 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000173 if (curves.length >= 2) {
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000174 tests.push(curves);
175 testTitles.push(title);
176 }
177}
178
179function init(test) {
180 var canvas = document.getElementById('canvas');
181 if (!canvas.getContext) return;
182 canvas.width = window.innerWidth - at_x;
183 canvas.height = window.innerHeight - at_y;
184 ctx = canvas.getContext('2d');
185 var xmin = Infinity;
186 var xmax = -Infinity;
187 var ymin = Infinity;
188 var ymax = -Infinity;
189 for (var curves in test) {
190 var curve = test[curves];
191 var last = curve.length;
192 for (var idx = 0; idx < last; idx += 2) {
193 xmin = Math.min(xmin, curve[idx]);
194 xmax = Math.max(xmax, curve[idx]);
195 ymin = Math.min(ymin, curve[idx + 1]);
196 ymax = Math.max(ymax, curve[idx + 1]);
197 }
198 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000199 subscale = 1;
200 decimal_places = init_decimal_places;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000201 if (xmax != xmin && ymax != ymin) {
202 while ((xmax - xmin) * subscale < 0.1 && (ymax - ymin) * subscale < 0.1) {
203 subscale *= 10;
caryclark@google.comd1688742012-09-18 20:08:37 +0000204 decimal_places += 1;
205 // if (subscale > 100000) {
206 // break;
207 // }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000208 }
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000209 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000210 columns = Math.ceil(xmax * subscale) - Math.floor(xmin * subscale) + 1;
211 rows = Math.ceil(ymax * subscale) - Math.floor(ymin * subscale) + 1;
212
213 xStart = Math.floor(xmin * subscale) / subscale;
214 yStart = Math.floor(ymin * subscale) / subscale;
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000215 var hscale = ctx.canvas.width / columns / ticks;
216 var vscale = ctx.canvas.height / rows / ticks;
caryclark@google.comd1688742012-09-18 20:08:37 +0000217 minScale = Math.floor(Math.min(hscale, vscale));
218 scale = minScale * subscale;
219 // while (columns < 1000 && rows < 1000) {
220 // columns *= 2;
221 // rows *= 2;
222 // }
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000223}
224
225function drawPoint(px, py, xoffset, yoffset, unit) {
226 var label = px.toFixed(decimal_places) + ", " + py.toFixed(decimal_places);
227 var _px = px * unit + xoffset;
228 var _py = py * unit + yoffset;
229 ctx.beginPath();
230 ctx.arc(_px, _py, 3, 0, Math.PI*2, true);
231 ctx.closePath();
232 ctx.fill();
233 ctx.fillText(label, _px + 5, _py);
234}
235
236function draw(test, title, _at_x, _at_y, scale) {
237 ctx.fillStyle = "rgba(0,0,0, 0.1)";
238 ctx.font = "normal 50px Arial";
239 ctx.fillText(title, 50, 50);
240 ctx.font = "normal 10px Arial";
241
242 var unit = scale * ticks;
243 ctx.lineWidth = 1;
244 var i;
245 for (i = 0; i <= rows * ticks; ++i) {
246 ctx.strokeStyle = (i % ticks) != 0 ? "rgb(160,160,160)" : "black";
247 ctx.beginPath();
caryclark@google.comd1688742012-09-18 20:08:37 +0000248 ctx.moveTo(_at_x + 0, _at_y + i * minScale);
249 ctx.lineTo(_at_x + unit * columns, _at_y + i * minScale);
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000250 ctx.stroke();
251 }
252 for (i = 0; i <= columns * ticks; ++i) {
253 ctx.strokeStyle = (i % ticks) != 0 ? "rgb(160,160,160)" : "black";
254 ctx.beginPath();
caryclark@google.comd1688742012-09-18 20:08:37 +0000255 ctx.moveTo(_at_x + i * minScale, _at_y + 0);
256 ctx.lineTo(_at_x + i * minScale, _at_y + unit * rows);
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000257 ctx.stroke();
258 }
259
260 var xoffset = xStart * -unit + _at_x;
261 var yoffset = yStart * -unit + _at_y;
262
263 ctx.fillStyle = "rgb(40,80,60)"
264 for (i = 0; i <= columns; i += (1 / ticks))
265 {
caryclark@google.comd1688742012-09-18 20:08:37 +0000266 num = xStart + i / subscale;
267 ctx.fillText(num.toFixed(decimal_places), xoffset + num * unit - 5, 10);
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000268 }
269 for (i = 0; i <= rows; i += (1 / ticks))
270 {
caryclark@google.comd1688742012-09-18 20:08:37 +0000271 num = yStart + i / subscale;
272 ctx.fillText(num.toFixed(decimal_places), 0, yoffset + num * unit + 0);
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000273 }
274
275 // draw curve 1 and 2
276 var curves, pts;
277 for (curves in test) {
278 var curve = test[curves];
279 ctx.beginPath();
280 ctx.moveTo(xoffset + curve[0] * unit, yoffset + curve[1] * unit);
281 switch (curve.length) {
282 case 6:
283 ctx.quadraticCurveTo(
284 xoffset + curve[2] * unit, yoffset + curve[3] * unit,
285 xoffset + curve[4] * unit, yoffset + curve[5] * unit);
286 break;
287 case 8:
288 ctx.bezierCurveTo(
289 xoffset + curve[2] * unit, yoffset + curve[3] * unit,
290 xoffset + curve[4] * unit, yoffset + curve[5] * unit,
291 xoffset + curve[6] * unit, yoffset + curve[7] * unit);
292 break;
293 }
294 if (curves == 2) ctx.strokeStyle = curves ? "red" : "blue";
295 ctx.stroke();
296 ctx.strokeStyle = "rgba(0,0,0, 0.3)";
297 ctx.beginPath();
298 ctx.moveTo(xoffset + curve[0] * unit, yoffset + curve[1] * unit);
299 ctx.lineTo(xoffset + curve[2] * unit, yoffset + curve[3] * unit);
300 ctx.lineTo(xoffset + curve[4] * unit, yoffset + curve[5] * unit);
301 if (curve.length == 8)
302 ctx.lineTo(xoffset + curve[6] * unit, yoffset + curve[7] * unit);
303 ctx.stroke();
304 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000305 // optionally draw fat lines for curve
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000306 if (fat1)
307 drawFat(test[0], xoffset, yoffset, unit);
308 if (fat2)
309 drawFat(test[1], xoffset, yoffset, unit);
caryclark@google.comd1688742012-09-18 20:08:37 +0000310 if (ctl1)
311 drawCtl(test[0], xoffset, yoffset, unit);
312 if (ctl2)
313 drawCtl(test[1], xoffset, yoffset, unit);
314 if (ctlPts1)
315 drawCtlPts(test[0], xoffset, yoffset, unit);
316 if (ctlPts2)
317 drawCtlPts(test[1], xoffset, yoffset, unit);
318}
319
320function drawCtl(curve, xoffset, yoffset, unit) {
321 var last = curve.length - 2;
322 ctx.strokeStyle = "rgba(0,0,0, 0.5)";
323 ctx.beginPath();
324 ctx.moveTo(xoffset + curve[0] * unit, yoffset + curve[1] * unit);
325 ctx.lineTo(xoffset + curve[2] * unit, yoffset + curve[3] * unit);
326 ctx.lineTo(xoffset + curve[4] * unit, yoffset + curve[5] * unit);
327 ctx.stroke();
328}
329
330function drawCtlPts(curve, xoffset, yoffset, unit) {
331 drawPoint(curve[0], curve[1], xoffset, yoffset, unit);
332 drawPoint(curve[2], curve[3], xoffset, yoffset, unit);
333 drawPoint(curve[4], curve[5], xoffset, yoffset, unit);
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000334}
335
336function drawFat(curve, xoffset, yoffset, unit) {
337 var last = curve.length - 2;
338 ctx.strokeStyle = "rgba(0,0,0, 0.5)";
339 ctx.beginPath();
340 ctx.moveTo(xoffset + curve[0] * unit, yoffset + curve[1] * unit);
341 ctx.lineTo(xoffset + curve[last] * unit, yoffset + curve[last + 1] * unit);
342 ctx.stroke();
343 // draw line parallel to end points through control points
344 var dx = curve[last] - curve[0];
345 var dy = curve[last + 1] - curve[1];
346 drawParallelLine(curve[2], curve[3], dx, dy, xoffset, yoffset, unit);
347 if (curve.length == 8)
348 drawParallelLine(curve[4], curve[5], dx, dy, xoffset, yoffset, unit);
349}
350
351function drawParallelLine(x, y, dx, dy, xoffset, yoffset, unit) {
352 var x1 = x - dx;
353 var y1 = y - dy;
354 var x2 = x + dx;
355 var y2 = y + dy;
356 ctx.beginPath();
357 ctx.moveTo(xoffset + x1 * unit, yoffset + y1 * unit);
358 ctx.lineTo(xoffset + x2 * unit, yoffset + y2 * unit);
359 ctx.stroke();
360}
361
362function drawTop() {
363 init(tests[testIndex]);
364 redraw();
365}
366
367function redraw() {
368 ctx.beginPath();
369 ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
370 ctx.fillStyle="white";
371 ctx.fill();
372 draw(tests[testIndex], testTitles[testIndex], at_x, at_y, scale);
373}
374
375function doKeyPress(evt) {
376 var char = String.fromCharCode(evt.charCode);
377 switch (char) {
caryclark@google.comd1688742012-09-18 20:08:37 +0000378 case 'c':
379 ctl2 ^= true;
380 if (ctl2 == false)
381 ctl1 ^= true;
382 drawTop();
383 break;
384 case 'd':
385 ctlPts2 ^= true;
386 if (ctlPts2 == false)
387 ctlPts1 ^= true;
388 drawTop();
389 break;
caryclark@google.coma7e483d2012-08-28 20:44:43 +0000390 case 'f':
391 fat2 ^= true;
392 if (fat2 == false)
393 fat1 ^= true;
394 drawTop();
395 break;
396 case 'N':
397 testIndex += 9;
398 case 'n':
399 if (++testIndex >= tests.length)
400 testIndex = 0;
401 mouseX = Infinity;
402 drawTop();
403 break;
404 case 'P':
405 testIndex -= 9;
406 case 'p':
407 if (--testIndex < 0)
408 testIndex = tests.length - 1;
409 mouseX = Infinity;
410 drawTop();
411 break;
412 }
413}
414
415function handleMouseClick() {
416}
417
418function handleMouseOver() {
419}
420
421function start() {
422 for (i = 0; i < testDivs.length; ++i) {
423 var title = testDivs[i].id.toString();
424 var str = testDivs[i].firstChild.data;
425 parse(str, title);
426 }
427 drawTop();
428 window.addEventListener('keypress', doKeyPress, true);
429 window.onresize = function() {
430 drawTop();
431 }
432}
433
434</script>
435</head>
436
437<body onLoad="start();">
438<canvas id="canvas" width="750" height="500"
439 onmousemove="handleMouseOver()"
440 onclick="handleMouseClick()"
441 ></canvas >
442</body>
443</html>