msquares.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <style type="text/css">
  2. #diagram {
  3. background:#303030;
  4. width:115px;
  5. height:5120px;
  6. padding:5px
  7. }
  8. </style>
  9. <script src="http://cdnjs.cloudflare.com/ajax/libs/bonsai/0.4/bonsai.min.js">
  10. </script>
  11. <div id="diagram">
  12. </div>
  13. <script>
  14. function main()
  15. {
  16. var colors = [
  17. '#f33',
  18. '#0d0',
  19. '#dd0',
  20. '#2af'
  21. ]
  22. var tab = [];
  23. var edgetab = [];
  24. for (var i = 0; i < 256; i++) {
  25. new Text('' + i).addTo(stage).attr({
  26. x: 24,
  27. y: 4 + i * 20,
  28. fontFamily: 'Arial, sans-serif',
  29. fontSize: '15',
  30. textFillColor: '#aaa',
  31. textStrokeWidth: 0 });
  32. var a = i & 0x3;
  33. var b = (i >> 2) & 0x3;
  34. var c = (i >> 4) & 0x3;
  35. var d = (i >> 6) & 0x3;
  36. new Text('' + d + c + b + a).addTo(stage).attr({
  37. x: 55,
  38. y: 4 + i * 20,
  39. fontFamily: 'Arial, sans-serif',
  40. fontSize: '15',
  41. textFillColor: '#e77',
  42. textStrokeWidth: 0 });
  43. var x = 0;
  44. new Rect(x + 1, 1 + i * 20, 18, 18)
  45. .addTo(stage)
  46. .attr('fillColor', '#555');
  47. var nw = new Circle(x + 4, i * 20 + 4, 2).addTo(stage);
  48. var ne = new Circle(x + 16, i * 20 + 4, 2).addTo(stage);
  49. var sw = new Circle(x + 4, i * 20 + 16, 2).addTo(stage);
  50. var se = new Circle(x + 16, i * 20 + 16, 2).addTo(stage);
  51. sw.fill(colors[a]);
  52. se.fill(colors[b]);
  53. ne.fill(colors[c]);
  54. nw.fill(colors[d]);
  55. var x = 95;
  56. new Rect(x + 1, 1 + i * 20, 18, 18)
  57. .addTo(stage)
  58. .attr('fillColor', '#555');
  59. var nw = new Circle(x + 4, i * 20 + 4, 2).addTo(stage);
  60. var ne = new Circle(x + 16, i * 20 + 4, 2).addTo(stage);
  61. var sw = new Circle(x + 4, i * 20 + 16, 2).addTo(stage);
  62. var se = new Circle(x + 16, i * 20 + 16, 2).addTo(stage);
  63. sw.fill(colors[a]);
  64. se.fill(colors[b]);
  65. ne.fill(colors[c]);
  66. nw.fill(colors[d]);
  67. var pts = [
  68. [x + 4, i * 20 + 16], // sw 0
  69. [x + 10, i * 20 + 16], // s 1
  70. [x + 16, i * 20 + 16], // se 2
  71. [x + 16, i * 20 + 10], // e 3
  72. [x + 16, i * 20 + 4], // ne 4
  73. [x + 10, i * 20 + 4], // n 5
  74. [x + 4, i * 20 + 4], // nw 6
  75. [x + 4, i * 20 + 10], // w 7
  76. [x + 10, i * 20 + 10], // center
  77. ];
  78. entry = [[], [], [], []];
  79. tab.push(entry)
  80. var draw_polygon = function(color_index, corner_index, indices) {
  81. var ppts = indices.map(function(i) { return pts[i]; });
  82. var tri0 = new Path().moveTo(ppts[0][0], ppts[0][1]);
  83. for (var j = 1; j < ppts.length; j++) {
  84. if (j > 1) {
  85. entry[corner_index].push(indices[0]);
  86. entry[corner_index].push(indices[j - 1]);
  87. entry[corner_index].push(indices[j]);
  88. }
  89. tri0.lineTo(ppts[j][0], ppts[j][1]);
  90. }
  91. tri0.closePath();
  92. tri0.addTo(stage);
  93. tri0.stroke('#000');
  94. tri0.attr({
  95. strokeWidth: 0.5,
  96. join: 'round'
  97. });
  98. tri0.fill(colors[color_index]);
  99. };
  100. edge = [[], [], [], []];
  101. edgetab.push(edge)
  102. var draw_edge = function(color_index, corner_index, indices) {
  103. var ppts = indices.map(function(i) { return pts[i]; });
  104. var x0 = ppts[0][0], y0 = ppts[0][1];
  105. var x1 = ppts[1][0], y1 = ppts[1][1];
  106. var dx = x1 - x0, dy = y1 - y0;
  107. var isqrt = 2 / Math.sqrt(dx * dx + dy * dy);
  108. x0 -= (x1 - x0) * isqrt;
  109. y0 -= (y1 - y0) * isqrt;
  110. var lin0 = new Path().moveTo(x0, y0);
  111. for (var j = 1; j < ppts.length; j++) {
  112. edge[corner_index].push(indices[j - 1]);
  113. edge[corner_index].push(indices[j]);
  114. lin0.lineTo(ppts[j][0], ppts[j][1]);
  115. }
  116. lin0.addTo(stage);
  117. lin0.stroke(colors[color_index]);
  118. lin0.attr({
  119. strokeWidth: 1.5,
  120. opacity: 0.75,
  121. join: 'round'
  122. });
  123. };
  124. if (a == b && b == c && c == d) {
  125. draw_polygon(a, 0, [0, 2, 4, 6]);
  126. continue;
  127. }
  128. if ((a != b && b != c && c != d && a != c && a != d && b != d) ||
  129. (a == c && b == d)) {
  130. draw_polygon(a, 0, [0, 1, 8, 7]);
  131. draw_polygon(b, 1, [1, 2, 3, 8]);
  132. draw_polygon(c, 2, [8, 3, 4, 5]);
  133. draw_polygon(d, 3, [7, 8, 5, 6]);
  134. draw_edge(a, 0, [1, 8, 7]);
  135. draw_edge(b, 1, [3, 8, 1]);
  136. draw_edge(c, 2, [5, 8, 3]);
  137. draw_edge(d, 3, [7, 8, 5]);
  138. continue;
  139. }
  140. if (a == b && b == c) {
  141. draw_polygon(a, 0, [7, 0, 2, 4, 5]);
  142. draw_polygon(d, 3, [7, 5, 6]);
  143. draw_edge(d, 3, [7, 5]);
  144. draw_edge(a, 0, [5, 7]);
  145. continue;
  146. }
  147. if (b == c && c == d) {
  148. draw_polygon(b, 1, [1, 2, 4, 6, 7]);
  149. draw_polygon(a, 0, [7, 0, 1]);
  150. draw_edge(a, 0, [1, 7]);
  151. draw_edge(b, 1, [7, 1]);
  152. continue;
  153. }
  154. if (c == d && d == a) {
  155. draw_polygon(a, 0, [3, 4, 6, 0, 1]);
  156. draw_polygon(b, 1, [1, 2, 3]);
  157. draw_edge(b, 1, [3, 1]);
  158. draw_edge(a, 0, [1, 3]);
  159. continue;
  160. }
  161. if (b == d && d == a) {
  162. draw_polygon(a, 0, [5, 6, 0, 2, 3]);
  163. draw_polygon(c, 2, [3, 4, 5]);
  164. draw_edge(c, 2, [5, 3]);
  165. draw_edge(a, 0, [3, 5]);
  166. continue;
  167. }
  168. if (a == b && c == d) {
  169. draw_polygon(a, 0, [0, 2, 3, 7]);
  170. draw_edge(a, 0, [3, 7]);
  171. // draw_polygon(a, 0, [0, 1, 8, 7]);
  172. // draw_polygon(a, 0, [1, 2, 3, 8]);
  173. draw_polygon(c, 2, [7, 3, 4, 6]);
  174. draw_edge(c, 2, [7, 3]);
  175. // draw_polygon(c, 2, [7, 8, 5, 6]);
  176. // draw_polygon(c, 2, [8, 3, 4, 5]);
  177. continue;
  178. }
  179. if (b == c && a == d) {
  180. draw_polygon(b, 1, [1, 2, 4, 5]);
  181. draw_edge(b, 1, [5, 1]);
  182. // draw_polygon(b, 1, [1, 2, 3, 8])
  183. // draw_polygon(b, 1, [8, 3, 4, 5])
  184. draw_polygon(a, 0, [0, 1, 5, 6]);
  185. draw_edge(a, 0, [1, 5]);
  186. // draw_polygon(a, 0, [0, 1, 8, 7]);
  187. // draw_polygon(a, 0, [7, 8, 5, 6]);
  188. continue;
  189. }
  190. if (a == b) {
  191. // draw_polygon(a, 0, [0, 2, 3, 7]);
  192. draw_polygon(a, 0, [0, 1, 8, 7]);
  193. draw_polygon(a, 0, [1, 2, 3, 8]);
  194. draw_edge(a, 0, [3, 8, 7]);
  195. draw_polygon(c, 2, [8, 3, 4, 5]);
  196. draw_edge(c, 2, [5, 8, 3]);
  197. draw_polygon(d, 3, [7, 8, 5, 6]);
  198. draw_edge(d, 3, [7, 8, 5]);
  199. continue;
  200. }
  201. if (c == b) {
  202. // draw_polygon(b, 1, [1, 2, 4, 5]);
  203. draw_polygon(b, 1, [1, 2, 3, 8])
  204. draw_polygon(b, 1, [8, 3, 4, 5])
  205. draw_edge(b, 1, [5, 8, 1]);
  206. draw_polygon(a, 0, [0, 1, 8, 7]);
  207. draw_edge(a, 0, [1, 8, 7]);
  208. draw_polygon(d, 3, [7, 8, 5, 6]);
  209. draw_edge(d, 3, [7, 8, 5]);
  210. continue;
  211. }
  212. if (c == d) {
  213. // draw_polygon(c, 2, [7, 3, 4, 6]);
  214. draw_polygon(c, 2, [7, 8, 5, 6]);
  215. draw_polygon(c, 2, [8, 3, 4, 5]);
  216. draw_edge(c, 2, [7, 8, 3]);
  217. draw_polygon(a, 0, [0, 1, 8, 7]);
  218. draw_edge(a, 0, [1, 8, 7]);
  219. draw_polygon(b, 1, [1, 2, 3, 8]);
  220. draw_edge(b, 1, [3, 8, 1]);
  221. continue;
  222. }
  223. if (d == a) {
  224. // draw_polygon(a, 0, [0, 1, 5, 6]);
  225. draw_polygon(a, 0, [0, 1, 8, 7]);
  226. draw_polygon(a, 0, [7, 8, 5, 6]);
  227. draw_edge(a, 0, [1, 8, 5]);
  228. draw_polygon(b, 1, [1, 2, 3, 8]);
  229. draw_edge(b, 1, [3, 8, 1]);
  230. draw_polygon(c, 2, [8, 3, 4, 5]);
  231. draw_edge(c, 2, [5, 8, 3]);
  232. continue;
  233. }
  234. if (a == c && b != d) {
  235. draw_polygon(a, 0, [0, 1, 3, 4, 5, 7]);
  236. draw_edge(a, 0, [1, 3]);
  237. draw_edge(a, 0, [5, 7]);
  238. draw_polygon(b, 1, [1, 2, 3]);
  239. draw_edge(b, 1, [3, 1]);
  240. draw_polygon(d, 3, [7, 5, 6]);
  241. draw_edge(d, 3, [7, 5]);
  242. continue;
  243. }
  244. if (b == d && a != c) {
  245. draw_polygon(b, 1, [6, 7, 1, 2, 3, 5]);
  246. draw_edge(b, 1, [7, 1]);
  247. draw_edge(b, 1, [3, 5]);
  248. draw_polygon(a, 0, [7, 0, 1]);
  249. draw_edge(a, 0, [1, 7]);
  250. draw_polygon(c, 2, [3, 4, 5]);
  251. draw_edge(c, 2, [5, 3]);
  252. continue;
  253. }
  254. }
  255. msg = 'char const* QUATERNARY_TABLE =\n'
  256. for (var t = 0; t < tab.length; t++) {
  257. if ((t & 3) > 0) {
  258. continue;
  259. }
  260. msg += ' "';
  261. for (var c = 0; c < 4; c++) {
  262. msg += tab[t][c].length / 3;
  263. for (var p = 0; p < tab[t][c].length; p++) {
  264. msg += tab[t][c][p];
  265. }
  266. }
  267. msg += '"\n'
  268. }
  269. msg += '\nchar const* QUATERNARY_EDGES =\n'
  270. msg += ' "';
  271. for (var t = 0; t < edgetab.length; t++) {
  272. if ((t & 3) > 0) {
  273. continue;
  274. }
  275. for (var c = 0; c < 4; c++) {
  276. msg += edgetab[t][c].length / 2;
  277. for (var p = 0; p < edgetab[t][c].length; p++) {
  278. msg += edgetab[t][c][p];
  279. }
  280. }
  281. if ((t % 16) == 0) {
  282. msg += '"\n'
  283. msg += ' "';
  284. }
  285. }
  286. msg += '"\n'
  287. console.log(msg)
  288. }
  289. var el = document.getElementById('diagram');
  290. bonsai.run(el, {
  291. code: main,
  292. width: 115,
  293. height: 5120
  294. });
  295. // setTimeout(function() {
  296. // window.console.log(el.children[0].innerHTML);
  297. // }, 5000);
  298. </script>