jw_cad 外部変形 - (1406) vbscriptで線に直交線を引く -

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

 

vbscriptで線に直交線を引く

:vbscriptで線に直交線を引く
@echo off
if not exist %~dp0eval.vbs echo ExecuteGlobal WScript.StdIn.ReadAll > %~dp0eval.vbs
for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
  copy jwc_temp.txt myfiles > nul
  (
:   echo Option Explicit
    echo On Error Resume Next
:   echo On Error GoTo 0
    more +%%n %0
  ) | cscript //nologo %~dp0eval.vbs > jwc_temp.txt
)
goto:eof

REM #jww
REM #1ln 線を指示してください
REM #bz
REM #e

#!この次の行からプログラムを書いてください
'1)正規表現
Dim re
Set re = New RegExp
re.IgnoreCase = True '大文字と小文字を区別しない
re.Global = True '文字列全体を検索する

function reSplit(str)
  re.Pattern = "[ ]+"
  reSplit = Split(Trim(re.Replace(str," ")))
end function

function reTest(pat, str)
  re.Pattern = pat
  reTest = re.Test(str)
end function

'2)円周率
Const PI = 3.141592653589793

'4)ユーザ定義関数
function atan2(y, x)
  if x = 0 then
    atan2 = atn(1) * 2 * sgn(y)
  else
    atan2 = atn(y / x)
  end if
  if x < 0 then
    if y < 0 then
      atan2 = atan2 - atn(1) * 4
    else
      atan2 = atan2 + atn(1) * 4
    end if
  end if
end function

function hypot(x, y)
  hypot = sqr(x * x + y * y)
end function

function ptdist(x, y)
  if typename(x) = "Integer" then
    ptdist = hypot(y(0), y(1))
    exit function
  end if
  if typename(y) = "Integer" then
    ptdist = hypot(x(0), x(1))
    exit function
  end if
  if typename(x) = "String" then x = Split(Trim(x))
  if typename(y) = "String" then y = Split(Trim(y))
  ptdist = hypot(y(0) - x(0), y(1) - x(1))
end function

function polar(r, d, w) '方向余弦のセット
  if w = NULL or w = "" or typename(w) = "Empty" then w = 1
  polar = Array(r * cos(d), r * sin(d) * w)
end function

function moveto(x, y) '点 x を 相対距離 y [a, b] へ移動
  if typename(x) = "String" then x = Split(Trim(x))
  if typename(y) = "String" then y = Split(Trim(y))
  moveto = Array(x(0) + y(0), x(1) + y(1))
end function

function polarto(x, r, d, w)
  if typename(x) = "String" then x = Split(Trim(x))
  if w = NULL or w = "" or typename(w) = "Empty" then w = 1
  polarto = moveto(x, polar(r, d, w))
end function

function lnlength(x) '線長
  if typename(x) = "String" then x = Split(Trim(x))
  lnlength = hypot(x(2) - x(0), x(3) - x(1))
end function

function lnslope(x) '線角
  if typename(x) = "String" then x = Split(Trim(x))
  lnslope = atan2(x(3) - x(1), x(2) - x(0))
end function

function lnpoint(x, pm) '線データの値を返す
  if typename(x) = "String" then x = Split(Trim(x))
  '始点 pm=-1,  終点 pm=1 の座標を返す
  if pm = 0 then
    lnpoint = Array( (x(0) + x(2)) / 2, (x(1) + x(3)) / 2)
  else
    if pm < 0 then
      lnpoint = Array(x(0), x(1))
    else
      lnpoint = Array(x(2), x(3))
    end if
  end if
end function

Dim line, F
Dim p1, ln1
With CreateObject("Scripting.FileSystemObject")
  With .OpenTextFile("myfiles")
  Do While Not .AtEndOfStream
    line = .ReadLine: F = reSplit(line)
    if reTest("^hp1",line) then p1 = Array(F(1)*1.0,F(2)*1.0)
    if reTest("^[ ]",line) then ln1 = F '指示線
  Loop
  End With
End With

Dim i
for i = 0 to 3
  ln1(i) = Cdbl(ln1(i))
next

Dim p0, p2, pc, l1, l2, l3
p0 = p1
p1 = lnpoint(ln1,-1)
p2 = lnpoint(ln1, 1)
pc = lnpoint(ln1, 0)
l1 = ptdist(p0, p1)
l2 = ptdist(p0, p2)
l3 = ptdist(p0, pc)
if (sgn(l2 - l1) + sgn(l3 - l1)) = 2 then pc = p1
if (sgn(l1 - l2) + sgn(l3 - l2)) = 2 then pc = p2
Dim l, d
l = lnlength(ln1) / 2
d = lnslope(ln1) + PI / 2
WScript.Echo "bz"
WScript.Echo Join(polarto(pc, l, d, 1)) & " " & Join(polarto(pc,-l, d, 1))