From e351fd2b54af521607e2a603f7a2af8d8ece55ec Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 17 Apr 2018 15:50:52 +0500 Subject: [PATCH 1/5] add dragon curve --- Dragon Curve/DragonCurve.js | 67 +++++++++++++++++++++++++++++++++++++ Dragon Curve/index.html | 15 +++++++++ 2 files changed, 82 insertions(+) create mode 100644 Dragon Curve/DragonCurve.js create mode 100644 Dragon Curve/index.html diff --git a/Dragon Curve/DragonCurve.js b/Dragon Curve/DragonCurve.js new file mode 100644 index 0000000..6f5d279 --- /dev/null +++ b/Dragon Curve/DragonCurve.js @@ -0,0 +1,67 @@ +var canvas = document.createElement('canvas'); +document.body.insertBefore(canvas, document.body.firstChild); +canvas.style.position = 'fixed'; +canvas.style.top = '0px'; +canvas.style.left = '0px'; +canvas.width = document.body.offsetWidth; +canvas.height = window.innerHeight; +var sky = canvas.getContext('2d'); +var len = 15; + +function inRad(num) { + return num * Math.PI / 180; +} +var axiome = "FX"; +var sentence = axiome; +var rules = []; + +rules[0] = { + a: "X", + b: "X+YF+" +} +rules[1] = { + a: "Y", + b: "-FX-Y" +} + +function turtle(n) { + for(var j=0;j + + + + + + Document + + + + + + + \ No newline at end of file From 625e2380976250a155d70129749836b579159fa9 Mon Sep 17 00:00:00 2001 From: Maxim Date: Wed, 23 May 2018 13:51:29 +0500 Subject: [PATCH 2/5] Refactoring --- Dragon Curve/DragonCurve.js | 70 +++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/Dragon Curve/DragonCurve.js b/Dragon Curve/DragonCurve.js index 6f5d279..8fd371e 100644 --- a/Dragon Curve/DragonCurve.js +++ b/Dragon Curve/DragonCurve.js @@ -6,22 +6,36 @@ canvas.style.left = '0px'; canvas.width = document.body.offsetWidth; canvas.height = window.innerHeight; var sky = canvas.getContext('2d'); -var len = 15; +var length = 15; -function inRad(num) { - return num * Math.PI / 180; +function toRadiance(degree) { + return degree * Math.PI / 180; } + var axiome = "FX"; var sentence = axiome; -var rules = []; - -rules[0] = { - a: "X", - b: "X+YF+" +var rules = { +"X" : "X+YF+" +,"Y" : "-FX-Y" } -rules[1] = { - a: "Y", - b: "-FX-Y" +var keys=Object.keys(rules); + +function generate() { + var nextSentence = ""; + for (var i = 0; i < sentence.length; i++) { + var current = sentence.charAt(i); + var found = false; + for (var j = 0; j < keys.length; j++) { + if (current === keys[j]){ + found = true; + nextSentence += rules[keys[j]]; + break; + } + } + if (!found) + nextSentence += current; + } + sentence = nextSentence; } function turtle(n) { @@ -33,35 +47,17 @@ function turtle(n) { sky.strokeStyle = "green"; for (var i = 0; i < sentence.length; i++) { var current = sentence.charAt(i); - if (current == "F") { + if (current === "F") { sky.beginPath(); sky.moveTo(0, 0); - sky.lineTo(0, -len); - sky.translate(0, -len); + sky.lineTo(0, -length); + sky.translate(0, -length); sky.stroke(); - } else if (current == "+") { - sky.rotate(inRad(90)); - } else if (current == "-") { - sky.rotate(inRad(-90)); + } else if (current === "+") { + sky.rotate(toRadiance(90)); + } else if (current === "-") { + sky.rotate(toRadiance(-90)); } } } - -function generate() { - var nextSentence = ""; - for (var i = 0; i < sentence.length; i++) { - var current = sentence.charAt(i); - var found = false; - for (var j = 0; j < rules.length; j++) { - if (current == rules[j].a) { - found = true; - nextSentence += rules[j].b; - break; - } - } - if (!found) - nextSentence += current; - } - sentence = nextSentence; -} -turtle(10); \ No newline at end of file +turtle(10); From 75bd82b65f2bba7c230b8da6aafec6a487aadd9b Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 29 May 2018 21:19:15 +0500 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BE=D1=81=D1=82=D0=BE=D1=8F?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dragon Curve/DragonCurve.js | 45 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/Dragon Curve/DragonCurve.js b/Dragon Curve/DragonCurve.js index 8fd371e..ba161b7 100644 --- a/Dragon Curve/DragonCurve.js +++ b/Dragon Curve/DragonCurve.js @@ -7,46 +7,53 @@ canvas.width = document.body.offsetWidth; canvas.height = window.innerHeight; var sky = canvas.getContext('2d'); var length = 15; - -function toRadiance(degree) { - return degree * Math.PI / 180; -} - var axiome = "FX"; -var sentence = axiome; +var currentSentence = axiome; +var sentences = [axiome]; var rules = { -"X" : "X+YF+" -,"Y" : "-FX-Y" + "X": "X+YF+" + , "Y": "-FX-Y" +} +var keys = Object.keys(rules); + +function toRadiance(degree) { + return degree * Math.PI / 180; } -var keys=Object.keys(rules); function generate() { + currentSentence = sentences[sentences.length - 1]; var nextSentence = ""; - for (var i = 0; i < sentence.length; i++) { - var current = sentence.charAt(i); + for (var i = 0; i < currentSentence.length; i++) { + var currentChar = currentSentence.charAt(i); var found = false; for (var j = 0; j < keys.length; j++) { - if (current === keys[j]){ + if (currentChar === keys[j]) { found = true; nextSentence += rules[keys[j]]; break; } } if (!found) - nextSentence += current; + nextSentence += currentChar; } - sentence = nextSentence; + sentences.push(nextSentence); + currentSentence = nextSentence; } function turtle(n) { - for(var j=0;j n) + currentSentence = sentences[n]; + else { + let len = sentences.length; + for (var j = 0; j < n - len + 1; j++) { + generate(); + } } sky.translate(600, 500); sky.lineWidth = "1"; sky.strokeStyle = "green"; - for (var i = 0; i < sentence.length; i++) { - var current = sentence.charAt(i); + for (var i = 0; i < currentSentence.length; i++) { + var current = currentSentence.charAt(i); if (current === "F") { sky.beginPath(); sky.moveTo(0, 0); @@ -57,7 +64,7 @@ function turtle(n) { sky.rotate(toRadiance(90)); } else if (current === "-") { sky.rotate(toRadiance(-90)); - } + } } } turtle(10); From 43398a248c3b8130b491a4d5f751fee87a8e95cc Mon Sep 17 00:00:00 2001 From: Maxim Date: Mon, 18 Jun 2018 00:01:03 +0500 Subject: [PATCH 4/5] Fixed --- Dragon Curve/DragonCurve.js | 49 +++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/Dragon Curve/DragonCurve.js b/Dragon Curve/DragonCurve.js index ba161b7..3b1fabe 100644 --- a/Dragon Curve/DragonCurve.js +++ b/Dragon Curve/DragonCurve.js @@ -20,40 +20,35 @@ function toRadiance(degree) { return degree * Math.PI / 180; } -function generate() { - currentSentence = sentences[sentences.length - 1]; - var nextSentence = ""; - for (var i = 0; i < currentSentence.length; i++) { - var currentChar = currentSentence.charAt(i); - var found = false; - for (var j = 0; j < keys.length; j++) { - if (currentChar === keys[j]) { - found = true; - nextSentence += rules[keys[j]]; - break; +function generate(n) { + for (var k = 0; k < n; k++) { + currentSentence = sentences[sentences.length - 1]; + var nextSentence = ""; + for (var i = 0; i < currentSentence.length; i++) { + var currentChar = currentSentence.charAt(i); + var found = false; + for (var j = 0; j < keys.length; j++) { + if (currentChar === keys[j]) { + found = true; + nextSentence += rules[keys[j]]; + break; + } } + if (!found) + nextSentence += currentChar; } - if (!found) - nextSentence += currentChar; + sentences.push(nextSentence); + currentSentence = nextSentence; } - sentences.push(nextSentence); - currentSentence = nextSentence; + turtle(sentences[n]); } -function turtle(n) { - if (sentences.length > n) - currentSentence = sentences[n]; - else { - let len = sentences.length; - for (var j = 0; j < n - len + 1; j++) { - generate(); - } - } +function turtle(totalSentence) { sky.translate(600, 500); sky.lineWidth = "1"; sky.strokeStyle = "green"; - for (var i = 0; i < currentSentence.length; i++) { - var current = currentSentence.charAt(i); + for (var i = 0; i < totalSentence.length; i++) { + var current = totalSentence.charAt(i); if (current === "F") { sky.beginPath(); sky.moveTo(0, 0); @@ -67,4 +62,4 @@ function turtle(n) { } } } -turtle(10); +generate(10); From 22c8cbe87d3668f4ef2631a600659211fc518d80 Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 19 Jun 2018 14:57:48 +0500 Subject: [PATCH 5/5] Totaly fixed --- Dragon Curve/DragonCurve.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Dragon Curve/DragonCurve.js b/Dragon Curve/DragonCurve.js index 3b1fabe..6b7c695 100644 --- a/Dragon Curve/DragonCurve.js +++ b/Dragon Curve/DragonCurve.js @@ -7,13 +7,15 @@ canvas.width = document.body.offsetWidth; canvas.height = window.innerHeight; var sky = canvas.getContext('2d'); var length = 15; -var axiome = "FX"; +var axiome = "[FX]"; var currentSentence = axiome; var sentences = [axiome]; + var rules = { "X": "X+YF+" , "Y": "-FX-Y" } + var keys = Object.keys(rules); function toRadiance(degree) { @@ -40,25 +42,32 @@ function generate(n) { sentences.push(nextSentence); currentSentence = nextSentence; } - turtle(sentences[n]); + turtle(sentences[n],{x: 0, y: 0, angle: 0 },90); } -function turtle(totalSentence) { - sky.translate(600, 500); +function turtle(totalSentence, startPosition, angle) { + let position = startPosition; + var stack = []; + sky.translate(800, 200); sky.lineWidth = "1"; sky.strokeStyle = "green"; for (var i = 0; i < totalSentence.length; i++) { var current = totalSentence.charAt(i); if (current === "F") { sky.beginPath(); - sky.moveTo(0, 0); - sky.lineTo(0, -length); - sky.translate(0, -length); + sky.moveTo(position.x, position.y); + position.x += Math.cos(position.angle* Math.PI/180) * length; + position.y += Math.sin(position.angle* Math.PI/180) * length; + sky.lineTo(position.x, position.y); sky.stroke(); } else if (current === "+") { - sky.rotate(toRadiance(90)); + position.angle += angle; } else if (current === "-") { - sky.rotate(toRadiance(-90)); + position.angle -= angle; + } else if (current === "[") { + stack.push({ x: position.x, y: position.y, angle: position.angle }); + } else if (current === "]") { + position = stack.pop(); } } }