#target illustrator
function diagonalRandomSplit() {
var doc = app.activeDocument;
if (doc.selection.length !== 1) {
alert("1つのオブジェクトを選択してください。");
return;
}
var obj = doc.selection[0];
var bounds = obj.geometricBounds;
var width = bounds[2] - bounds[0];
var height = bounds[1] - bounds[3];
var splitCount =
parseInt(prompt("分割数を入力してください(例: 5)", "5"), 10);
if (isNaN(splitCount) || splitCount < 1) {
alert("有効な分割数を入力してください。");
return;
}
var newObjects = [obj];
for (var i = 0; i < splitCount; i++) {
var tempObjects = [];
for (var j = 0; j < newObjects.length; j++) {
var item = newObjects[j];
var itemBounds = item.geometricBounds;
var x1 = itemBounds[0], y1 = itemBounds[1];
var x2 = itemBounds[2], y2 = itemBounds[3];
if ((x2 - x1 > 10) && (y1 - y2 > 10)) {
// 斜めに分割
var randX = x1 + (Math.random() * 0.6 + 0.2) * (x2 - x1);
var randY = y2 + (Math.random() * 0.6 + 0.2) * (y1 - y2);
var newLine = doc.pathItems.add();
newLine.setEntirePath([[x1, y1], [randX, randY]]);
newLine.stroked = true;
newLine.strokeWidth = 1;
tempObjects.push(item);
} else {
tempObjects.push(item);
}
}
newObjects = tempObjects;
}
}
diagonalRandomSplit();