#! /bin/bash -eu
# Last edited on 2026-04-18 18:42:38 by stolfi

echo "plotting book sizes ..." 1>&2

dims_file="res/book-dims.txt"

sheets_files=()

for b in 0 1; do 
  sh_file="res/sheet-dims-${b}.txt"
  ht0=$( echo "168/sqrt(sqrt(2))^${b}" | bc -lq )
  gawk -v ht0=${ht0} \
    ' BEGIN { 
        ht = ht0; ns = 3;
        for (i = 0; i < ns; i++) {
          wd = ht/sqrt(2)
          printf "%6.3f %6.3f\n", 0.0, 0.0;
          printf "%6.3f %6.3f\n", 0.0, ht;
          printf "%6.3f %6.3f\n", wd, ht;
          printf "%6.3f %6.3f\n", wd, 0.0;
          printf "%6.3f %6.3f\n", 0.0, 0.0;
          printf "\n";
          ht = wd;
        }
      }
    ' \
    > ${sh_file}
  sheets_files+=( ${sh_file} )
done

temp="/tmp/$$"

export GDFONTPATH="${HOME}/ttf"
temp_plot_file="${temp}_plot.png"

color=

plot_size=1000
tsize=$(( ${plot_size} * 2 ))

ticvals=(
    "3.600" 0.0002 , 
    "3.700" 0.0005 , 
    "3.800" 0.002 , 
    "3.900" 0.005 ,  
    "4.000" 0.02 , 
    "4.100" 0.02 , 
    "4.200" 0.02 , 
    "4.300" 0.02 , 
    "4.400" 0.02 , 
    "4.500" 0.02 , 
    "4.600" 0.02 , 
    "4.000" 0.02 , 
  )

gnuplot <<EOF
  set term pngcairo size ${tsize},${tsize} linewidth 2.0 font "Arial,20" noenhanced
  set size ratio -1
  set output "${temp_plot_file}"
  set xrange [-5:180]
  set yrange [-5:180]
  set xtics 10
  set mxtics 10
  # set xtics add ( ${ticvals[@]} )
  set ytics 10 rotate by 90 center
  set mytics 10
  # set ytics add ( ${ticvals[@]} )
  set grid xtics mxtics lt 1 lc rgb "#77bbff", lt 1 lc rgb "#cceeff"
  set grid ytics mytics lt 1 lc rgb "#77bbff", lt 1 lc rgb "#cceeff"
  set lmargin 6.5
  set xlabel  "WIDTH (PX)" font "Arial,20"
  set ylabel  "HEIGHT (PX)" font "Arial,20" offset 2,0
  plot \
    "${sheets_files[0]}" notitle with lines lw 2 lc rgb "#0099ff", \
    "${sheets_files[1]}" notitle with lines lw 2 lc rgb "#33aa00", \
    (sqrt(2)*x) notitle with lines lw 2 lc rgb "#339900", \
    "${dims_file}" using 1:2 notitle \
      with points pt 7 ps 2.0 \
      lc rgb '#ff2200', \
    "" using 1:2:6 notitle \
      with labels rotate by -45 offset character 1,-0.4 left \
      lc rgb "#000000"
  quit
EOF

plot_file="res/book-dims-plot.png"
if [[ -s ${temp_plot_file} ]]; then
  convert ${temp_plot_file} -resize '50%' ${plot_file}
  display -title '%f' ${plot_file}
else
  echo "** ${temp_plot_file} not created" 1>&2; exit 1
fi
rm -f ${temp}-*
 
