jw_cad 外部変形 - (1167) javaで階段を割り付ける -

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

 

javaで階段を割り付ける

:javaで階段を割り付ける
@echo off
if not exist %~dp0Step.class (
  for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
    more +%%n %0 > %~dpn0.java
    javac -encoding shift_jis %~dpn0.java
  )
)

set n=%1
if defined n (
  echo ^%1> %~dpn0.txt
) else (
  if exist %~dpn0.txt (
    for /f "tokens=*" %%a in (%~dpn0.txt) do set n=%%a
  )
)
if not defined n set n=12

copy jwc_temp.txt myfiles > nul
java -Dfile.encoding=shift_jis -classpath %~dp0 Step > jwc_temp.txt
goto:eof

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

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

class Step {
  public static void main(String args) {
    int n = Integer.parseInt(System.getenv("n"));
    if (n <= 0) { n = 12; }
    try {
      BufferedReader f = new BufferedReader(new FileReader("myfiles"));
      String line;
      double hk = 0;
      int i = 0;
      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();
      System.out.println( step(hp[1], hp[2], n, hk) );
    } catch (IOException e) {
      System.out.println(e);
    }
  }
  private static int to_i(double a){
    return (int) (a < 0 ? Math.ceil(a) : Math.floor(a));
  }
  private static String step(String pt1, String pt2, int n, double hk){
    double x1, y1, x2, y2, x3, y3;
    double dx, dy, ww, hh, aa, bb, xx, yy;
    double co = Math.cos(hk * Math.PI / 180);
    double si = Math.sin(hk * Math.PI / 180);
    String
s = pt1.split(" ");
    x1 = Double.parseDouble(s[0]);
    y1 = Double.parseDouble(s[1]);
    s = pt2.split(" ");
    x2 = Double.parseDouble(s[0]);
    y2 = Double.parseDouble(s[1]);
    dx = x2 - x1;
    dy = y2 - y1;
    ww = dx * co + dy * si;
    hh =-dx * si + dy * co;
    aa = hh / n;
    bb = 0;
    if (n > 1) { bb = ww / (n - 1); } else { bb = ww; }
    xx = bb * co - aa * si;
    yy = bb * si + aa * co;
    x2 = x1 - aa * si;
    y2 = y1 + aa * co;
    x3 = x1;
    y3 = y1;
    String a = "";
    for (int i = 1; i <= n; i++) {
      x3 += xx;
      y3 += yy;
      a += String.format("%.15f %.15f %.15f %.15f%n", x1, y1, x2, y2);
      a += String.format("%.15f %.15f %.15f %.15f%n", x2, y2, x3, y3);
      x1 = x3;
      y1 = y3;
      x2 += xx;
      y2 += yy;
    }
    return a;
  }
}