Superellipse: Difference between revisions

Content added Content deleted
(Added Processing implementation)
(→‎{{header|Raku}}: simplified)
Line 1,310: Line 1,310:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>my (\a, \b, \n) = 200, 200, 2.5;
{{works with|rakudo|2018-10}}
Generate an svg image to STDOUT. Redirect into a file to capture it.
<lang perl6>constant a = 200;
constant b = 200;
constant n = 2.5;


# y in terms of x
# y in terms of x
sub y ($x) { sprintf "%d", b * (1 - ($x / a).abs ** n ) ** (1/n) }
sub y ($x) { floor b × (1 - ($x / a).abs ** n ) ** (1/n) }


# find point pairs for one quadrant
# find point pairs for one quadrant
my @q = flat map -> \x { x, y(x) }, (0, 1 ... 200);
my @q = flat map -> \x { x, y(x) }, ^(a+1);


my $out = open('superellipse.svg', :w);
# Generate an SVG image
$out.print: [~] qq|<svg height="{b×2}" width="{a×2}" xmlns="http://www.w3.org/2000/svg">\n|,
INIT say qq:to/STOP/;
pline( flat @q ),
<?xml version="1.0" standalone="no"?>
pline( flat @q «×» ( 1,-1) ), # flip and mirror
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
pline( flat @q «×» (-1,-1) ), # for the other
<svg height="{b*2}" width="{a*2}" version="1.1" xmlns="http://www.w3.org/2000/svg">
pline( flat @q «×» (-1, 1) ), # three quadrants
STOP
END say '</svg>';
'</svg>'
;

.put for
pline( @q ),
pline( @q «*» ( 1,-1) ), # flip and mirror
pline( @q «*» (-1,-1) ), # for the other
pline( @q «*» (-1, 1) ); # three quadrants


sub pline (@q) {
sub pline (@q) {
qq|<polyline points="{@q}"
qq:to/END/;
style="fill:none;stroke:black;stroke-width:3"
<polyline points="{@q}"
style="fill:none; stroke:black; stroke-width:3" transform="translate({a}, {b})" />
transform="translate({a}, {b})" />\n|
END
}</lang>
}</lang>
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/superellipse.svg Superellipse] (offsite image)
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/superellipse.svg Superellipse] (offsite image)