Skip to content

Instantly share code, notes, and snippets.

@shspage
Created April 9, 2017 08:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save shspage/9529a56bb7b2598a12f3cebe42e160d2 to your computer and use it in GitHub Desktop.
Illustrator:最背面のパス(円)に関して残りの選択パスのアンカーポイントを反転変換した座標に移動する
// 最背面のパス(円)に関して残りの選択パスの
// アンカーポイントを反転変換した座標に移動する
// --------------------------------------
var Point = function(x, y){
this.x = x;
this.y = y;
}
Point.prototype = {
// anc : PathPoint.anchor
byAnchor : function(anc){
this.x = anc[0],
this.y = anc[1];
return this;
}
}
// --------------------------------------
// 半径 r, 中心 o の円に関して点 p を反転変換する
// r2: float, 半径 r の2乗
// o, p: Point
function inverse(o, r2, p){
var d2 = dist2(o, p);
if(d2 == 0){
return Infinity;
}
var m = r2 / d2;
var q = new Point((p.x - o.x) * m + o.x,
(p.y - o.y) * m + o.y);
return q;
}
// --------------------------------------
// Point p1, p2 の間の距離の2乗を返す
function dist2(p1, p2) {
var dx = p1.x - p2.x;
var dy = p1.y - p2.y;
return dx*dx + dy*dy;
}
// --------------------------------------
// PageItem item の中心の座標を返す
function getCenter(item){
var gb = item.geometricBounds; // [left, top, right, bottom]
return new Point((gb[0] + gb[2]) / 2,
(gb[1] + gb[3]) / 2);
}
// --------------------------------------
// PathPoint p の位置を Point p に設定する
function setPoint(pt, p){
pt.anchor = [p.x, p.y];
pt.rightDirection = pt.anchor;
pt.leftDirection = pt.anchor;
}
// --------------------------------------
function main(){
// 選択範囲を取得。最背面は反転に使用する円
// 残りは PathItem とする
var sel = activeDocument.selection;
var circle = sel.pop();
var o = getCenter(circle);
var r = circle.width / 2;
var r2 = r * r;
for(var si = 0; si < sel.length; si++){
var pp = sel[si].pathPoints;
for(var i = 0; i < pp.length; i++){
var q = inverse(o, r2, new Point().byAnchor(pp[i].anchor));
if(q != Infinity){ // 無限遠は無視
setPoint(pp[i], q);
}
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment