掲示板の「等幅」オプションをONにしてあげ直します。
(※RBファイル内の '(シングルクォート)がもし文字化けして動かない場合は、AIに修復を依頼するか、半角に置換してください。)
plusハッチ.bat
@echo off
setlocal
goto EXE
REM plusハッチ外変
REM #jww
REM #cd
REM #g0
REM #gn
REM #1ln 1本目の線を選択
REM #2ln 2本目の線を選択
REM #hr
REM #e
:EXE
pushd "%~dp0" >nul
set "SCRIPT=%~dp0plusハッチ.rb"
if exist "C:\Ruby33-x64\bin\ruby.exe" (
"C:\Ruby33-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else if exist "C:\Ruby32-x64\bin\ruby.exe" (
"C:\Ruby32-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else if exist "C:\Ruby31-x64\bin\ruby.exe" (
"C:\Ruby31-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else if exist "C:\Ruby30-x64\bin\ruby.exe" (
"C:\Ruby30-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else (
ruby -Ks "%SCRIPT%"
)
popd >nul
endlocal
exit /b 0
plusハッチ.rb
# encoding: Windows-31J
# plusハッチ.rb
Dir.chdir(File.dirname(__FILE__))
input_file = 'jwc_temp.txt'
# --- 設定値 ---
SIZE_MM = 0.8
INTERVAL_MM = 2.2
# --------------
scale = 1.0
selected_lines = []
if File.exist?(input_file)
src = File.read(input_file, encoding: 'Windows-31J')
hs_vals = []
group = 0
src.each_line do |line|
l = line.strip
if l =~ /^hs\s+(.+)/
hs_vals = $1.split.map(&:to_f)
elsif l =~ /^lg([0-9a-f])/
group = $1.hex
scale = hs_vals[group] if hs_vals && hs_vals[group]
elsif l =~ /^[ \t]*(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)/
selected_lines << [$1.to_f, $2.to_f, $3.to_f, $4.to_f]
end
end
end
exit if selected_lines.size < 2
l1, l2 = selected_lines[-2], selected_lines[-1]
# --- 1. 成功していた範囲定義(4端点を結ぶ台形範囲) ---
p1, p2 = [l1[0], l1[1]], [l1[2], l1[3]]
p3, p4 = [l2[0], l2[1]], [l2[2], l2[3]]
dist_p1_p3 = (p1[0]-p3[0])**2 + (p1[1]-p3[1])**2
dist_p1_p4 = (p1[0]-p4[0])**2 + (p1[1]-p4[1])**2
if dist_p1_p3 < dist_p1_p4
poly = [p1, p2, p4, p3]
else
poly = [p1, p2, p3, p4]
end
# 多角形内判定関数
def inside?(px, py, poly)
ins = false; j = poly.size - 1
poly.size.times do |i|
if ((poly[i][1] > py) != (poly[j][1] > py)) &&
(px < (poly[j][0] - poly[i][0]) * (py - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0])
ins = !ins
end
j = i
end
ins
end
# --- 2. 座標計算(成功時のロジックを100%維持) ---
half, step = (SIZE_MM / 2.0) * scale, INTERVAL_MM * scale
rad1, rad2 = 45.0 * Math::PI / 180.0, -45.0 * Math::PI / 180.0
u = [Math.cos(rad1) * step, Math.sin(rad1) * step]
v = [Math.cos(rad2) * step, Math.sin(rad2) * step]
xs, ys = poly.map{|p|p[0]}, poly.map{|p|p[1]}
xmin, xmax, ymin, ymax = xs.min, xs.max, ys.min, ys.max
cx_mid, cy_mid = (xmin + xmax) / 2.0, (ymin + ymax) / 2.0
num = (Math.sqrt((xmax-xmin)**2 + (ymax-ymin)**2) / step).ceil + 5
# --- 書き出し ---
file = File.open(input_file, 'w')
begin
file.write "h#十字配置完了\n"
file.write "pl\n" # 曲線属性開始
(-num..num).each do |i|
(-num..num).each do |j|
px = cx_mid + i * u[0] + j * v[0]
py = cy_mid + i * u[1] + j * v[1]
# 十字の端点4つすべてが領域内にあるか厳密にチェック
pts = [
[px, py - half], [px, py + half],
[px - half, py], [px + half, py]
]
if pts.all? { |pt| inside?(pt[0], pt[1], poly) }
file.write "%.6f %.6f %.6f %.6f\n" % [px, py - half, px, py + half]
file.write "%.6f %.6f %.6f %.6f\n" % [px - half, py, px + half, py]
end
end
end
file.write "#\n" # 曲線属性終了
file.write "hr\n"
ensure
file.close
end