Public
Edited
Dec 23, 2023
Insert cell
md`# Cannot cut polygon with MultiLine #159`
Insert cell
Insert cell
data = {
const { Segment, Multiline, Polygon, point } = Flatten;
const polygon = new Polygon([
point(20, 20),
point(60, 20),
point(60, 60),
point(20, 60)
]);

const segment = new Segment(point(20, 20), point(40, 40));
const ip1 = segment.intersect(polygon);
const ip_sorted1 = segment.sortPoints(ip1);

const segment2 = new Segment(point(40, 40), point(50, 40));
const ip2 = segment2.intersect(polygon);
const ip_sorted2 = segment2.sortPoints(ip2);

const segment3 = new Segment(point(50, 40), point(60, 60));
const ip3 = segment3.intersect(polygon);
const ip_sorted3 = segment3.sortPoints(ip3);

const multiLine = new Multiline([segment, segment2, segment3]).split([
...ip_sorted1,
...ip_sorted2,
...ip_sorted3
]);

debugger;
// const polygonParts = polygon.cut(multiLine);

return [polygon, multiLine];
}
Insert cell
transformed_data = {
let m = data2center(data,width,height);
let transformed_data = data.map(shape => shape.transform(m))
return transformed_data;
}
Insert cell
{
let p = new Flatten.Polygon([[1,1],[1,2],[2,2],[2,1],[1,1]])
p.isValid()
return p
}
Insert cell
Insert cell
stageBox = new Flatten.Box(0, 0, width, height)
Insert cell
height = 400
Insert cell
function data2center(data, width, height) {
let {matrix} = Flatten;
let box = data.reduce( (acc,shape) => acc = acc.merge(shape.box), new Flatten.Box() )
let data_width = box.xmax - box.xmin || 400;
let data_height = box.ymax - box.ymin || 400;
let k = Math.min( width / (1.1*data_width), height / (1.1*data_height) );
let tx = width/2 - k*(box.xmin + box.xmax)/2;
let ty = height/2 - k*(box.ymin + box.ymax)/2;
let m = matrix().translate(tx,ty).scale(k,k)
return m;
}
Insert cell
Flatten = require("@flatten-js/core")
Insert cell
d3 = require("d3")
Insert cell
// Flatten = {
// const response = await fetch(
// "https://raw.githubusercontent.com/alexbol99/flatten-js/svg_attributes/dist/main.umd.js"
// );
// const blob = await response.blob();
// return require(URL.createObjectURL(blob)).catch(() => window.flatten);
// }
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more