jw_cad 外部変形 - (683) パワーシェルでレムニスケートを描く -

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

 

パワーシェルでレムニスケートを描く

:パワーシェルでレムニスケートを描く
@echo off
for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
  more +%%n %0 | powershell -command -
)
goto:eof

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

#!ここから more +n %0 の n 行目:最初の行は 0 行
function hypot ($x,$y)
{
  [math]::sqrt($x*$x+$y*$y)
}
function f ($a,$p)
{
  return $a*[math]::cos($p)*[math]::sqrt(1.0-0.5*[math]::pow([math]::sin($p),2))
}
function g ($a,$p)
{
  return $a/[math]::sqrt(2.0)*[math]::sin($p)*[math]::cos($p)
}
function hhh ($n)
{
  $x1,$y1,$x2,$y2 = $hp[1]+$hp[2]
  $d = [math]::atan2($y2-$y1,$x2-$x1) + [math]::PI/2.0
  $si = [math]::sin($d)
  $co = [math]::cos($d)
  $a = hypot ($x2-$x1) ($y2-$y1)
  $p = 2.0*[math]::PI/[double] $n
  $x = f $a 0.0
  $y = g $a 0.0
  $xa =  $x*$si + $y*$co + $x1
  $ya = -$x*$co + $y*$si + $y1
  $i = 1
  while ($i -ne $n+1)
  {
    $x = f $a ([double] $i*$p)
    $y = g $a ([double] $i*$p)
    $xb =  $x*$si + $y*$co + $x1
    $yb = -$x*$co + $y*$si + $y1
    "$xa $ya $xb $yb"
    $xa = $xb;
    $ya = $yb;
    $i++
  }
}

$hp = 0,1,2
gc jwc_temp.txt | %{
  switch -regex -case ($_)
  {
    ^hp[1-9][0-9]? { # 指示点
      $0 = [regex]::split($_,' +')
      $hp[ [regex]::match($0[0],"[1-9][0-9]?").value ] = @($0[1,2] | %{ [double] $_ })
    }
  }
}
hhh 100 | sc jwc_temp.txt

#ここまで powershell は 文末に必ず空白行を置いてください