jw_cad 外部変形 - (804) rubyで線の端点に矢印を描く -

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

 

rubyで線の端点に矢印を描く

:rubyで線の端点に矢印を描く
@echo off
ruby -x %0 jwc_temp.txt
goto:eof

REM #jww
REM #1ln 線を指示してください
REM #bz
REM #e

#!ruby -Ks -an -i.bak
def ptdist(pt1, pt2)
  return hypot(pt2[0] - pt1[0], pt2[1] - pt1[1])
end
def lnslope(ln) #線の角度を返す
  return atan2(ln[3] - ln[1], ln[2] - ln[0])
end
def polarto(pt, r, d)
  return [pt[0] + r * cos(d), pt[1] + r * sin(d)]
end
def arrow(pt1, d, pm, size = 3, xang = 60, xan2 = 90)
  l = -size * pm
  a = xang * PI / 180 / 2.0
  pt2 = polarto(pt1, l, d - a)
  pt4 = polarto(pt1, l, d + a)
  if xan2 == 180
    sl = pt1 + pt2 + pt4
  else
    c = cos(a) - sin(a) * tan((90.0 - xan2 / 2.0) * PI / 180)
    pt3 = polarto(pt1, l * c, d)
    sl = pt1 + pt2 + pt3 + pt4
  end
  puts "sl" + " %s" * sl.size % sl
end
BEGIN { include Math }
case $_
when /^hp1ln/
  pt = $F[1..2].map { |x| x.to_f }
when /^[ ]/
  if $F.size == 4
    x1, y1, x2, y2 = $F.map { |x| x.to_f }
    size = 5 #矢印の長さ( 図寸 )
    xang = 45 #矢印の交角( ゚ )
    xan2 = 120 #矢尻の交角( ゚ )
    d = lnslope([x1, y1, x2, y2])
    l1 = ptdist(pt, [x1, y1])
    l2 = ptdist(pt, [x2, y2])
    pm = l1 <= l2 ? -1 : 1
    puts "bz"
    arrow((pm == 1 ? [x2, y2] : [x1, y1]), d, pm, size, xang, xan2)
  end
end
__END__