!A4用紙に目一杯の大きさの蛇腹折りの折り目を描くプログラム
! 実行例：
!  折り目パターンを入力したら、
!  $ gfortran prjabara.f90
!  $ ./a.out>jaba6x8.tex
!  $ platex jaba6x8
!  $ dvipdfmx jaba6x8    (同じ名前のファイルがある場合は、$ dvipdfmx jaba6x8.dvi)
!  $ evince jaba6x8.pdf&
implicit none
!---入力データ
integer :: nh=8 !    :高さ方向の段数
integer :: nr=6 !偶数:周方向の角数
!---
real(8) :: a4h=210.d-1-1.d-1*2.d0        ! cm:A4用紙の幅-余白
real(8) :: a4b=297.d-1-0.d-1*2.d0-12.d-1 ! cm:A4用紙の高さ-余白
real(8) :: pi=2.d0*asin(1.d0)            !rad:円周率
real(8) :: th,dh,da,db
integer i,j,k,l
th=pi/nr
dh=a4h/nh/2
da=(a4b+dh*nr/tan(th))/(nr+0.5d0)
db=da-2.d0*dh/tan(th)
!---ヘッダー出力
print'("\documentclass[dvipdfmx]{jsarticle}")'
!余白は上のa4hやa4bに合わせて調整すべし
print'("\usepackage[top=0truemm,bottom=0truemm,&
      &left=1truemm,right=1truemm]{geometry}%余白")'
print'("\usepackage{tikz}%描画")'
print'("")'
print'("\begin{document}")'
print'("\pagestyle{empty}%ページ番号を消す")'
print'("")'
print'("%%%%%%%%山折り・谷折り%%%%%%%%")'
print'("\begin{tikzpicture}")'
print'("\draw[very thick] (5,0.5)--(6,0.5) node[right] {山折り};")'
print'("\draw[very thick,dashed] (5,0)--(6,0) node[right] {谷折り};")'
print'("\end{tikzpicture}")'
print'("\newline")'
print'("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")'
print'("\begin{tikzpicture}")'
print'("")'
!---折り目の計算と出力
!周期
do j=0,nh-1
   !縦1
   do i=0,nr/2-1
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+(da+db)*i,&
          &")--(",2.d0*dh*j,",",dh/tan(th)+(da+db)*i+db,");"
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick,dashed] (",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i,&
          &")--(",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i+da,");"
   enddo
   i=nr/2-1
   !のりしろ1
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\filldraw[fill=black!10,draw=none](",&
       &2.d0*dh*j,",",dh/tan(th)+(da+db)*(i+1.d0),&
       &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*(i+1.d0),&
       &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*(i+1.d0)+da/2,&
       &")--(",2.d0*dh*j,",",dh/tan(th)+(da+db)*(i+1.d0)+db/2,");"
   !のりしろ2
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\filldraw[fill=black!10,draw=none](",&
       &2.d0*dh*(j+0.5d0),",",(da+db)*(i+1.d0),&
       &")--(",2.d0*dh*(j+1.d0),",",dh/tan(th)+(da+db)*(i+1.d0),&
       &")--(",2.d0*dh*(j+1.d0),",",dh/tan(th)+(da+db)*(i+1.d0)+db/2,&
       &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*(i+1.d0)+da/2,");"
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i+da,&
       &")--(",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i+da+db/2,");"
   !斜め1
   do i=0,nr/2-1
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+(da+db)*i,&
          &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*i,");"
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i,&
          &")--(",2.d0*dh*(j+0.5d0),",",da+(da+db)*i,");"
   enddo
   i=nr/2
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+(da+db)*i,&
       &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*i,");"
   !縦2
   do i=0,nr/2-1
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick,dashed] (",2.d0*dh*(j+0.5d0),",",(da+db)*i,&
          &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*i+da,");"
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",2.d0*dh*(j+0.5d0),",",da+(da+db)*i,&
          &")--(",2.d0*dh*(j+0.5d0),",",da+(da+db)*i+db,");"
   enddo
   i=nr/2
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick,dashed] (",2.d0*dh*(j+0.5d0),",",(da+db)*i,&
       &")--(",2.d0*dh*(j+0.5d0),",",(da+db)*i+da/2,");"
   !斜め2
   do i=0,nr/2-1
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",2.d0*dh*(j+0.5d0),",",(da+db)*i,&
          &")--(",2.d0*dh*(j+1.d0),",",dh/tan(th)+(da+db)*i,");"
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",2.d0*dh*(j+0.5d0),",",da+(da+db)*i,&
          &")--(",2.d0*dh*(j+1.d0),",",dh/tan(th)+db+(da+db)*i,");"
   enddo
   i=nr/2
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick] (",2.d0*dh*(j+0.5d0),",",(da+db)*i,&
       &")--(",2.d0*dh*(j+1.d0),",",dh/tan(th)+(da+db)*i,");"

enddo
!右端
j=nh
do i=0,nr/2-1
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+(da+db)*i,&
       &")--(",2.d0*dh*j,",",dh/tan(th)+(da+db)*i+db,");"
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick,dashed] (",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i,&
       &")--(",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i+da,");"
enddo
i=nr/2-1
print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
    &"\draw[very thick] (",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i+da,&
    &")--(",2.d0*dh*j,",",dh/tan(th)+db+(da+db)*i+da+db/2,");"

!---フッダー出力
print'("")'
print'("\end{tikzpicture}")'
print'("\end{document}")'
end
