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

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

 

javaで線に直交線を引く

:javaで線に直交線を引く
@echo off
if not exist %~dp0Lncross.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 Lncross > jwc_temp.txt
goto:eof

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

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

class Lncross {
  public static void main(String args) {
    try {
      BufferedReader f = new BufferedReader(new FileReader("myfiles"));
      String line;
      int i = 0;
      String
hp = new String[10];
      String ln = new String[10];
      while ( (line = f.readLine()) != null) {
        if (Pattern.compile("^hq").matcher(line).find()) {
          System.out.println("bz");
          continue;
        }
        if (Pattern.compile("^hp[1-9][0-9]*").matcher(line).find()) {
          String
str = line.trim().split(" ", 2);
          i = Integer.parseInt(str[0].replaceAll("^hp|ln|-$", ""));
          hp[i] = str[1].trim();
        }
        if (Pattern.compile("^hhp[1-9][0-9]*ln").matcher(line).find()) {
          String str = line.trim();
          i = Integer.parseInt(str.replaceAll("^hhp|ln$", ""));
          continue;
        }
        if (Pattern.compile("^ ").matcher(line).find()) {
          String p = line.trim().split(" ");
          if (p.length == 4) {
            ln[i]=line.trim();
          }
        }
      }
      f.close();
      String p0, ln1, p1, p2, p3, pc;
      double x1, y1, x2, y2, l1, l2, l3;
      double l, d, lx, ly, xc, yc;
      p0 = hp[1];
      ln1 = ln[1];
      String
s = ln1.split(" ");
      p1 = s[0] + " " + s[1];
      p2 = s[2] + " " + s[3];
      x1 = Double.parseDouble(s[0]);
      y1 = Double.parseDouble(s[1]);
      x2 = Double.parseDouble(s[2]);
      y2 = Double.parseDouble(s[3]);
      p3 = String.format("%.15f %.15f", (x1 + x2) / 2, (y1 + y2) / 2);
      l1 = lnlength(p0 + " " + p1);
      l2 = lnlength(p0 + " " + p2);
      l3 = lnlength(p0 + " " + p3);
      pc = p3;
      if (Math.signum(l2 - l1) + Math.signum(l3 - l1) == 2) { pc = p1; }
      if (Math.signum(l1 - l2) + Math.signum(l3 - l2) == 2) { pc = p2; }
      l = lnlength(ln1) / 2;
      d = lnslope(ln1);
      lx = l * Math.cos(d);
      ly = l * Math.sin(d);
      String p = pc.split(" ");
      xc = Double.parseDouble(p[0]);
      yc = Double.parseDouble(p[1]);
      System.out.printf("%.15f %.15f %.15f %.15f%n", xc + ly, yc - lx, xc - ly, yc + lx);
    } catch (IOException e) {
      System.out.println(e);
    }
  }
  private static double lnlength(String ln){
    double x1, y1, x2, y2;
    String
s = ln.split(" ");
    x1 = Double.parseDouble(s[0]);
    y1 = Double.parseDouble(s[1]);
    x2 = Double.parseDouble(s[2]);
    y2 = Double.parseDouble(s[3]);
    return Math.hypot(x2 - x1, y2 - y1);
  }
  private static double lnslope(String ln){
    double x1, y1, x2, y2;
    String[] s = ln.split(" ");
    x1 = Double.parseDouble(s[0]);
    y1 = Double.parseDouble(s[1]);
    x2 = Double.parseDouble(s[2]);
    y2 = Double.parseDouble(s[3]);
    return Math.atan2(y2 - y1, x2 - x1);
  }
}