Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dragon curve #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Dragon Curve/DragonCurve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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 length = 15;
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) {
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;
}
}
if (!found)
nextSentence += currentChar;
}
sentences.push(nextSentence);
currentSentence = nextSentence;
}

function turtle(n) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше сделать так, чтобы черепашка получала на вход готовое слово и его рисовала. Убери из нее логику выбора последнего sentence.

if (sentences.length > 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 < currentSentence.length; i++) {
var current = currentSentence.charAt(i);
if (current === "F") {
sky.beginPath();
sky.moveTo(0, 0);
sky.lineTo(0, -length);
sky.translate(0, -length);
sky.stroke();
} else if (current === "+") {
sky.rotate(toRadiance(90));
} else if (current === "-") {
sky.rotate(toRadiance(-90));
}
}
}
turtle(10);
15 changes: 15 additions & 0 deletions Dragon Curve/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="eng">

<head>

<meta charset="UTF-8">
<title>Document</title>
</head>

<body>
<script src="DragonCurve.js">
</script>
</body>

</html>