Superellipse: Difference between revisions

Added Perl example
(Added Perl example)
Line 645:
 
::requires rxMath library</lang>
 
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>my $a = 200;
my $b = 200;
my $n = 2.5;
 
# y in terms of x
sub y_from_x {
my($x) = @_;
int $b * abs(1 - ($x / $a) ** $n ) ** (1/$n)
}
 
# find point pairs for one quadrant
push @q, $_, y_from_x($_) for 0..200;
 
# Generate an SVG image
open $fh, '>', 'superellipse.svg';
print $fh
qq|<svg height="@{[2*$b]}" width="@{[2*$a]}" xmlns="http://www.w3.org/2000/svg">\n|,
pline( 1, 1, @q ),
pline( 1,-1, @q ), # flip and mirror
pline(-1,-1, @q ), # for the other
pline(-1, 1, @q ), # three quadrants
'</svg>';
 
sub pline {
my($sx,$sy,@q) = @_;
 
for (0..$#q/2) {
$q[ 2*$_] *= $sx;
$q[1+2*$_] *= $sy;
}
 
qq|<polyline points="@{[join ' ',@q]}"
style="fill:none;stroke:black;stroke-width:3"
transform="translate($a, $b)" />\n|
}</lang>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/superellipse.svg Superellipse] (offsite image)
 
=={{header|Perl 6}}==
2,392

edits