Public
Edited
Aug 24, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
otherSystems
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
otherSystems = {
const data = [];

tArray.map((t) => {
const light = originSystem.find((d) => (d.name === "light") & (d.t === t)),
grids = originSystem.filter((d) => d.type === "grid"); // & (d.t === t));

system_vs.map((z) => {
// ---------------------------------------------------
// Select records
const system = originSystem.find(
(d) => (d.name === z.name) & (d.t === t)
),
translate = (x, t) => {
const newT = system.length,
newX = 0,
diffT = newT - system.t2,
diffX = newX - system.x2;
return { x: x + diffX, t: t + diffT };
};

// ---------------------------------------------------
// System and light
{
const d1 = translate(system.x2, system.t2),
d2 = translate(light.x2, light.t2),
{ name, ratio } = system;

data.push({
x1: 0,
t1: 0,
x2: d1.x,
t2: d1.t,
t,
name,
type: "system",
ratio
});

data.push({
x1: 0,
t1: 0,
x2: d2.x - (correctOffset ? t * speedOfLight : 0),
t2: d2.t,
t,
name,
type: "light",
ratio
});

if (z.name === "system-v") {
const d4 = translate(-light.x2, light.t2);
data.push({
x1: 0,
t1: 0,
x2: d4.x - (correctOffset ? t * speedOfLight : 0),
t2: d4.t,
t,
name,
type: "light",
ratio
});
}
}

// ---------------------------------------------------
// Grids
if (z.name === "system-v") {
grids.map((grid) => {
const d2 = translate(grid.x2, grid.t2),
ruleLight = data.filter(
(d) =>
(d.type === "light") & (d.name === "system-v") & (d.t === t)
),
{ name, ratio } = grid;

// console.log(grid, ruleLight);

// const t2Scale = d3
// .scaleLinear()
// .domain([-1, 1])
// .range(ruleLight.map((d) => d.t2)),
// x2Scale = d3
// .scaleLinear()
// .domain([-1, 1])
// .range(ruleLight.map((d) => d.x2));

data.push({
x1: 0,
t1: 0,
// x2: x2Scale(grid.lightScaleValue), //d2.x - (correctOffset ? t * speedOfLight : 0),
// t2: t2Scale(grid.lightScaleValue), //d2.t,
x2: d2.x - (correctOffset ? t * speedOfLight : 0),
t2: d2.t,
t,
name,
type: "grid",
ratio
});
});
}
});
});

return data;
}
Insert cell
originSystem
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
originSystem = {
const data = [],
c = speedOfLight;

tArray.map((t) => {
// Light
{
const x1 = 0,
t1 = 0,
x2 = c * t,
t2 = t,
name = "light";

data.push({ x1, t1, x2, t2, name, t, ratio: 1.0 });
}

// System2
{
function addSystem(v, name, ratio) {
const x1 = 0,
t1 = 0,
x2 = v * t,
t2 = t;

data.push({ x1, t1, x2, t2, name, t, ratio });
}

system_vs.map((d) => addSystem(d.v, d.name, d.ratio));
}

// Grid
{
grids
.filter((d) => d.t === t)
.map((d) => {
data.push(Object.assign({}, d));
});
}
});

data.map((d) => {
const length = distance([d.x1, d.t1], [d.x2, d.t2]);
Object.assign(d, { length, type: d.name.split("-")[0] });
});

return data;
}
Insert cell
grids = {
const data = [],
xRange = [-0.5, 3],
tRange = tArray,
n = tRange.length,
names = [];

// Left edge, bottom up
tArray.map((_, i) => {
names.push([i, [0, i]]);
});

// Right edge, top down
tArray.map((_, i) => {
names.push([i + n, [1, n - i - 1]]);
});

// Rename the nodes,
// and put other attrs.
var name,
x2,
t2,
t,
ratio,
x1 = 0,
t1 = 0,
lightScale,
lightScaleValue;

names.map((d) => {
name = "grid-" + d[0];
x2 = xRange[d[1][0]];
t2 = tRange[d[1][1]];
t = t2;
ratio = x2 / t2;

lightScale = d3
.scaleLinear()
.domain([-1, 1])
.range([-t * speedOfLight, t * speedOfLight]);
lightScaleValue = lightScale.invert(x2);

data.push({ x1, t1, x2, t2, name, t, ratio, lightScaleValue });
});

return data;
}
Insert cell
grids
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
system_vs
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
/**
* Distance between two points,
* a: [a.x, a.y]
* b: [b.x, b.y]
*/
distance = (a = [0, 0], b = [1, 1]) => {
return Math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2);
}
Insert cell
d3 = require("d3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more