!A4用紙に目一杯の大きさのダイヤカットの折り目を描くプログラム
! 実行例：
!  折り目パターンを入力したら、
!  $ gfortran prdaiya.f90
!  $ ./a.out>dy6x8.tex
!  $ platex dy6x8
!  $ dvipdfmx dy6x8    (同じ名前のファイルがある場合は、$ dvipdfmx dy6x8.dvi)
!  $ evince dy6x8.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) :: dh,db
integer i,j,k,l
dh=a4h/nh/2
db=a4b/(nr+0.5d0)
!---ヘッダー出力
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*2-1
   !縦1
   do i=0,nr-1
      if (mod(j,2)==0) then
         print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
             &"\draw[very thick,dashed] (",dh*j,",",db*i,&
             &")--(",dh*j,",",db*i+db,");"
      else
         print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
             &"\draw[very thick,dashed] (",dh*j,",",db*(i+0.5d0),&
             &")--(",dh*j,",",db*(i+1.5d0),");"
      endif
   enddo
   i=nr
   if (mod(j,2)==0) then
      !のりしろ
      print'(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] (",dh*j,",",db*i,&
          &")--(",dh*(j+1.d0),",",db*(i+0.5d0),&
          &")--(",dh*j,",",db*(i+0.5d0),");"
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick,dashed] (",dh*j,",",db*i,&
          &")--(",dh*j,",",db*(i+0.5d0),");"
   endif
   !斜め1
   do i=0,nr
      if (mod(j,2)==0) then
         print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
             &"\draw[very thick] (",dh*j,",",db*i,&
             &")--(",dh*(j+1.d0),",",db*(i+0.5d0),");"
      else
         if (i==nr) then
            print'(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] (",dh*j,",",db*(i+0.5d0),&
                &")--(",dh*(j+1.d0),",",db*i,&
                &")--(",dh*(j+1.d0),",",db*(i+0.5d0),");"
         endif
         print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
             &"\draw[very thick] (",dh*j,",",db*(i+0.5d0),&
             &")--(",dh*(j+1.d0),",",db*i,");"
      endif
   enddo
   !斜め2
   do i=0,nr-1
      if (mod(j,2)==0) then
         print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
             &"\draw[very thick] (",dh*j,",",db*(i+1.d0),&
             &")--(",dh*(j+1.d0),",",db*(i+0.5d0),");"
      else
         print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
             &"\draw[very thick] (",dh*j,",",db*(i+0.5d0),&
             &")--(",dh*(j+1.d0),",",db*(i+1.d0),");"
      endif
   enddo
enddo
!右端
j=nh*2
do i=0,nr-1
   if (mod(j,2)==0) then
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick,dashed] (",dh*j,",",db*i,&
          &")--(",dh*j,",",db*i+db,");"
   else
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick,dashed] (",dh*j,",",db*(i+0.5d0),&
          &")--(",dh*j,",",db*(i+1.5d0),");"
   endif
enddo
i=nr
if (mod(j,2)==0) then
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick,dashed] (",dh*j,",",db*i,&
       &")--(",dh*j,",",db*i+db/2,");"
endif

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