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

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

 

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

:javaで2点間の距離を計算する(ptdist)
@echo off
if not exist %~dp0PtDist.class (
  for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
    more +%%n %0 > %~dpn0.java
    javac -encoding shift_jis %~dpn0.java
  )
)
copy jwc_temp.txt myfiles > nul
java -Dfile.encoding=shift_jis -classpath %~dp0 PtDist > jwc_temp.txt
goto:eof

REM #jww
REM #1-%d 始点を指示してください
REM #2%d 終点を指示してください
REM #e

#!この次の行からプログラムを書いてください
import java.io.*;
import java.util.regex.Pattern;

class PtDist {
  public static void main(String args) {
    try {
      BufferedReader f = new BufferedReader(new FileReader("myfiles"));
      String line;
      int i = 1;
      String
hp = new String[10];
      while ( (line = f.readLine()) != null) {
        if (Pattern.compile("^hp[1-9][0-9]*").matcher(line).find()) {
          String str = line.split(" ", 2);
          i = Integer.parseInt(str[0].replaceAll("^hp|-$", ""));
          hp[i] = str[1].trim();
        }
      }
      f.close();
      double x1, y1, x2, y2, r;
      String
p = hp[1].split(" ");
      x1 = Double.parseDouble(p[0]);
      y1 = Double.parseDouble(p[1]);
      String[] q = hp[2].split(" ");
      x2 = Double.parseDouble(q[0]);
      y2 = Double.parseDouble(q[1]);
      r = Math.hypot(x2 - x1, y2 - y1);
      System.out.printf("h#2点間の距離 L = %.3f\n", r);
    } catch (IOException e) {
      System.out.println(e);
    }
  }
}