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

Шарафиева Юлия #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
98 changes: 98 additions & 0 deletions LSystems.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!doctype html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-cache" />

<script>

function LSystem(axiom, rules) {
this.axiom = axiom;
this.rules = rules;
this.makeNewF = function (n) {
let str = "";
for (var i = 0; i < n; i++) {
str = "";
for (var smb of this.axiom){
if(smb == "F")
str += this.rules;
else
str += smb;
}
this.axiom = str;
}
return this.axiom;
};
}
function Painter(point, lenStep, angle) {
this.p = point;
this.s = lenStep;
this.q = angle;
this.draw = function (string) {
var canvas = document.getElementById("canvas2");
var canvasHeight = parseInt(canvas.getAttribute("height"));
var canvasWidth = parseInt(canvas.getAttribute("width"));
var context = canvas.getContext('2d');
context.lineWidth = "1";
context.strokeStyle = "black";
context.beginPath();
context.moveTo(this.p.x, this.p.y);
var stack = [];
for (var i = 0; i < string.length; i++) {
switch (string.charAt(i)) {
case 'F':
this.p.x += this.s * Math.cos(this.p.a * Math.PI / 180);
this.p.y += this.s * Math.sin(this.p.a * Math.PI / 180);
context.lineTo(this.p.x, this.p.y);
break;
case '+':
this.p.a += this.q;
break;
case '-':
this.p.a -= this.q;
break;
case '[':
stack.push({ x: this.p.x, y: this.p.y, a: this.p.a })
break;
case ']':
var tmp = stack.pop();
this.p.x = tmp.x;
this.p.y = tmp.y;
this.p.a = tmp.a;
context.moveTo(this.p.x, this.p.y);
break;
default:
break;
}
}
context.stroke();
}
}
function run() {
var fractal = prompt ("Введите 1, чтобы нарисовать куст или 2, чтобы нарисовать снежинку ", 1);
if (fractal === "2"){
var n = 3;
var q =60;
var s =2;
var point = { x: 450, y: 450, a: 0 };
var ls = new LSystem("[F]+[F]+[F]+[F]+[F]+[F]", "F F[+FF][-FF]FF[+F][-F]FF");
Copy link

Choose a reason for hiding this comment

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

Если бы ты использовала let, то переменную ls надо было объявить вне if/else. Это верно, потому что результат используется после. var в JS позволяет тебе написать так, как ты написала, но это плохая часть языка. В 2018 году не надо использовать var в JS.

}
else{
var q = 22.5;
var s = 10;
var point = { x: 450, y: 450, a: 250 };
var ls = new LSystem("F", "-F+F+[+F-F-]-[-F+F+F]");
var n = 4;
}
var alg = ls.makeNewF(n);
var painter = new Painter(point, s, q);
painter.draw(alg);
}
</script>

<body onload="run()">
<canvas height='1000' width='1000' id='canvas2'></canvas>
</body>

</html>
110 changes: 0 additions & 110 deletions index.html

This file was deleted.