-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime-spiral.html
executable file
·176 lines (144 loc) · 5.63 KB
/
prime-spiral.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Polar Prime Plot</title>
<style>
body {
background-color: slateblue;
}
#canvas {
border-style: double;
border-color: slateblue;
}
#mid {
text-align: center;
margin-right: 15%;
}
.slider {
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
</style>
</head>
<body>
<script type="text/javascript">
var num_max = 400;
var num_current = 0;
var numsPerFrame = 1e1001;//2000000;
var scale = 0.03;
var timer;
var color_bg = "#000";
var color_dot = "#0FF";
var color_centerDot = "#888";
var dotRadius = 4;
var centerX;
var centerY;
window.onload = setup;
let primeStorage = [false, false, true];
function setup() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
document.getElementById("scaleRange").oninput = updateScale;
centerX = canvas.width / 2;
centerY = canvas.height / 2;
timer = window.requestAnimationFrame(preDraw);
}
function preDraw() {
//drawing background, center dot, prime 2
//beg
ctx.fillStyle = color_bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
//center dot
drawCircle(color_centerDot, centerX, centerY, dotRadius);
//drawing 2
drawCircle(color_dot, centerX + ((2 / scale) * Math.cos(2)), centerY + ((2 / scale) * Math.sin(2)), dotRadius);
num_current = 3;
//calling main function
draw();
}
function draw() {
//testing x numbers
for (var a=num_current;a<num_max;a+=2) {
//if it's prime, put it in its place
if (isPrime(a)) {
var xOff = (a / scale) * Math.cos(a);
var yOff = (a / scale) * Math.sin(a);
drawCircle(color_dot, centerX + xOff, centerY + yOff, dotRadius);
}
}
num_current += numsPerFrame;
//call next frame
if (num_current < num_max) {
timer = window.requestAnimationFrame(draw);
}
}
function updateScale() {
//turn into a decimal, then square for more smaller values
scale = (((this.value / 2000) * (this.value / 2000)) * 2000).toFixed(2);
//I don't really care enough to change this
dotRadius = 2 / (this.value / 500);
if (dotRadius > 4) {
dotRadius = 4;
}
document.getElementById("thickVal").innerHTML = scale;
num_max = Math.sqrt((centerX * centerX) + (centerY * centerY)) * scale;
timer = window.requestAnimationFrame(preDraw);
}
function drawCircle(color, x, y, radius) {
ctx.beginPath();
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.ellipse(x, y, radius, radius, 0, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
}
function isPrime(number) {
if (primeStorage[number] != undefined) {
return primeStorage[number];
}
//add up digits of number, if the digits mod 9 add up to 0, 3, or 6 it'll be divisible by something
//only saves time and works for larger numbers
if (number > 100) {
var digAdd = 0;
for (var char of ("" + number)) {
digAdd += +char;
}
if (digAdd % 3 == 0) {
primeStorage[number] = false;
return false;
}
}
var sqrtAmount = Math.sqrt(number);
//check for factor of 2
if (number % 2 == 0) {
primeStorage[number] = false;
return false;
}
//for loop does every odd number to reduce computation
//every composite has a factor <= to its square root, so we just have to go to that
for (var divider=3; divider<=sqrtAmount; divider+=2) {
//if there are any factors, not prime
if (number % divider == 0) {
primeStorage[number] = false;
return false;
}
}
//if still in the function, the number must be prime
primeStorage[number] = true;
return true;
}
</script>
<div id="mid">
<p>Change Scale: <input type="range" min="5" max="2000" value="5" class="slider" id="scaleRange"> (<span id="thickVal">1</span> numbers / pixel)</p>
<canvas id="canvas" width="640" height="480"></canvas>
</div>
</body>
</html>