# Flattening 3d triangle

• Hello, flattening 3d triangle is very useful for all kinds of projects, and it's not available at this moment on V2, so here's a little function that does it. It comes from this discussion : https://stackoverflow.com/a/8051489

``````function d2ize(p){
var x0 = p, y0 = p, z0 = p,
x1 = p, y1 = p, z1 = p,
x2 = p, y2 = p, z2 = p

var X0 = 0, Y0 = 0
var X1 = Math.sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0) + (z1 - z0)*(z1 - z0)),
Y1 = 0
var X2 = ((x1 - x0) * (x2 - x0) + (y1 - y0) * (y2 - y0) + (z1 - z0) * (z2 - z0)) / X1,
Y2 = Math.sqrt((x2 - x0)*(x2 - x0) + (y2 - y0)*(y2 - y0) + (z2 - z0)*(z2 - z0) - X2*X2)

return [[X0, Y0], [X1, Y1], [X2, Y2]]

}
``````

• Thank you. I only work with triangulated models for coding simplicity, even if later (for designs clarity) I usually remove walls between co-planar neighbours. I will certainly soon start to make V2 versions of my code, was only missing this, but for the moment I'm making a vanilla JS pattern editor.

• Cool. But be careful as the boolean operations can create polygons with more than three (3) vertices. Also, some of the primitives create polygons with four (4) vertices.