jw_cad 外部変形 - (1278) luaで線の複線を引く -

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

 

luaで線の複線を引く

:luaで線の複線を引く
@echo off
for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
  copy jwc_temp.txt myfiles > nul
  more +%%n %0 | lua > jwc_temp.txt
)
goto:eof

REM #jww
REM #1ln 線を指示してください。
REM #2 基準点を指示してください。
REM #bz
REM #e

#!この次の行からプログラムを書いてください
function join(a, FS)
  if type(a) ~= "table" then return a end
  if FS == nil then FS = " " end
  return table.concat(flatten(a), FS)
end

function flatten(a)
  local i, j, k
  local c = {}
  for i in pairs(a) do
    if type(a[i]) == "table" then
      --while #a[i] == 1 do a[i] = a[i][1] end
      for j in pairs(a[i]) do
        if type(a[i][j]) == "table" then
          --while #a[i][j] == 1 do a[i][j] = a[i][j][1] end
          for k in pairs(a[i][j]) do
            table.insert(c, a[i][j][k])
          end
        else
          table.insert(c, a[i][j])
        end
      end
    else
      table.insert(c, a[i])
    end
  end
  return c
end

function echo(...)
  if #{...} == 0 then return end
  return print(join({select(1, ...)}))
end

function lnlength(s) --線長を返す
  local x1, y1, x2, y2 = s[1], s[2], s[3], s[4]
  return math.sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
end

function lnslope(s) --線の角度を返す
  local x1, y1, x2, y2 = s[1], s[2], s[3], s[4]
  return math.atan2(y2 - y1, x2 - x1)
end

function ln_hpdist(s, p) --線と点の最短距離を返す
  local x = {p[1], p[2], s[1], s[2]}
  return lnlength(x) * math.sin(lnslope(s) - lnslope(x))
end

function lnmove(s, b, pmy) --線 ln を直交距離 b だけ pmy の方向に平行移動する
  if pmy == nil then pmy = 1 end
  local x1, y1, x2, y2 = s[1], s[2], s[3], s[4]
  local d  = lnslope(s)
  local co = b * math.cos(d) * pmy
  local si = b * math.sin(d) * pmy
  return {x1 - si, y1 + co, x2 - si, y2 + co}
end

for line in io.lines("myfiles") do
  F = {}
  string.gsub(line, "%S+", function (x) table.insert(F, x) end)
  if line:match("^hq") then print("bz") end
  if line:match("^hp2") then hp2 = {F[2], F[3]} end
  if line:match("^ ") then
    echo(lnmove(F, ln_hpdist(F, hp2)))
  end
end