#!/usr/bin/perl -w
# 使用方式: ./torus > torus.dat
# 可用選項: 見程式註解.
# 例如 ./torus -f geomview -s 80 -t 32 > torus.mesh
# 可為 geomview 產生一個細密的 mesh
#
# 如果你是為了學 gnuplot 或 geomview 而使用這個程式,
# 可以不必理會 perl 的語法; 只要用 -s 8 -t 4
# 就可以產生一個比較簡單, 可以理解的資料檔.

use Getopt::Std;
use strict;

my (%opts, $pi, $s, $t, $i, $j);

%opts = (
    a => 10,		# 大軸半徑
    b => 3,		# 小軸半徑
    s => 24,		# s 參數取樣個數
    t => 12,		# t 參數取樣個數
    f => "gnuplot",	# 為那個應用軟體產生輸出? 可以是 gnuplot 或 geomview
);

getopts("a:b:f:s:t:", \%opts);

$pi = atan2(0,-1);
if ($opts{f} eq "geomview") {
    printf "MESH\n%d %d\n\n", $opts{t}+1, $opts{s}+1;
}
for ($i=0; $i<=$opts{s}; ++$i) {
    $s = 2*$pi * $i/$opts{s};
    for ($j=0; $j<=$opts{t}; ++$j) {
	$t = 2*$pi * $j/$opts{t};
	printf "%7.4f %7.4f %7.4f\n",
	    ($opts{a}+$opts{b}*cos($t))*cos($s),
	    ($opts{a}+$opts{b}*cos($t))*sin($s),
	    $opts{b}*sin($t);
    }
    print "\n";
}

