jw_cad 外部変形 - (1145) javaで2点間の勾配を計算する(ptslope) -

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

 

javaで2点間の勾配を計算する(ptslope)

:javaで2点間の勾配を計算する(ptslope)
@echo off
if not exist %~dp0PtSlope.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 PtSlope > jwc_temp.txt
goto:eof

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

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

class PtSlope {
  public static void main(String args) {
    try {
      BufferedReader f = new BufferedReader(new FileReader("myfiles"));
      String line;
      double hk = 0.0;
      int i = 1;
      String
hp = new String[10];
      while ( (line = f.readLine()) != null) {
        if (Pattern.compile("^hk").matcher(line).find()) {
          hk = Double.parseDouble(line.substring(2));
          continue;
        }
        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, d;
      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]);
      d = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI - hk;
      System.out.printf("h#2点間の勾配∠ = %.3f゚\n", d);
    } catch (IOException e) {
      System.out.println(e);
    }
  }
}