Published
Edited
Aug 29, 2020
10 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function gappy_circle([xc, yc], r)
{
const pixel = (x,y) => [x,y].map(Math.round);
const pixels = [pixel(xc, yc+r), pixel(xc, yc-r)];
for(let i = 1; i <= r; ++i)
{
// Four pixels: [xc+i, y+sqrt], [xc+i, y-sqrt], and the xc-i versions of same
let sqrt_disc = Math.sqrt(r**2 - i**2);
pixels.splice(pixels.length, 0, ...[pixel(xc+i, yc+sqrt_disc),
pixel(xc+i, yc-sqrt_disc),
pixel(xc-i, yc+sqrt_disc),
pixel(xc-i, yc-sqrt_disc)]);
}
return pixels;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function midpoint_circle([xc, yc], r)
{
const pixel = (x,y) => [x,y].map(Math.round);
// Initial condition
let [ic, jc] = pixel(xc, yc);
r = Math.round(r);
let [i,j] = [0, r];
// Find the error
let d = 1 - r;
// Rasterize by octant; start w/ four cardinal points
const pixels = [pixel(ic, jc + r),
pixel(ic, jc - r),
pixel(ic + r, jc),
pixel(ic - r, jc)];
while(i < j)
{
// Update midpoint/error term
if(d >= 0)
{
j -= 1;
d -= 2 * j;
}
i += 1;
d += 2 * i + 1;

pixels.splice(pixels.length, 0, ...[pixel(ic + i, jc + j),
pixel(ic + i, jc - j),
pixel(ic - i, jc + j),
pixel(ic - i, jc - j),
pixel(ic + j, jc + i),
pixel(ic + j, jc - i),
pixel(ic - j, jc + i),
pixel(ic - j, jc - i)]);
}
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

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