-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.js
More file actions
224 lines (198 loc) · 5.79 KB
/
screen.js
File metadata and controls
224 lines (198 loc) · 5.79 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//collect all update functions in a single call
function update(newVal = "")
{
updateCoord();
if(newVal != "")
{
document.getElementById("ref").value = newVal;
redraw();
relist();
refUpdate += 1;
}
else
{
redraw();
relist();
}
}
/*
relist functionality
*/
//update the tickmarks
function updateCoord()
{
document.getElementById("xstep").value = expRound(Math.pow(10, Math.round(Math.log(
scaleIn("x", xMax - xMin) / 2) / Math.log(10)) - 1));
document.getElementById("tstep").value = expRound(Math.pow(10, Math.round(Math.log(
scaleIn("t", tMax - tMin) / 2) / Math.log(10)) - 1));
xStep = parseFloat(document.getElementById("xstep").value);
tStep = parseFloat(document.getElementById("tstep").value);
}
//re-list numbers in the reference frame list
function relist()
{
var out = "<pre> # + x"+" ".repeat(numDec + 5)+" t"+" ".repeat(numDec + 5)+" v<br>";
for(var i = 0; i < states.length; i++)
{
out += (Math.abs(states[i][0]) < TOL && Math.abs(states[i][1]) < TOL &&
Math.abs(states[i][2]) < TOL ? "> " : " ") + (i + 1) + " " +
(ticks[i] ? "#" : "-") + " " + numForm("x", states[i][0]) + " " +
numForm("t", states[i][1]) + " " + numForm("v", states[i][2]) + "<br>";
}
document.getElementById("states").innerHTML = out + "</pre>";
}
//format number for printing
function numForm(mode, num)
{
var out;
if(Math.abs(num) > TOL)
out = expRound(scaleIn(mode, num));
else
out = "0e0";
var front = parseFloat(out.substring(0, out.indexOf("e"))).toFixed(numDec);
front = (parseFloat(front) < 0 ? "" : " ") + front;
var exp = parseInt(out.substring(out.indexOf("e") + 1));
//catch rollover in toFixed
if(front.indexOf(".") > 2)
{
front = (parseFloat(front) / 10).toFixed(numDec);
front = (parseFloat(front) < 0 ? "" : " ") + front;
exp += 1;
}
return (front + "e" + exp + " ").substring(0, 7 + numDec);
}
/*
redraw functionality
*/
//redraw the canvas
function redraw()
{
ctx.clearRect(0, 0, canv.width, canv.height);
var oldLineWidth = ctx.lineWidth;
var oldStrokeStyle = ctx.strokeStyle
//redraw usual frames
for(var i = 0; i < states.length; i ++)
{
draw(states[i], true, true, true, false, ticks[i]);
}
drawLight([0, 0, 0]);
//load alternate settings for "special" (multiply-selected) frames
ctx.lineWidth = oldLineWidth * 4;
ctx.strokeStyle = "#0000FF";
for(var i = 0; i < special.length; i++)
{
draw(states[special[i]], true, true, true, false, ticks[special[i]]);
}
//load alternate settings for highlighted frame
ctx.lineWidth = oldLineWidth * 1.5;
ctx.strokeStyle = "#FF0000";
draw(states[parseInt(document.getElementById("ref").value) - 1], true, true, true, false,
ticks[parseInt(document.getElementById("ref").value) - 1]);
//reset draw settings
ctx.lineWidth = oldLineWidth;
ctx.strokeStyle = oldStrokeStyle;
}
//draw state with desired point, axes, light rays, and tick mark options
function draw(state, p = true, x = true, t = true, lt = false, tick = true)
{
if(p) drawP(state);
if(x) drawX(state, tick);
if(t) drawT(state, tick);
if(lt) drawLight(state);
}
//draw light rays through point (dashed lines)
function drawLight(state)
{
//top left to bottom right
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(xScale(xMin), tScale(state[1] - (xMin - state[0])));
ctx.lineTo(xScale(xMax), tScale(state[1] - (xMax - state[0])));
ctx.stroke();
//bottom left to top right
ctx.beginPath();
ctx.moveTo(xScale(xMin), tScale(state[1] + (xMin - state[0])));
ctx.lineTo(xScale(xMax), tScale(state[1] + (xMax - state[0])));
ctx.stroke();
ctx.setLineDash([]);
}
//draw point given coordinates
function drawP(state)
{
ctx.beginPath();
ctx.arc(xScale(state[0]), tScale(state[1]), PT_RAD, 0, 2 * Math.PI);
ctx.stroke();
}
//draw x-axis, with optional tick marks
function drawX(state, tick = true)
{
//axis
ctx.beginPath();
ctx.moveTo(xScale(xMin), tScale(state[1] + state[2] * (xMin - state[0])));
ctx.lineTo(xScale(xMax), tScale(state[1] + state[2] * (xMax - state[0])));
ctx.stroke();
//tick marks
if(tick)
{
var step = scaleOut("x", xStep) / Math.sqrt(1 - state[2] * state[2]);
for(var i = step * Math.ceil((xMin - state[0]) / step) + state[0], j = (xMax - i) / (xMax - xMin);
i <= xMax; i += step, j = (xMax - i) / (xMax - xMin))
{
drawTick(xScale(xMin * j + xMax * (1 - j)),
tScale(state[1] + state[2] * (xMin * j + xMax * (1 - j) - state[0])),
(state[2] == 0 ? 1 / TOL : 1 / state[2]));
}
}
}
//draw t-axis, with optional tick marks
function drawT(state, tick = true)
{
//axis
ctx.beginPath();
ctx.moveTo(xScale(state[0] + state[2] * (tMin - state[1])), tScale(tMin));
ctx.lineTo(xScale(state[0] + state[2] * (tMax - state[1])), tScale(tMax));
ctx.stroke();
//tick marks
if(tick)
{
var step = scaleOut("t", tStep) / Math.sqrt(1 - state[2] * state[2]);
for(var i = step * Math.ceil((tMin - state[1]) / step) + state[1], j = (tMax - i) / (tMax - tMin);
i <= tMax; i += step, j = (tMax - i) / (tMax - tMin))
{
drawTick(xScale(state[0] + state[2] * (tMin * j + tMax * (1 - j) - state[1])),
tScale(tMin * j + tMax * (1 - j)), state[2]);
}
}
}
//draw tick mark given location (in pixels) and slope
function drawTick(x, t, slp)
{
slp *= (tMax - tMin) / (xMax - xMin);
ctx.beginPath();
ctx.moveTo(x - COORD_RAD / Math.sqrt(1 + slp * slp), t - COORD_RAD * slp / Math.sqrt(1 + slp * slp));
ctx.lineTo(x + COORD_RAD / Math.sqrt(1 + slp * slp), t + COORD_RAD * slp / Math.sqrt(1 + slp * slp));
ctx.stroke();
}
/*
handle coordinate transforms
*/
//scale for x-axis
function xScale(coord)
{
return (DIM * (coord - xMin) / (xMax - xMin));
}
//undo x scale
function xUnscale(coord)
{
return coord * 1.0 / DIM * (xMax - xMin) + xMin;
}
//scale for t-axis
function tScale(coord)
{
return (DIM * (tMax - coord) / (tMax - tMin));
}
//undo t scale
function tUnscale(coord)
{
return tMax - coord * 1.0 / DIM * (tMax - tMin);
}