#!/usr/bin/perl -W
#
# Usage : $pgm v1 v2 v3 mu m n 
#
# Output : TeX/TikZ file with the (m,n)-motif in (0,0) of the plan
# defined by the normal vector (v1,v2,v3) with shift mu

use POSIX;

my ($v1,$v2,$v3,$mu,$m,$n,$unit) = @ARGV;

$colup="blue!30"; $colleft="red!30"; $colright="green!30";

my $dx = 0.866025403784438597;
my $dy = 0.5;

print "\\documentclass{article}

\\usepackage[utf8]{inputenc}
\\usepackage{amsmath}
\\usepackage{tikz}

\\begin{document}

\\begin{figure}
\t\\caption{\$\\mathbf P \\left(( $v1 , $v2 , $v3 ), $mu \\right)\$}
\t\\begin{center}
\t\t\\begin{tikzpicture}\n"; 

# After reduction
my $a = $v1/$v2, $b = $v2/$v3;
$mu = $mu/$v3;

for(my $j = 0; $j < $n; ++$j) {
    for(my $i = 0; $i < $m; ++$i) {
	my $tmp = floor($a*$i + $b*$j + $mu);
	print &cube($unit, ($i-$j)*$dx, -$tmp-($j+$i)*$dy, $colup, $colleft, $colright);
    }
}

print "\t\t\\end{tikzpicture}\n\t\\end{center}\n\\end{figure}\n\\end{document}";

sub cube {
    my ($edge, $x, $y, $color_up, $color_left, $color_right) = @_;
    my $dx = 0.866025403784438597;
    my $dy = 0.5;

    return "\\draw[fill=".$color_up."] (".sprintf("%.10f",$x*$edge)." pt, ".sprintf("%.10f",$y*$edge)." pt) -- (".sprintf("%.10f",$x*$edge+$dx*$edge)." pt, ".sprintf("%.10f",$y*$edge+$dy*$edge)." pt) -- (".sprintf("%.10f",$x*$edge)." pt, ".sprintf("%.10f",$y*$edge+$edge)." pt) -- (".sprintf("%.10f",$x*$edge-$dx*$edge)." pt, ".sprintf("%.10f",$y*$edge+$dy*$edge)." pt) -- cycle;\n\\draw[fill=".$color_left."] (".sprintf("%.10f",$x*$edge)." pt, ".sprintf("%.10f",$y*$edge)." pt) -- (".sprintf("%.10f",$x*$edge-$dx*$edge)." pt, ".sprintf("%.10f",$y*$edge+$dy*$edge)." pt) -- (".sprintf("%.10f",$x*$edge-$dx*$edge)." pt, ".sprintf("%.10f",$y*$edge-$dy*$edge)." pt)  -- (".sprintf("%.10f",$x*$edge)." pt, ".sprintf("%.10f",$y*$edge-$edge)." pt) -- cycle;\n\\draw[fill=".$color_right."] (".sprintf("%.10f",$x*$edge)." pt, ".sprintf("%.10f",$y*$edge)." pt) -- (".sprintf("%.10f",$x*$edge)." pt, ".sprintf("%.10f",$y*$edge-$edge)." pt) -- (".sprintf("%.10f",$x*$edge+$dx*$edge)." pt, ".sprintf("%.10f",$y*$edge-$dy*$edge)." pt) -- (".sprintf("%.10f",$x*$edge+$dx*$edge)." pt, ".sprintf("%.10f",$y*$edge+$dy*$edge)." pt) -- cycle;\n";
}
