!A4用紙に回転螺旋折りの折り目を描くプログラム
! 実行例：
!  折り目パターンを入力したら、
!  $ gfortran prkrasen.f90
!  $ ./a.out>kr5x10.tex
!  $ platex kr5x10
!  $ dvipdfmx kr5x10    (同じ名前のファイルがある場合は、$ dvipdfmx kr5x10.dvi)
!  $ evince kr5x10.pdf&
implicit none
!---入力データ
integer :: nh=10  !高さ方向の段数
integer :: nr=5  !周方向の角数
real(8) :: bt=30 !度:角度(許容範囲がそこそこ大きい)
!---
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) :: alp,th,dh,db
integer i,j,k,l
alp=pi/nr
bt=bt/180*pi!radianに直す
th=pi/2-(alp+bt)
dh=a4h/nh
db=dh*(tan(pi/2-bt)-tan(th))
!---alpとbtが適切かどうかの確認
if (pi/4-pi/2/nr>=bt.or.bt>=pi/2-pi/nr) then 
   print'(A,f9.4,A,f9.4,A)',"今の条件：bt=",bt*180/pi,"度,alp=",alp*180/pi,"度"
   print'("角度btの値が適切でありません。")'
   print'(f6.2,A,f6.2,A)',(pi/4-alp/2)*180/pi," < bt < ",(pi/2-alp)*180/pi,"  (度)"
   print'("を満たす値に変えて下さい。")'
endif

!---ヘッダー出力
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-1
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",dh*j,",",dh*tan(th)*j+db*i,&
          &")--(",dh*j,",",dh*tan(th)*j+db*i+db,");"
   enddo
   i=nr
   !のりしろ1
   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,",",dh*tan(th)*j+db*i,&
       &")--(",dh*(j+0.5d0),",",dh*tan(th)*(j+0.5d0)+db*(i+0.5d0),&
       &")--(",dh*j,",",dh*tan(th)*j+db*(i+0.5d0),");"
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick] (",dh*j,",",dh*tan(th)*j+db*i,&
       &")--(",dh*j,",",dh*tan(th)*j+db*(i+0.5d0),");"
   !のりしろ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] (",dh*j,",",dh*tan(th)*j+db*i,&
       &")--(",dh*(j+1.d0),",",dh*tan(th)*(j+1.d0)+db*i,&
       &")--(",dh*(j+1.d0),",",dh*tan(th)*(j+1.d0)+db*(i+0.5d0),&
       &")--(",dh*(j+0.5d0),",",dh*tan(th)*(j+0.5d0)+db*(i+0.5d0),");"
   !斜め1
   do i=0,nr
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick] (",dh*j,",",dh*tan(th)*j+db*i,&
          &")--(",dh*(j+1.d0),",",dh*tan(th)*(j+1.d0)+db*i,");"
   enddo
   !斜め2
   do i=0,nr-1
      print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
          &"\draw[very thick,dashed] (",dh*j,",",dh*tan(th)*j+db*i,&
          &")--(",dh*(j+1.d0),",",dh*tan(th)*(j+1.d0)+db*i+db,");"
   enddo
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick,dashed] (",dh*j,",",dh*tan(th)*j+db*i,&
       &")--(",dh*(j+0.5d0),",",dh*tan(th)*(j+0.5d0)+db*i+db/2,");"
enddo
!右端
j=nh
do i=0,nr-1
   print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
       &"\draw[very thick] (",dh*j,",",dh*tan(th)*j+db*i,&
       &")--(",dh*j,",",dh*tan(th)*j+db*i+db,");"
enddo
i=nr
print'(A,f9.4,a,f9.4,A,f9.4,a,f9.4,A)',&
    &"\draw[very thick] (",dh*j,",",dh*tan(th)*j+db*i,&
    &")--(",dh*j,",",dh*tan(th)*j+db*i+db/2,");"

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