Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.61 KB | None | 0 0
  1. // Set up!
  2. var a_canvas = document.getElementById("a");
  3. var context = a_canvas.getContext("2d");
  4.  
  5. var lineLength = 20;
  6. var treeHeight = 1;
  7.  
  8. function drawGrid(treeHeight, lineLength) {
  9. var width = 0;
  10. for (i = 0; i <= treeHeight - 1; i++) {
  11. width = width + Math.pow(3, treeHeight - i - 1);
  12. }
  13. width = width * 2;
  14. return width;
  15. }
  16.  
  17. var width = drawGrid(3, 1);
  18. var height = Math.sqrt((3/4)*Math.pow(width, 2))
  19.  
  20. //draw horizontal lines
  21. for (i = 0; i < width+3; i++) { //+3 and +2 in the function r padding
  22. context.beginPath();
  23. context.moveTo(0, i*lineLength*(height/width));
  24. context.lineTo((width+2)*lineLength, i*lineLength*(height/width));
  25. context.stroke();
  26. }
  27.  
  28. context.fillStyle = "black";
  29.  
  30. //draw two vertical border lines
  31. context.beginPath();
  32. context.moveTo(0, 0);
  33. context.lineTo(0, lineLength*(width+2)*(height/width));
  34. context.stroke();
  35. context.beginPath();
  36. context.moveTo((width+2)*lineLength, 0);
  37. context.lineTo((width+2)*lineLength, lineLength*(width+2)*(height/width));
  38. context.stroke();
  39.  
  40. //draw the vertical (left leaning) lines
  41. //--------------------------------------------------------------------------------
  42. for (i = 0; i < (width/2)+1; i++) { //+3 and +2 in the function r padding
  43. if (width%4===0) { //when shit is even
  44. context.beginPath();
  45. context.moveTo(0.5*lineLength+lineLength*i, lineLength*(width+2)*(height/width));
  46. context.lineTo(0, height*lineLength - (lineLength*2*(height/width))*i + (lineLength*(height/width)));
  47. context.stroke();
  48. } else { //when shit is odd
  49. context.beginPath();
  50. context.moveTo(lineLength+lineLength*i, lineLength*(width+2)*(height/width));
  51. context.lineTo(0, height*lineLength - (lineLength*2*(height/width))*i);
  52. context.stroke();
  53. }
  54. }
  55.  
  56. for (i = 0; i < (width/2)+1; i++) { //+3 and +2 in the function r padding
  57. if (width%4===0) { //when shit is even
  58. context.beginPath();
  59. context.moveTo(lineLength+lineLength*i - 0.5*lineLength + ((width/2)+1)*lineLength, lineLength*(width+2)*(height/width));
  60. context.lineTo(lineLength*i + 0.5*lineLength, 0);
  61. context.stroke();
  62. } else { //when shit is odd
  63. context.beginPath();
  64. context.moveTo(lineLength+lineLength*i + ((width/2)+1)*lineLength, lineLength*(width+2)*(height/width));
  65. context.lineTo(lineLength+lineLength*i, 0);
  66. context.stroke();
  67. }
  68. }
  69.  
  70. for (i = 0; i < (width/2)+1; i++) { //+3 and +2 in the function r padding
  71. if (width%4===0) { //when shit is even
  72. context.beginPath();
  73. context.moveTo((width+2)*lineLength, height*lineLength - (lineLength*2*(height/width))*i + (lineLength*(height/width)));
  74. context.lineTo(lineLength+lineLength*i + ((width/2)+1)*lineLength - 0.5*lineLength, 0);
  75. context.stroke();
  76. } else { //when shit is odd
  77. context.beginPath();
  78. context.moveTo((width+2)*lineLength, height*lineLength - (lineLength*2*(height/width))*i);
  79. context.lineTo(lineLength+lineLength*i + ((width/2)+1)*lineLength, 0);
  80. context.stroke();
  81. }
  82. }
  83. //--------------------------------------------------------------------------------
  84.  
  85. //draw the vertical (right leaning) lines
  86. //--------------------------------------------------------------------------------
  87. for (i = 0; i < (width/2)+1; i++) { //+3 and +2 in the function r padding
  88. if (width%4===0) { //when shit is even
  89. context.beginPath();
  90. context.moveTo(0, height*lineLength - (lineLength*2*(height/width))*i + (lineLength*(height/width)));
  91. context.lineTo(lineLength*(width)/2 - lineLength*i + 0.5*lineLength, 0);
  92. context.stroke();
  93. } else { //when shit is odd
  94. context.beginPath();
  95. context.moveTo(0, (lineLength*2*(height/width))*i + (lineLength*2*(height/width)));
  96. context.lineTo(lineLength + lineLength*i, 0);
  97. context.stroke();
  98. }
  99. }
  100.  
  101. for (i = 0; i < (width/2)+1; i++) { //+3 and +2 in the function r padding
  102. if (width%4===0) { //when shit is even
  103. context.beginPath();
  104. context.moveTo(0.5*lineLength+lineLength*i, lineLength*(width+2)*(height/width));
  105. context.lineTo(lineLength+lineLength*i + ((width/2)+1)*lineLength - 0.5*lineLength, 0);
  106. context.stroke();
  107. } else { //when shit is odd
  108. context.beginPath();
  109. context.moveTo(lineLength+lineLength*i, lineLength*(width+2)*(height/width));
  110. context.lineTo(lineLength+lineLength*i + ((width/2)+1)*lineLength, 0);
  111. context.stroke();
  112. }
  113. }
  114.  
  115. for (i = 0; i < (width/2)+1; i++) { //+3 and +2 in the function r padding
  116. if (width%4===0) { //when shit is even
  117. context.beginPath();
  118. context.moveTo(lineLength+lineLength*i - 0.5*lineLength + ((width/2)+1)*lineLength, lineLength*(width+2)*(height/width));
  119. context.lineTo((width+2)*lineLength, (lineLength*2*(height/width))*i + (lineLength*(height/width)));
  120. context.stroke();
  121. } else { //when shit is odd
  122. context.beginPath();
  123. context.moveTo(lineLength+lineLength*i + ((width/2)+1)*lineLength, lineLength*(width+2)*(height/width));
  124. context.lineTo((width+2)*lineLength, (lineLength*2*(height/width))*i + (lineLength*2*(height/width)));
  125. context.stroke();
  126. }
  127. }
  128. //--------------------------------------------------------------------------------
  129.  
  130.  
  131. //drawNode(x, y, z)
  132. function drawNode(x, y, z) {
  133. var xAdjust = 0;
  134. xAdjust = xAdjust + x*lineLength; //x
  135. xAdjust = xAdjust - y*0.5*lineLength; //y
  136. xAdjust = xAdjust + z*0.5*lineLength; //z
  137. var yAdjust = 0;
  138. if (y!==0) {
  139. yAdjust = yAdjust - Math.abs(y)/y * Math.sqrt((3/4)*Math.pow(lineLength*y, 2)); //y
  140. }
  141. if (z!==0) {
  142. yAdjust = yAdjust - Math.abs(z)/z * Math.sqrt((3/4)*Math.pow(lineLength*z, 2)); //z
  143. }
  144.  
  145. context.fillStyle = "black";
  146. context.beginPath();
  147. context.arc(((width+2)*lineLength)/2 + xAdjust, (lineLength*(width+2)*(height/width))/2 + yAdjust, 5, 0, 2*Math.PI);
  148. context.closePath();
  149. context.fill();
  150. //return yAdjust;
  151. return;
  152. }
  153.  
  154. //Function to draw the lines between nodes
  155. function drawLine(xfrom, yfrom, zfrom, xto, yto, zto) {
  156. context.strokeStyle = "red";
  157. context.lineWidth = 3;
  158.  
  159. var xAdjustfrom = 0;
  160. xAdjustfrom = xAdjustfrom + xfrom*lineLength; //x
  161. xAdjustfrom = xAdjustfrom - yfrom*0.5*lineLength; //y
  162. xAdjustfrom = xAdjustfrom + zfrom*0.5*lineLength; //z
  163. var yAdjustfrom = 0;
  164. if (yfrom!==0) {
  165. yAdjustfrom = yAdjustfrom - Math.abs(yfrom)/yfrom * Math.sqrt((3/4)*Math.pow(lineLength*yfrom, 2)); //y
  166. }
  167. if (zfrom!==0) {
  168. yAdjustfrom = yAdjustfrom - Math.abs(zfrom)/zfrom * Math.sqrt((3/4)*Math.pow(lineLength*zfrom, 2)); //z
  169. }
  170.  
  171. var xAdjustto = 0;
  172. xAdjustto = xAdjustto + xto*lineLength; //x
  173. xAdjustto = xAdjustto - yto*0.5*lineLength; //y
  174. xAdjustto = xAdjustto + zto*0.5*lineLength; //z
  175. var yAdjustto = 0;
  176. if (yto!==0) {
  177. yAdjustto = yAdjustto - Math.abs(yto)/yto * Math.sqrt((3/4)*Math.pow(lineLength*yto, 2)); //y
  178. }
  179. if (zto!==0) {
  180. yAdjustto = yAdjustto - Math.abs(zto)/zto * Math.sqrt((3/4)*Math.pow(lineLength*zto, 2)); //z
  181. }
  182.  
  183. context.beginPath();
  184. context.moveTo(((width+2)*lineLength)/2 + xAdjustfrom, (lineLength*(width+2)*(height/width))/2 + yAdjustfrom);
  185. context.lineTo(((width+2)*lineLength)/2 + xAdjustto, (lineLength*(width+2)*(height/width))/2 + yAdjustto);
  186. context.stroke();
  187.  
  188. return;
  189. }
  190.  
  191. context.fillStyle = "black";
  192.  
  193. // Write "Hello, World!"
  194. context.font = "30px Garamond";
  195. //context.fillText(drawNode(1, 1, 1),15,175);
  196. //context.fillText(height,15,150);
  197.  
  198. function parse(text){
  199.  
  200. function maketree(index, root){
  201.  
  202. while(true){
  203.  
  204. var laatstekomma = text.lastIndexOf(',', index-1);
  205. var laatstehaakje = text.lastIndexOf(')', index-1);
  206. var laatstesluitend = text.lastIndexOf('(', index-1);
  207.  
  208. if (laatstesluitend == -1 && laatstekomma == -1){ // er is geen sluitend ding of komma meer dus alles is geparsed (gebeurt alleen bij eerste keer aanroepen)
  209. var naam = text.substring(0, index).trim();
  210. var newchild = new node(naam, root, []);
  211. var newindex = 0;
  212.  
  213. root.children.push(newchild);
  214. return 0;
  215. }
  216.  
  217. if (text.charAt(index) == '(') { // laatste element subtree is net geparsed
  218. return Math.max(text.lastIndexOf(',', index - 1), text.lastIndexOf('(', index - 1)) + 1;
  219. }
  220.  
  221. if (laatstekomma < laatstehaakje && laatstesluitend < laatstehaakje){ // heeft een subtree
  222. var naam = text.substring(laatstehaakje + 1, index).trim();
  223. var newchild = new node(naam, root, []);
  224.  
  225. var newindex = maketree(laatstehaakje, newchild);
  226.  
  227. root.children.push(newchild);
  228.  
  229. index = newindex - 1;
  230. }
  231.  
  232. if (laatstehaakje < laatstesluitend || laatstehaakje < laatstekomma){ // heeft geen subtree
  233. var poep = Math.max(laatstesluitend, laatstekomma);
  234. var naam = text.substring(poep + 1, index).trim();
  235. var newchild = new node(naam, root, []);
  236.  
  237. root.children.push(newchild);
  238.  
  239. index = poep;
  240. }
  241.  
  242.  
  243. }
  244. }
  245.  
  246. text = text.trim();
  247. text = text.replace(/\n/g, "");
  248. console.log(text);
  249. var superroot = new node("", null, []);
  250. maketree(text.length - 1, superroot);
  251. var echteroot = superroot.children[0];
  252. echteroot.parent = null;
  253. return echteroot;
  254. }
  255.  
  256. class node{
  257. constructor(name, parent, children) {
  258. this.parent = parent
  259. this.children = children
  260. this.name = name
  261. //this.descendentsAmount = null
  262. }
  263.  
  264. get childrenAmount(){
  265. return this.children.length;
  266. }
  267. get descendentsAmount(){
  268. //if (this.descendentsAmount == null){
  269. var tmp = 0
  270. for (var child of this.children){
  271. tmp += child.descendentsAmount + 1
  272. }
  273. return tmp
  274. //}
  275. //return this.descendentsAmount
  276. }
  277. get height(){
  278. var max = 0;
  279. for (var child of this.children){
  280. max = Math.max(max, child.height + 1);
  281. }
  282. return max;
  283. }
  284.  
  285. }
  286.  
  287. var trueRoot;
  288.  
  289. function drawTree(root, pdir, x, y, z, depth) {
  290. //direction to the right is 1, then mod 6
  291. var dir = (1+pdir)%6;
  292. if (pdir == -1) {
  293. i = -1;
  294. trueRoot = root;
  295. } else {
  296. i = 0;
  297. }
  298. drawNode(x, y, z) //draw root node
  299. var linelength = Math.pow(3, trueRoot.height-depth-1); //need the fucking depth for this
  300. for (var child of root.children) {
  301. console.log(root.name + " " + child.name + " " + dir + " " + depth);
  302. if (i > 4) {
  303. break;
  304. } else {
  305. if (i == -1) {
  306. context.fillText(dir,60,300);
  307. context.fillText(x+linelength,90,300);
  308. context.fillText(y,60,300);
  309. context.fillText(z,60,300);
  310. }
  311. i++
  312. if (dir == 0) { //pdir 6
  313. drawLine(x, y, z, x+linelength, y, z);
  314. drawTree(child, 3, x+linelength, y, z, depth+1);
  315. } else if (dir == 1) {
  316. drawLine(x, y, z, x, y-linelength, z);
  317. drawTree(child, 4, x, y-linelength, z, depth+1);
  318. } else if (dir == 2) {
  319. drawLine(x, y, z, x, y, z-linelength);
  320. drawTree(child, 5, x, y, z-linelength, depth+1);
  321. } else if (dir == 3) {
  322. drawLine(x, y, z, x-linelength, y, z);
  323. drawTree(child, 0, x-linelength, y, z, depth+1);
  324. } else if (dir == 4) {
  325. drawLine(x, y, z, x, y+linelength, z);
  326. drawTree(child, 1, x, y+linelength, z, depth+1);
  327. } else if (dir == 5) {
  328. drawLine(x, y, z, x, y, z+linelength);
  329. drawTree(child, 2, x, y, z+linelength, depth+1);
  330. }
  331. console.log(dir)
  332. dir = (dir+1)%6
  333. //drawLine(x, y, z, 3, 0, 0)
  334. context.fillText(dir,15,300);
  335. }
  336. }
  337. }
  338.  
  339. drawTree(parse("(A,B,(C,(Z,N)D,G,H)E)F;"), -1, 0, 0, 0, 0);
  340.  
  341. //
  342. // Example function by pieter
  343. //
  344. //function printtree(root, pre){
  345. // var string = "";
  346. // if (pre.length != 0)
  347. // string += pre + "-";
  348. // string += root.name + "\n";
  349.  
  350. // for (var child of root.children){
  351. // string += printtree(child, pre + (pre.length != 0 ? " |" : "|"));
  352. // if(child != root.children[root.children.length - 1] && child.childrenAmount != 0)
  353. // string += pre + (pre.length != 0 ? " |" : "|") + "\n"
  354. // }
  355. // return string
  356. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement