ハートのアニメーション【結婚記念】

gnuplotのrereadとifを使ったループを利用して、アニメーションを作りました。某パーティーで使用したものです。

この例ではこちらのページで紹介されているハートの書き方を借用して3Dのハート型の座標の入ったデータファイルを作成し、それをアニメーションにしています。アニメーションの作成には渦糸のアニメーションの場合と同じく、番号つきのPNGファイルを何枚か作成し、それをgiamというプログラムでgifアニメーションに結合するという方法を採っています。

以下のスクリプトを実行するにはハートのデータファイルheart_3D-3.datをダウンロードして同じフォルダにおいてから実行してください。また、PNGファイルを置くためのフォルダとして、heart_animation/というフォルダを作成してから実行してください。

#version 4.3 ハートのアニメーション

tablefile="heart_3D-3.dat"

if (exist("I")==0) I=0

Imax=39  #フレーム数 引く 1
set view 90,(250+I*9)%360

if (I==0) set xrange [-1.5:1.5]
if (I==0) set yrange [-1.5:1.5]
if (I==0) set palette color defined (-0.6 "pink", 0 "red", 0.6 "pink")

if (I==0) unset key
if (I==0) set border 0
if (I==0) unset colorbox
if (I==0) unset xtics
if (I==0) unset ytics
if (I==0) unset ztics

# ループの一回目だけ試しにプロットしてみる
if (I==0) splot tablefile using 2:3:4:2 w pm3d  

if (I==0) pause -1 "Click OK to make PNG files"

set out sprintf("heart_animation/heart%03d.png", I)
set term png size 500,400
rep

if (I<Imax) I=I+1; pause 0.1;  \
     reread # スクリプトの最初に戻る


undefine I
set term win
set out
replot
pause -1 "Finished"

ハートのデータファイルはgnuplotおよびgawkを使用して作成しています。参考までに、データファイルの作成まで含めたスクリプトも公開しておきます。以下のスクリプトの実行にはgawkがインストールされている必要がありますが、実行するとheart_3D-3.datが作成された上、番号付PNGファイルが作成されます。

#version 4.3gpNov08

tablefile="heart.dat"
tablefile2="heart_3D.dat"
tablefile3="heart_3D-2.dat"
tablefile4="heart_3D-3.dat"

if (exist("I")==0) I=0

if (I==0) set contour
if (I==0) set cntrparam levels discrete 0.0
if (I==0) set view map
if (I==0) set xrange [-1.5:1.5]
if (I==0) set yrange [-1:1.5]
if (I==0) set isosamples 51
if (I==0) set samples 101
if (I==0) unset surface
if (I==0) splot (x**2 + y**2 - 1)**3 - 3*x**2*y**3 

#ハートのデータファイルを作る
if (I==0) set table tablefile
if (I==0) replot
if (I==0) unset table

if (I==0) plot tablefile u ($1):($2) w lp
if (I==0) pause -1


if (I==0) set view
if (I==0) unset contour

if (I==0) set autoscale xy
if (I==0) set angles radians
if (I==0) set table tablefile2

if (I==0) N=20
if (I==0) r=1.0/N*pi/2.0
if (I==0) plot for [i=-N:N] tablefile using 0:(0.25*sin(i*r)):($1*cos(i*r)):($2*cos(i*r)) with xerrorbars
if (I==0) unset table

#吐き出したデータファイルのうち、行の末尾がoまたはuのものを削除する。
if (I==0) gawkcommand= sprintf("gawk \"$0 !~ /[ou]$/ {print} \" %s > %s", tablefile2, tablefile3)
if (I==0) print gawkcommand
if (I==0) system(gawkcommand)

#データファイルの2行の空行を1行の空行に変換する
if (I==0) gawkcommand= sprintf("gawk \"BEGIN {frag=0} {if ($0 ~ /^$/) frag=frag+1; else frag=0;} {if (frag<=1) print} \" %s > %s", tablefile3, tablefile4)
if (I==0) print gawkcommand
if (I==0) system(gawkcommand)

Imax=39
set view 90,(250+I*9)%360

if (I==0) set xrange [-1.5:1.5]
if (I==0) set yrange [-1.5:1.5]
if (I==0) set palette color defined (-0.6 "pink", 0 "red", 0.6 "pink")

if (I==0) unset key
if (I==0) set border 0
if (I==0) unset colorbox
if (I==0) unset xtics
if (I==0) unset ytics
if (I==0) unset ztics

if (I==0) splot tablefile4 using 2:3:4:2 w pm3d

if (I==0) pause -1

set out sprintf("heart_animation/heart%03d.png", I)
set term png size 500,400
rep

if (I<Imax) I=I+1; pause 0.1;  reread

undefine I

set term win
set out
replot
pause -1 "Finished"