jw_cad 外部変形 - (803) rubyでレムニスケートを描く -

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

 

rubyレムニスケートを描く

:rubyレムニスケートを描く
@echo off
ruby -x %0 jwc_temp.txt
goto:eof

REM #jww
REM #1-%d 中心点を指示してください
REM #2%d X軸上の点を指示してください
REM #e

#!ruby -Ks -an -i.bak
def f(a, p)
  return a * cos(p) * sqrt(1.0 - 0.5 * sin(p) ** 2)
end
def g(a, p)
  return a / sqrt(2.0) * sin(p) * cos(p)
end
def h(n)
  x1, y1, x2, y2 = $hp[1] + $hp[2]
  d = atan2(y2 - y1, x2 - x1) + PI / 2
  si = sin(d)
  co = cos(d)
  a = hypot(x2 - x1, y2 - y1)
  p = 2 * 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
    x = f(a, i * p)
    y = g(a, i * p)
    xb =  x * si + y * co + x1
    yb = -x * co + y * si + y1
    puts "%s %s %s %s" % [xa, ya, xb, yb]
    xa = xb
    ya = yb
    i += 1
  end
end
BEGIN {
  include Math
  $hp = []
}
case $_
when /^hp[1-9]/
  $hp[$F[0].gsub(/(hp|ln|ci|ch|#|-)/,"").to_i] = [$F[1].to_f, $F[2].to_f]
end
END { h(100) }
__END__