jw_cad 外部変形 - (1265) luaで2点間の勾配を計算する(ptslope) -

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

 

luaで2点間の勾配を計算する(ptslope)

:luaで2点間の勾配を計算する(ptslope)
@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 #1-%d 始点を指示してください
REM #2%d 終点を指示してください
REM #e

#!この次の行からプログラムを書いてください
function ptslope(x, y)
  local x1, y1 = x[1], x[2]
  local x2, y2 = y[1], y[2]
  return math.atan2(y2 - y1, x2 - x1)
end

function sprintf(...)
  if #{...} == 0 then return end
  if #{...} == 1 then return ... end
  return string.format(...)
end

function printf(...)
  return print(sprintf(...))
end

for line in io.lines("myfiles") do
  F = {}
  string.gsub(line, "%S+", function (x) table.insert(F, x) end)
  if line:match("^hk") then hk = F[2] end
  if string.match(line, "^hp1") then x1, y1 = F[2], F[3] end
  if string.match(line, "^hp2") then x2, y2 = F[2], F[3] end
end
printf("h#2点間の勾配∠ = %.03f゚", math.deg(ptslope({x1, y1}, {x2, y2})) - hk)