-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.html
125 lines (105 loc) · 2.84 KB
/
cc.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
125
<style>
p {
font-size: 16px;
}
#collatz {
border: 1px solid black;
}
#collatz-div {
white-space: pre;
display: flex;
}
.collatz-item {
font-size: 10px;
padding: 6px;
text-align: center;
}
</style>
<h1>Collatz Conjecture</h1>
<canvas id="collatz"></canvas>
<!-- <div id="collatz-div"></div> -->
<p>Inspired by:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/LqKpkdRRLZw" frameborder="0" allowfullscreen></iframe>
<br>
<br>
<a href="/">Back to home</a>
<script>
var canvasWidth = 600
var canvasHeight = 400
var canvas = document.getElementById('collatz')
canvas.width = canvasWidth
canvas.height = canvasHeight
var ctx = canvas.getContext('2d')
// start point consist of x, y coordinates and previous vector (angle)
var startPoint = [100, canvasHeight-20, 50]
var lineLength = 3
var angleDelta = 3
for (var i = 1; i <= 10000; i++)
draw(i)
function draw(n) {
var p = startPoint
var stack = new Array()
ctx.strokeStyle = getRandomColour()
ctx.lineWidth = 12
ctx.lineCap = 'round'
ctx.beginPath()
ctx.moveTo(p[0], p[1])
do {
n = getNextNumber(n)
stack.push(n)
} while (n != 1)
while (stack.length > 0) {
if (stack.pop() % 2 == 0) {
p = drawNext(p, angleDelta)
} else {
p = drawNext(p, -angleDelta)
}
}
ctx.stroke()
// ctx.strokeStyle = "white"
// ctx.lineWidth = 10
// ctx.stroke()
}
function drawNext(p, angleChange) {
// move new vector by 30 degrees
var newAngleDegrees = p[2] + angleChange
var newAngleRadians = toRadians(newAngleDegrees)
var xChange = lineLength * Math.cos(newAngleRadians)
var yChange = lineLength * Math.sin(newAngleRadians)
p = [p[0]-xChange, p[1]-yChange, newAngleDegrees]
ctx.lineTo(p[0], p[1])
return p
}
function toRadians (angle) {
return angle * (Math.PI / 180);
}
var collatzDiv = document.getElementById('collatz-div')
for (var i = 1; i <= 500; i++) {
var n = i
var collatzItem = document.createElement("div");
collatzItem.className = 'collatz-item'
collatzItem.textContent += n + "\r\n"
do {
n = getNextNumber(n)
collatzItem.textContent += n + "\r\n"
} while (n != 1)
// collatzItem.textContent += "\r\n"
collatzDiv.appendChild(collatzItem)
}
function getNextNumber(n) {
if (n % 2 == 0) {
return n/2
} else {
return 3*n + 1
}
}
function getRandomInt(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
//The maximum is exclusive and the minimum is inclusive
}
function getRandomColour(min, max) {
return 'rgb( ' + getRandomInt(0,256) + ', ' + getRandomInt(0,256) + ', ' + getRandomInt(0,256) + ')'
}
</script>