added gitignore and koch

This commit is contained in:
Niel 2019-01-22 18:24:41 +01:00
parent 3a66f7772a
commit a28e6926d0
4 changed files with 59 additions and 1 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.vscode/

14
koch/index.html Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<script src="../p5/p5.min.js"></script>
<script src="../p5/p5.dom.min.js"></script>
<meta charset="utf-8">
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>

44
koch/sketch.js Normal file
View file

@ -0,0 +1,44 @@
let v1, v2;
function setup() {
createCanvas(800, 300);
v1 = createVector(50, 250);
v2 = createVector(750, 250);
background(0);
stroke(255);
koch(v1, v2, 6);
}
function koch(p1, p2, i) {
if (i === 1) connect(p1, p2);
else {
const {a, b, c, d, e} = generateEdges(p1, p2)
koch(a, b, i-1)
koch(b, c, i-1)
koch(c, d, i-1)
koch(d, e, i-1)
}
}
function connect(p1, p2) {
line(p1.x, p1.y, p2.x, p2.y);
}
function generateEdges(p1, p2) {
let v = p5.Vector.sub(p2, p1);
v.div(3);
let a = p1.copy();
let b = p5.Vector.add(p1, v);
let d = p5.Vector.sub(p2, v);
v.rotate(-PI/3);
let c = p5.Vector.add(b, v);
e = p2.copy();
return {a, b, c, d, e}
}

View file

@ -4,7 +4,6 @@
<head>
<script src="../p5/p5.min.js"></script>
<script src="../p5/p5.dom.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
</head>