Jump to content

Superellipse: Difference between revisions

(Added Processing implementation)
(→‎{{header|Raku}}: simplified)
Line 1,310:
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>constantmy (\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
sub y ($x) { sprintf "%d",floor b *× (1 - ($x / a).abs ** n ) ** (1/n) }
 
# find point pairs for one quadrant
my @q = flat map -> \x { x, y(x) }, ^(0, a+1 ... 200);
 
my $out = open('superellipse.svg', :w);
# Generate an SVG image
$out.print: [~] qq|<svg height="{b*2b×2}" width="{a*2a×2}" version="1.1" 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>';
;
 
.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) {
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})" />\n|
END
}</lang>
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/superellipse.svg Superellipse] (offsite image)
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.