jw_cad 外部変形 - (1384) vbscriptで2点間の距離を計算する(ptdist) -

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

 

vbscriptで2点間の距離を計算する(ptdist)

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

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

Dim line, F, p1, p2
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,F(2)*1)
    if reTest("^hp2",line) then p2 = Array(F(1)*1,F(2)*1)
  Loop
  End With
End With
Dim x1, y1, x2, y2, l
x1 = p1(0): y1 = p1(1)
x2 = p2(0): y2 = p2(1)
l = hypot(x2 - x1, y2 - y1)
WScript.Echo "h#2点間の距離 L = " & FormatNumber(l, 3, 0)