Skip to content

Commit

Permalink
Merge pull request #341 from adracea/patch-1
Browse files Browse the repository at this point in the history
Fix polygon_drawing_to_walls.js for V11
  • Loading branch information
AnthonyVadala authored Dec 27, 2023
2 parents 003e6fd + 8693a05 commit e8a67ef
Showing 1 changed file with 41 additions and 46 deletions.
87 changes: 41 additions & 46 deletions misc/polygon_drawing_to_walls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,44 @@
* @Author: cole#9640
*/

function toRadians(degrees)
{
var pi = Math.PI;
return degrees * (pi/180);
}


let drawings = canvas.drawings.controlled;

drawings = drawings.filter(drawing => {
if (!drawing.isPolygon) {
ui.notifications.warn(`Drawing "${drawing.data._id}" is not a polygon, skipping`);
return false;
}
return true;
});

if (drawings.length) {
const newWalls = drawings.flatMap((drawing) => {
const { x, y, width, height } = drawing.data;
const xCenterOffset = width/2;
const yCenterOffset = height/2;

const θ = toRadians(drawing.data.rotation);
const cosθ = Math.cos(θ);
const sinθ = Math.sin(θ);

const points = drawing.data.points.map((point) => {
const offsetX = point[0] - xCenterOffset;
const offsetY = point[1] - yCenterOffset;
const rotatedX = (offsetX * cosθ - offsetY * sinθ);
const rotatedY = (offsetY * cosθ + offsetX * sinθ);
return [rotatedX + x + xCenterOffset, rotatedY + y + yCenterOffset];
});

return points.slice(0, points.length - 1)
.map((point, i) => {
return { c: point.concat(points[i + 1]) };
});
});

canvas.scene.createEmbeddedDocuments("Wall", newWalls);
canvas.walls.activate();
} else {
ui.notifications.error("No polygon drawings selected!");
}
let drawings = canvas.drawings.controlled;

drawings = drawings.filter(drawing => {
if (!drawing.isPolygon) {
ui.notifications.warn(`Drawing "${drawing.document._id}" is not a polygon, skipping`);
return false;
}
return true;
});

if (drawings.length) {
const newWalls = drawings.flatMap((drawing) => {
const { x, y } = drawing.document;
const { width, height } = drawing.document.shape;
const xCenterOffset = width / 2;
const yCenterOffset = height / 2;

const o = Math.toRadians(drawing.document.rotation);
const coso = Math.cos(o);
const sino = Math.sin(o);

pts = drawing.document.shape.points;
points = [];
for (i = 0; i < pts.length - 1; i += 2) {
const offsetX = pts[i] - xCenterOffset;
const offsetY = pts[i + 1] - yCenterOffset;
const rotatedX = (offsetX * coso - offsetY * sino);
const rotatedY = (offsetY * coso + offsetX * sino);
points.push([rotatedX + x + xCenterOffset, rotatedY + y + yCenterOffset]);
}
return points.slice(0, points.length - 1)
.map((point, i) => {
return { c: point.concat(points[i + 1]) };
});
});

canvas.scene.createEmbeddedDocuments("Wall", newWalls).then(as => { canvas.walls.activate(); });

} else {
ui.notifications.error("No polygon drawings selected!");
}

0 comments on commit e8a67ef

Please sign in to comment.