Published
Edited
Jun 14, 2020
Importers
Insert cell
Insert cell
function intersectionArea(poly1, poly2) {
const f32 = new Float32Array(pia.instance.exports.memory.buffer);
let i = 0;
for (let j = 0; j < poly1.length; j++) {
f32[i++] = poly1[j];
}
for (let j = 0; j < poly2.length; j++) {
f32[i++] = poly2[j];
}

const startTime = Date.now();
const result = pia.instance.exports.pia_area(pia.instance.exports.memory.buffer, poly1.length / 2, poly2.length / 2);
const time = Date.now() - startTime;
if (time > 25) {
console.log(poly1, poly2, time);
}
console.log(time);
return result;
}
Insert cell
function assert(a, b) {
if (a != b) throw new Error(`Expected ${a} to equal ${b}`);
}
Insert cell
{
// Unit tests
assert(intersectionArea([2, 3, 2, 3, 2, 3, 2, 4, 3, 3, 2, 3, 2, 3], [2, 3, 2, 3, 2, 3, 2, 4, 3, 3, 2, 3, 2, 3]), 0.5);
assert(intersectionArea([2, 3, 2, 3, 2, 3, 2, 4, 3, 3, 2, 3, 2, 3], [1, 1, 1, 4, 4, 4, 4, 1, 1, 1]), 0.5);
assert(intersectionArea([1, 7, 4, 7, 4, 6, 2, 6, 2, 3, 4, 3, 4, 2, 1, 2], [3, 1, 5, 1, 5, 4, 3, 4, 3, 5, 6, 5, 6, 0, 3, 0]), 0);
assert(intersectionArea([1, 1, 1, 2, 2, 1, 2, 2], [0, 0, 0, 4, 4, 4, 4, 0]), 0);
assert(intersectionArea([0, 0, 3, 0, 3, 2, 1, 2, 1, 1, 2, 1, 2, 3, 0, 3], [0, 0, 0, 4, 4, 4, 4, 0]), -9);
assert(intersectionArea([0, 0, 1, 0, 0, 1], [0, 0, 0, 1, 1, 1, 1, 0]), -0.5);
assert(intersectionArea([1, 3, 2, 3, 2, 0, 1, 0], [0, 1, 3, 1, 3, 2, 0, 2]), -1);
assert(intersectionArea([0, 0, 0, 2, 2, 2, 2, 0], [1, 1, 3, 1, 3, 3, 1, 3]), -1);
assert(intersectionArea([0, 0, 0, 4, 4, 4, 4, 0], [1, 1, 1, 2, 2, 2, 2, 1]), 1);
assert(intersectionArea([0, 0, 3, 3], [0, 0, 3, 3]), 0);
assert(intersectionArea([0, 0, 3, 3], [1, 1, 1, 2, 2, 2, 2, 1]), 0);
assert(intersectionArea([1, 1, 1, 2, 2, 2, 2, 1], [0, 0, 3, 3]), 0);
return 'PASS';
}
Insert cell
pia = {
const bytes = await FileAttachment('pia@4.wasm').arrayBuffer();
return await WebAssembly.instantiate(bytes);
}
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