/
index.html
124 lines (104 loc) · 3.33 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>レール経路の再帰描画</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
</style>
<script type="text/javascript">
// 2017.1.13 Jun Mitani
var UNIT_LENGTH = 50;
var POINT_R = 0.8;
var DEPTH_LIMIT = 4; // 再帰の深さ
var bDrawPoint = true; // 点の描画の有無
var bDrawLine = true;// 経路の描画の有無
function drawTree(ctx, type, angleIN, startX, startY, depth) {
if(type == "R") {
var cx = startX + UNIT_LENGTH * Math.sin((angleIN)* Math.PI / 180.0);
var cy = startY - UNIT_LENGTH * Math.cos((angleIN)* Math.PI / 180.0);
var sa = (angleIN + 90) * Math.PI / 180.0;
var ea = (angleIN + 45) * Math.PI / 180.0;
if(bDrawLine) {
ctx.beginPath();
ctx.arc(cx, cy, UNIT_LENGTH, ea, sa, false);
ctx.stroke();
}
startX = cx + UNIT_LENGTH * Math.cos((angleIN + 45) * Math.PI / 180.0);
startY = cy + UNIT_LENGTH * Math.sin((angleIN + 45) * Math.PI / 180.0);
angleIN -= 45;
} else if(type == "L") {
var cx = startX - UNIT_LENGTH * Math.sin((angleIN)* Math.PI / 180.0);
var cy = startY + UNIT_LENGTH * Math.cos((angleIN)* Math.PI / 180.0);
var sa = (angleIN - 90) * Math.PI / 180.0;
var ea = (angleIN - 45) * Math.PI / 180.0;
if(bDrawLine) {
ctx.beginPath();
ctx.arc(cx, cy, UNIT_LENGTH, sa, ea, false);
ctx.stroke();
}
startX = cx + UNIT_LENGTH * Math.cos((angleIN - 45) * Math.PI / 180.0);
startY = cy + UNIT_LENGTH * Math.sin((angleIN - 45) * Math.PI / 180.0);
angleIN += 45;
} else if(type == "S") {
var sx = startX;
var sy = startY;
var ex = sx + UNIT_LENGTH * Math.cos(angleIN * Math.PI / 180.0);
var ey = sy + UNIT_LENGTH * Math.sin(angleIN * Math.PI / 180.0);
if(bDrawLine) {
ctx.beginPath();
ctx.moveTo(sx, sy);
ctx.lineTo(ex, ey);
ctx.stroke();
}
startX = ex;
startY = ey;
} else {
return;
}
if(bDrawPoint) {
ctx.beginPath();
ctx.arc(startX, startY, POINT_R*2, 0, Math.PI*2, true);
ctx.fill();
}
if(depth >= DEPTH_LIMIT) return;
drawTree(ctx, "L", angleIN, startX, startY, depth+1);
drawTree(ctx, "S", angleIN, startX, startY, depth+1);
drawTree(ctx, "R", angleIN, startX, startY, depth+1);
}
function draw() {
var ww = window.innerWidth;
var wh = window.innerHeight;
var canvas = document.getElementById("mycanvas");
canvas.width = ww;
canvas.height = wh;
if(canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate( canvas.width/2, canvas.height/2);
var s = (canvas.height/2 -50) /(UNIT_LENGTH * (DEPTH_LIMIT));
ctx.scale(s, -s);
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgb(0, 0, 0)';
ctx.fillStyle = 'rgb(0, 0, 0)';
for(var i = 0; i < 8; i++) {
ctx.rotate(45 * Math.PI / 180);
drawTree(ctx, "L", 90, 0, 0, 1);
drawTree(ctx, "S", 90, 0, 0, 1);
drawTree(ctx, "R", 90, 0, 0, 1);
}
}
}
</script>
<style type="text/css">
canvas { border: 1px solid #000000; }
</style>
</head>
<body onload="draw()">
<canvas id="mycanvas" width="800" height="600"></canvas>
</body>
</html>