use utf8;
use Math::Cartesian::Product;

package Circle;

sub new {
    my ($class, $args) = @_;
    my $self = {
        x => $args->{x},
        y => $args->{y},
        r => $args->{r},
    };
    bless $self, $class;
}

sub show {
    my ($self, $args) = @_;
    sprintf "x =%7.3f  y =%7.3f  r =%7.3f\n", $args->{x}, $args->{y}, $args->{r};
}

package main;

sub circle {
    my($x,$y,$r) = @_;
    Circle->new({ x => $x, y=> $y, r => $r });
}

sub solve_Apollonius {
    my($c1, $c2, $c3, $s1, $s2, $s3) = @_;

    my $11 = 2 * $c2->{x} - 2 * $c1->{x};
    my $12 = 2 * $c2->{y} - 2 * $c1->{y};
    my $13 = $c1->{x}**2 - $c2->{x}**2 + $c1->{y}**2 - $c2->{y}**2 - $c1->{r}**2 + $c2->{r}**2;
    my $14 = 2 * $s2 * $c2->{r} - 2 * $s1 * $c1->{r};

    my $21 = 2 * $c3->{x} - 2 * $c2->{x};
    my $22 = 2 * $c3->{y} - 2 * $c2->{y};
    my $23 = $c2->{x}**2 - $c3->{x}**2 + $c2->{y}**2 - $c3->{y}**2 - $c2->{r}**2 + $c3->{r}**2;
    my $24 = 2 * $s3 * $c3->{r} - 2 * $s2 * $c2->{r};

    my $12 = $12 / $11;
    my $13 = $13 / $11;
    my $14 = $14 / $11;

    my $22 = $22 / $21 - $12;
    my $23 = $23 / $21 - $13;
    my $24 = $24 / $21 - $14;

    my $ = -$23 / $22;
    my $ = $24 / $22;
    my $ = -$12 * $ - $13;
    my $ = $14 - $12 * $;

    my $ = $**2 + $**2 - 1;
    my $ = 2 * $ * $ - 2 * $ * $c1->{x} + 2 * $ * $ - 2 * $ * $c1->{y} + 2 * $s1 * $c1->{r};
    my $ = $c1->{x}**2 + $**2 - 2 * $ * $c1->{x} + $**2 + $c1->{y}**2 - 2 * $ * $c1->{y} - $c1->{r}**2;

    my $ = $**2 - 4 * $ * $;
    my $rs = (-$ - sqrt $) / (2 * $);

    my $xs = $ + $ * $rs;
    my $ys = $ + $ * $rs;

    circle($xs, $ys, $rs);
}

$c1 = circle(0, 0, 1);
$c2 = circle(4, 0, 1);
$c3 = circle(2, 4, 2);

for (cartesian {@_} ([-1,1])x3) {
    print Circle->show( solve_Apollonius $c1, $c2, $c3, @$_);
}