2012-03-01 09:46:13 -08:00
|
|
|
package DDG::Goodie::Perimeter;
|
2014-08-20 11:45:33 -07:00
|
|
|
# ABSTRACT: Compute the perimeter of basic shapes.
|
2012-03-01 09:46:13 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2012-03-01 09:46:13 -08:00
|
|
|
use DDG::Goodie;
|
|
|
|
|
|
|
|
triggers start => "perimeter", "circumference";
|
|
|
|
|
|
|
|
zci is_cached => 1;
|
2012-03-20 21:08:12 -07:00
|
|
|
zci answer_type => "perimeter";
|
2012-05-23 19:05:08 -07:00
|
|
|
|
2012-03-01 09:46:13 -08:00
|
|
|
handle query_lc => sub {
|
|
|
|
if ($_ =~ m/^(?:circumference (?:of\s|)(?:circle\s|)(\d+(?:\.\d+)?))|(?:(perimeter) (?:of\s|)(?:(square|circle|pentagon|hexagon|octagon) (\d+(?:\.\d+)?)|(rectangle) (\d+(?:\.\d+)?)[,;]?\s(\d+(?:\.\d+)?)|(triangle) (\d+(?:\.\d+)?)[,;]?\s(\d+(?:\.\d+)?)[,;]?\s(\d+(?:\.\d+)?)))$/) {
|
|
|
|
my %polygons = ("pentagon" => 5, "hexagon" => 6, "octagon" => 8);
|
|
|
|
my $shape = $1 ? "circle" : $3 || $5 || $8;
|
2014-10-28 11:36:15 -07:00
|
|
|
|
2015-07-31 06:29:58 -07:00
|
|
|
my $subtitle = "Perimeter of $shape with sides of ";
|
2012-03-01 09:46:13 -08:00
|
|
|
my $answer;
|
|
|
|
|
|
|
|
if ($shape eq "square") {
|
2015-07-31 06:29:58 -07:00
|
|
|
$subtitle .= $4;
|
2012-03-01 09:46:13 -08:00
|
|
|
$answer = $4 * 4;
|
|
|
|
} elsif ($shape eq "rectangle") {
|
2015-07-31 06:29:58 -07:00
|
|
|
$subtitle .= "$6 and $7";
|
2012-03-01 09:46:13 -08:00
|
|
|
$answer = ($6 * 2) + ($7 * 2)
|
|
|
|
} elsif ($shape eq "triangle") {
|
2015-07-31 06:29:58 -07:00
|
|
|
$subtitle .= "$9, $10 and $11";
|
2012-03-01 09:46:13 -08:00
|
|
|
$answer = $9 + $10 + $11;
|
|
|
|
} elsif ($shape eq "circle") {
|
2015-07-31 06:29:58 -07:00
|
|
|
$subtitle = "Circumference of circle with radius of ".($1 || $4);
|
2012-03-01 09:46:13 -08:00
|
|
|
$answer = (2 * 3.14159265358979323846) * ($1 || $4);
|
|
|
|
} else {
|
2015-07-24 05:48:13 -07:00
|
|
|
if (substr $shape, index($shape, "agon") eq "agon") {
|
2015-07-31 06:29:58 -07:00
|
|
|
$subtitle .= $4;
|
2015-07-24 05:48:13 -07:00
|
|
|
$answer = $4 * $polygons{$shape};
|
|
|
|
}
|
2012-03-01 09:46:13 -08:00
|
|
|
}
|
2014-10-28 11:36:15 -07:00
|
|
|
|
2015-07-24 05:48:13 -07:00
|
|
|
return $answer, structured_answer => {
|
|
|
|
data => {
|
|
|
|
title => $answer,
|
|
|
|
subtitle => $subtitle
|
|
|
|
},
|
|
|
|
templates => {
|
|
|
|
group => 'text',
|
|
|
|
moreAt => 0
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 09:46:13 -08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
1;
|