jw_cad 外部変形 - (925) jscriptで円弧の端点に矢印を描く -

外部変形は データのやり取りをテキストファイルで行うので プログラム言語は 自由に選ぶことができます。図形は機能的かつシンプルなため、数多くのユーザーに受け入れられています。

 

jscriptで円弧の端点に矢印を描く

/* jscriptで円弧の端点に矢印を描く
@cls & echo off
copy jwc_temp.txt myfiles > nul
cscript //nologo //e:jscript %0 < myfiles > jwc_temp.txt
goto:eof

REM #jww
REM #1ci 円・円弧を指示してください
REM #bz
REM #e
*/

function cipoint(ci, pm) {
  x = ci[0]
  y = ci[1]
  r = ci[2]
  p1 = ci[3]
  p2 = ci[4]
  w = ci[5]
  d = ci[6] * Math.PI / 180
  co = Math.cos(d)
  si = Math.sin(d)
  if (pm == -1) { q = p1 } else { q = p2 }
  q *= Math.PI / 180.0
  xr = r * Math.cos(q)
  yr = r * Math.sin(q) * w
  a = Math.atan2(yr / w, xr * w) + d
  return [[x + xr * co - yr * si, y + xr * si + yr * co], a]
}
function hypot(x, y) {
  return Math.sqrt(x * x + y * y)
}
function lnslope(x) { //線の角度を返す
  return Math.atan2(x[3] - x[1], x[2] - x[0])
}
function ptdist(pt1, pt2) {
  return hypot(pt2[0] - pt1[0], pt2[1] - pt1[1])
}
function polarto(pt, r, d) {
  return [pt[0] + r * Math.cos(d), pt[1] + r * Math.sin(d)]
}
function arrow(pt1, d, size, xang, xan2) {
  with (Math) {
    l = size
    d *= PI / 180
    a = xang * PI / 180 / 2.0
    pt2 = polarto(pt1, l, d - a)
    pt4 = polarto(pt1, l, d + a)
    if (xan2 == 180) {
      sl = ["sl", pt1, pt2, pt4].join(" ").replace(/,/g, " ")
    } else {
      c = cos(a) - sin(a) * tan( (90.0 - xan2 / 2.0) * PI / 180)
      pt3 = polarto(pt1, l * c, d)
      sl = ["sl", pt1, pt2, pt3, pt4].join(" ").replace(/,/g, " ")
    }
  }
  return sl
}

size = 5   //矢印の長さ( 図寸 )
xang = 45  //矢印の交角( ゚ )
xan2 = 120 //矢尻の交角( ゚ )

f = WScript.StdIn
while (! f.AtEndOfStream) {
  F = ($_ = f.ReadLine()).split(/\s+/)
  if (/^hp1/i .test($_)) {
    if (hp1 = F.slice(1, 3)) for (i = 0; i < hp1.length; i++) hp1[i] *= 1.0;
  }
  if (/^ci/i .test($_)) { //ci 指示円
    for (i = 1; i < F.length; i++) F[i] *= 1.0;
    ci1 = F.slice(1, F.length)
    if (F.length == 4) {
      ci1 = ci1.concat(0, 360, 1, 0)
    }
  }
}
with (Math) {
  pta1 = cipoint(ci1, -1)
    pt1 = pta1[0]
    a1 = pta1[1]
  pta2 = cipoint(ci1, 1)
    pt2 = pta2[0]
    a2 = pta2[1]
  l1 = ptdist(hp1, pt1)
  l2 = ptdist(hp1, pt2)
  pm = l1 <= l2 ? -1 : 1
  pt = pm == 1 ? pt2 : pt1
  d = (pm == 1 ? a2 : a1) + PI / 2
}
g = WScript.StdOut
g.WriteLine("bz")
g.WriteLine(arrow(pt, d * 180 / Math.PI, -size*pm, xang, xan2))