Public
Edited
Sep 8, 2022
Importers
12 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function in_triangle(pt, vert_a, vert_b, vert_c)
{
let [alpha, beta, gamma] = calc_barycentric(pt, vert_a, vert_b, vert_c);
// zed is our error tolerance. Occasionally, points will be negative but pretty
// much zero and thus we are in the triangle. zed corrects for this. Higher
// precision floats or tiny tiny pixels avoid or ignore this
if(alpha >= zed && beta >= zed && gamma >= zed )
return true;
else
return false;
}
Insert cell
Insert cell
function fill_triangle(vert_a, vert_b, vert_c)
{
const pts = [];
let i_min = d3.min([vert_a, vert_b, vert_c], ([x,y]) => (Math.floor(x)));
let i_max = d3.max([vert_a, vert_b, vert_c], ([x,y]) => (Math.ceil(x)));
let j_min = d3.min([vert_a, vert_b, vert_c], ([x,y]) => (Math.floor(y)));
let j_max = d3.max([vert_a, vert_b, vert_c], ([x,y]) => (Math.ceil(y)));

for(let i = i_min; i <= i_max; ++i)
for(let j = j_min; j <= j_max; ++j)
if(in_triangle([i,j], vert_a, vert_b, vert_c)) pts.push([i,j]);
return pts;
}
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
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