jw_cad 外部変形 - (1283) 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 #1-%d 中心を指示してください
REM #2%d 中心線上の外周点を指示してください
REM #e

#!この次の行からプログラムを書いてください
function f(a, p)
  return a * math.cos(p) * math.sqrt(1.0 - 0.5 * math.sin(p) ^ 2)
end

function g(a, p)
  return a / math.sqrt(2.0) * math.sin(p) * math.cos(p)
end

function h(n)
  x1, y1, x2, y2 = hp[1][1], hp[1][2], hp[2][1], hp[2][2]
  d = math.atan2(y2 - y1, x2 - x1) + math.pi / 2
  si = math.sin(d)
  co = math.cos(d)
  a = math.sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
  p = 2 * math.pi / n
  x = f(a, 0)
  y = g(a, 0)
  xa =  x * si + y * co + x1
  ya = -x * co + y * si + y1
  i = 1
  while i ~= n + 1 do
    x = f(a, i * p)
    y = g(a, i * p)
    xb =  x * si + y * co + x1
    yb = -x * co + y * si + y1
    print(string.format("%s %s %s %s", xa, ya, xb, yb))
    xa = xb
    ya = yb
    i = i + 1
  end
end

hp = {}
for line in io.lines("myfiles") do
  F = {}
  string.gsub(line, "%S+", function (x) table.insert(F, x) end)
  if line:match("^hp[1-9]") then
    hp[F[1]:gsub("hp", ""):gsub("-", "")*1] = {F[2], F[3]}
  end
end
h(100)