« Illustratro CS3/CS4、部分保存駄目っぽい | メイン | Illustrator CS3、選択した2本の直線を4本の線に分割 »
2009年12月17日
Illustrator CS3、2つの直線の交点座標に●を表示
とりあえず、勉強用(?)
どこか間違っていて駄目な時も(笑)
// 選択した2本の直線の交点に●を表示する
(function(){
var obj1 = {};
var obj2 = {};
var selObj = app.activeDocument.selection;
obj1.x1 = selObj[0].geometricBounds[0];
obj1.y1 = selObj[0].geometricBounds[1];
obj1.x2 = selObj[0].geometricBounds[2];
obj1.y2 = selObj[0].geometricBounds[3];
obj2.x1 = selObj[1].geometricBounds[0];
obj2.y1 = selObj[1].geometricBounds[1];
obj2.x2 = selObj[1].geometricBounds[2];
obj2.y2 = selObj[1].geometricBounds[3];
var cood = getCrossPoint(obj1, obj2);
if (cood) drawCircle(cood[0], cood[1]);
})();
// 2つの線分の交点を求める
// 以下のURLのを参考
// http://www.dango-itimi.com/blog/archives/2005/000755.html
function getCrossPoint(temp1, temp2){
var A = ( temp1.y2 - temp1.y1 )/( temp1.x2 - temp1.x1 );
var B = ( temp2.y2 - temp2.y1 )/( temp2.x2 - temp2.x1 );
var x,y ;
if( A == B ){ return null; } // 直行線か平行線
if( ( temp1.x1 != temp1.x2 ) && ( temp2.x1 != temp2.x2 ) ){
x = ( ( temp1.x1*A ) - temp1.y1 - ( temp2.x1*B ) + temp2.y1 )/( A-B );
y = A*( x-temp1.x1 ) + temp1.y1;
}else if( ( temp1.x1 == temp1.x2 ) && ( temp2.x1 != temp2.x2 ) ){
x = temp1.x1;
y = B*( x-temp2.x1 ) + temp2.y1;
}else if( ( temp1.x1 != temp1.x2 ) && ( temp2.x1 == temp2.x2 ) ){
x = temp2.x1;
y = A*( x-temp1.x1 ) + temp1.y1;
}
return [x, y];
}
// 指定座標を中心にして4ポイント×4ポイントの楕円形を描く
function drawCircle(x,y){
var ellipseObj = app.activeDocument.pathItems.ellipse(y+2, x-2, 4, 4, false, true);
ellipseObj.fillColor = setCMYKColor(0, 100, 0, 0);
ellipseObj.stroked = false;
}
// CMYKカラーを設定し、CMYKカラーオブジェクトを返す
function setCMYKColor(c,m,y,k){
var CMYK = new CMYKColor();
CMYK.cyan = c;
CMYK.magenta = m;
CMYK.yellow = y;
CMYK.black = k;
return CMYK;
}
投稿者 openspc : 2009年12月17日 17:18