#!/usr/bin/perl -w

use strict;
use Opt;

$opt{H} = 2.0;
$opt{A} = 1.6;
$opt{B} = 2.4;

$opt{scale} = 1;
$opt{min} = 0;

sub main() {
    Opt::opt();
    print Opt::show();

    if ($opt{scale} != 1) {
	for my $cc (qw/ H A B /) {
	    $opt{$cc} *= $opt{scale};
	}
    }

    print "\n";
    print Opt::show();
    print "\n";
    my @data = ($opt{A}, $opt{B}, $opt{A} + $opt{B});
    if ($data[2] > 4) {
	print "illegal sum, A + B = $data[2]\n";
	exit(1);
    }
    print "data: ", join(" ", @data), "\n";

    my $ff = $opt{min} + $opt{H}/2;
    print "ff: $ff\n";
    my @area = map { $_ * $ff } @data;
    my @h = map { $_ / 4 } @area;
    my @d = map { $_ / $opt{H} } @data;
    my @u = map { $_ - $opt{min} } @h;
    my @l = map { $_ - $opt{H} } @u;
    my @x = ($u[0] * $d[0], $data[2] - $u[1] * $d[1], $u[2] * $d[0], $data[2] - $u[2]* $d[1]);

    print "area: ", join(" ", @area), "\n";
    print "h:    ", join(" ", @h), "\n";
    print "u:    ", join(" ", @u), "\n";
    print "l:    ", join(" ", @l), "\n";
    print "d:    ", join(" ", @d), "\n";
    print "x:    ", join(" ", @x), "\n";
    print "\n";

    # K1 ing
    print "K1: A + B
      \\fill[gray!30] (0,0) -- (0,$u[2]) -- ($x[2],0);
      \\fill[gray!30] (4,0) -- ($x[3],0) -- ($data[2],$h[2]) -- (4.0,$h[2]);
      \\fill[gray!60] ($x[2],0) -- ($data[0],$l[2]) -- ($x[3],0);
      \\draw (0,$h[2]) -- (0,$u[2]) -- ($data[0],$l[2]) -- ($data[2],$h[2]) -- (4,$h[2]);

K2: A
      \\fill[gray!30] (0,0) -- (0,$u[0]) -- ($x[0],0);
      \\fill[gray!30] (4,0) -- ($data[0],0) -- ($data[0],$h[0]) -- (4.0,$h[0]);
      \\fill[gray!60] ($x[0],0) -- ($data[0],$l[0]) -- ($data[0],0);
      \\draw (0,$h[0]) -- (0,$u[0]) -- ($data[0],$l[0]) -- ($data[0],$h[0]) -- (4,$h[0]);

K3: B
      \\fill[gray!30] (0,0) -- (0,$h[1]) -- ($data[0],$h[1]) -- ($data[0],0);
      \\fill[gray!30] (4,0) -- (4.0,$h[1]) -- ($data[2],$h[1]) -- ($x[1],0);
      \\fill[gray!60] ($data[0],0) -- ($data[0],$l[1]) -- ($x[1],0);
      \\draw (0,$h[1]) -- ($data[0],$h[1]) -- ($data[0],$l[1]) -- ($data[2],$u[1]) -- ($data[2],$h[1]) -- (4,$h[1]);
";


}

main();
