外部変形は データのやり取りをテキストファイルで行うので プログラム言語は 自由に選ぶことができます。図形は機能的かつシンプルなため、数多くのユーザーに受け入れられています。
javaで多角形の面積を計算する
:javaで多角形の面積を計算する
@echo off
if not exist %~dp0Area.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 Area > jwc_temp.txt
goto:eof
REM #jww
REM #1-%d 点を指示してください
REM #99#%d
REM #e
#!この次の行からプログラムを書いてください
import java.io.*;
import java.util.regex.Pattern;
class Area {
public static void main(String args) {
try {
BufferedReader f = new BufferedReader(new FileReader("myfiles"));
String line;
int i = 1;
String hp = new String[100];
while ( (line = f.readLine()) != null) {
if (Pattern.compile("^hp[1-9][0-9]*").matcher(line).find()) {
String str = line.split(" ", 2);
hp[i++] = str[1].trim();
}
}
f.close();
if (hp[1] != hp[i]) { hp[i++] = hp[1]; }
double ax = 0.0;
double sx = 0.0;
double sy = 0.0;
double ix = 0.0;
double iy = 0.0;
double xmin = 0.0;
double xmax = 0.0;
double ymin = 0.0;
double ymax = 0.0;
double x1, y1, x2, y2, gx, gy;
String p = new String[100];
for (int j = 1; j < i-1; j++) {
p = hp[j].split(" ");
x1 = Double.parseDouble(p[0]);
y1 = Double.parseDouble(p[1]);
p = hp[j+1].split(" ");
x2 = Double.parseDouble(p[0]);
y2 = Double.parseDouble(p[1]);
ax += 0.5 * (x1 - x2) * (y1 + y2); //左回りが正
sx += ( (x1 - x2) * (y1 * (2 * x1 + x2) + y2 * (2 * x2 + x1)) / 6.0);
sy += ( (y2 - y1) * (x1 * (2 * y1 + y2) + x2 * (2 * y2 + y1)) / 6.0);
ix += ( (x1 - x2) * (y1 * Math.pow(2 * x1 + x2, 2) + y2 * Math.pow(2 * x2 + x1, 2) + Math.pow(x1 - x2, 2) * (y1 + y2) / 2) / 18.0);
iy += ( (y2 - y1) * (x1 * Math.pow(2 * y1 + y2, 2) + x2 * Math.pow(2 * y2 + y1, 2) + Math.pow(y1 - y2, 2) * (x1 + x2) / 2) / 18.0);
if (xmin > x1) { xmin = x1; }
if (xmax < x1) { xmax = x1; }
if (ymin > y1) { ymin = y1; }
if (ymax < y1) { ymax = y1; }
if (i - 2 == j) {
if (xmin > x2) { xmin = x2; }
if (xmax < x2) { xmax = x2; }
if (ymin > y2) { ymin = y2; }
if (ymax < y2) { ymax = y2; }
}
}
gx = sx / ax; //図心
gy = sy / ax;
ix -= ax * gx * gx; //図心まわりの2次モーメント(Y軸) Iy-y
iy -= ax * gy * gy; //図心まわりの2次モーメント(X軸) Ix-x
System.out.printf("h#Area = %.1f Iy = %.1f Ix = %.1f\n", ax, ix, iy);
} catch (IOException e) {
System.out.println(e);
}
}
}
座標法で断面積、断面1次モーメント、断面2次モーメントを求めています。