Public
Edited
Sep 9, 2021
Importers
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable dda_points = dda(mutable start, mutable end)
Insert cell
function dda([x0, y0], [x1, y1]) {
// Round to determine pixel location
const pixel = (x, y) => [Math.round(x), Math.round(y)];
// Differences and step value
let [dx, dy] = [x1 - x0, y1 - y0];
let step = 0;
if(Math.abs(dx) > Math.abs(dy))
step = Math.abs(dx);
else
step = Math.abs(dy);
// Find slope and stepping direction
dx /= step; dy /= step;
// Generate
let [x, y] = [x0, y0];
const pixels = [pixel(x, y)];
for(let i = 0; i < step; ++i)
{
x += dx; y += dy;
pixels.push(pixel(x,y));
}
return pixels;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function midpoint([x0, y0], [x1, y1])
{
// Round to determine pixel location
const pixel = (x,y) => [Math.round(x), Math.round(y)]
let [i0, j0] = pixel(x0,y0);
let [i1, j1] = pixel(x1,y1);
let [i,j] = [i0,j0];

// Steps
let stepi = i1 > i0 ? 1 : i1 < i0? -1 : 0;
let stepj = j1 > j0 ? 1 : j1 < j0? -1 : 0;

// Determine error
let di = Math.abs(i1 - i0);
let dj = Math.abs(j1 - j0);
// Find line orientation
let steep = false;
if(dj > di)
{
steep = true;
[i,j] = [j,i];
[di,dj] = [dj,di];
[stepi,stepj] = [stepj,stepi];
}
let d = di - (2 * dj);

// Rasterize
const pixels = [];
const push_pixel = (i,j,steep) => steep? pixels.push([j,i]) : pixels.push([i,j]);
push_pixel(i,j,steep);
for(let count = 1; count <= di; ++count)
{
// Update midpoint/error term
if(d <= 0)
{
j += stepj;
d += 2 * di;
}
i += stepi;
d -= 2 * dj;
push_pixel(i,j,steep);
}
return pixels;
}
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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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