Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. // 座標や範囲を配列で表すと見にくいのでクラスのようなものにします。
  2. function Point(x, y){ // 点
  3. this.x = x;
  4. this.y = y;
  5. }
  6. function Rect(bounds){ // 範囲 bounds:[left, top, right, bottom]
  7. this.left = bounds[0];
  8. this.top = bounds[1];
  9. this.right = bounds[2];
  10. this.bottom = bounds[3];
  11.  
  12. this.center = new Point((this.right + this.left) / 2,
  13. (this.top + this.bottom) / 2);
  14. this.width = this.right - this.left;
  15. this.height = this.top - this.bottom;
  16. }
  17.  
  18. // O1を中心とした半径R1の円と、
  19. // O2を中心とした半径R2の円をつなぐ線(2つのうちの片方)
  20. // の接点を取得してP1, P2に設定する。
  21. // 接線が描けない場合 false を返す。
  22. function getTangentPoint(O1, R1, O2, R2, P1, P2){
  23. //2円の中心の距離を d とする。
  24. var dx = O1.x - O2.x;
  25. var dy = O1.y - O2.y;
  26. var d = Math.sqrt(dx*dx + dy*dy);
  27.  
  28. // 2円が重なっているか、接しているか、包囲している
  29. if(d <= R1 + R2) return false;
  30.  
  31. var t = Math.acos((R1 + R2) / d);
  32.  
  33. //O1を原点とした場合、中心を結ぶ線の角度は
  34. var T = Math.atan2(O2.y - O1.y, O2.x - O1.x);
  35.  
  36. //従って
  37. P1.x = O1.x + R1 * Math.cos(T + t);
  38. P1.y = O1.y + R1 * Math.sin(T + t);
  39.  
  40. T += Math.PI; // O2を原点とした角度に直す
  41. P2.x = O2.x + R2 * Math.cos(T + t);
  42. P2.y = O2.y + R2 * Math.sin(T + t);
  43.  
  44. // もう片方の接線の接点は -t とすると求められます。
  45.  
  46. return true;
  47. }
  48.  
  49. // メイン処理
  50. function main(){
  51. // 選択オブジェクトを取得
  52. // 選択物はパスか?などの確認は省略
  53. var sel = activeDocument.selection;
  54. var C1 = sel[0]; // 円1
  55. var C2 = sel[1]; // 円2
  56.  
  57. // それぞれを囲む範囲の情報を取得
  58. var C1_rect = new Rect(C1.geometricBounds);
  59. var C2_rect = new Rect(C2.geometricBounds);
  60.  
  61. var O1 = C1_rect.center; // 円1の中心
  62. var R1 = C1_rect.width / 2; // 円1の半径
  63.  
  64. var O2 = C2_rect.center;
  65. var R2 = C2_rect.width / 2;
  66.  
  67. var P1 = new Point(); // 接点1
  68. var P2 = new Point(); // 接点2
  69.  
  70. // result: 接線が描けない場合 false
  71. var result = getTangentPoint(O1, R1, O2, R2, P1, P2);
  72.  
  73. // 接線を作成
  74. if(result){
  75. var path = C1.duplicate(); // 属性を継承するために複製
  76. path.closed = false; // ただしクローズパスではない
  77. path.filled = false; // 一応塗りもリセットしておく
  78. path.setEntirePath([[P1.x, P1.y],[P2.x, P2.y]]);
  79. } else {
  80. alert("接線は描けません");
  81. }
  82. }
  83. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement