Curve that touches three points: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: pedantic) |
SqrtNegInf (talk | contribs) (Added Perl example) |
||
Line 57: | Line 57: | ||
dc.SavePNG("quadratic_curve.png") |
dc.SavePNG("quadratic_curve.png") |
||
}</lang> |
}</lang> |
||
=={{header|Perl}}== |
|||
Hilbert '''curve''' task code repeated here, with the addition that the 3 task-required points are marked. Mostly satisfies the letter-of-the-law of task specification while (all in good fun) subverting the spirit of the thing. |
|||
<lang perl>use SVG; |
|||
use List::Util qw(max min); |
|||
use constant pi => 2 * atan2(1, 0); |
|||
# Compute the curve with a Lindemayer-system |
|||
%rules = ( |
|||
A => '-BF+AFA+FB-', |
|||
B => '+AF-BFB-FA+' |
|||
); |
|||
$hilbert = 'A'; |
|||
$hilbert =~ s/([AB])/$rules{$1}/eg for 1..6; |
|||
# Draw the curve in SVG |
|||
($x, $y) = (0, 0); |
|||
$theta = pi/2; |
|||
$r = 5; |
|||
for (split //, $hilbert) { |
|||
if (/F/) { |
|||
push @X, sprintf "%.0f", $x; |
|||
push @Y, sprintf "%.0f", $y; |
|||
$x += $r * cos($theta); |
|||
$y += $r * sin($theta); |
|||
} |
|||
elsif (/\+/) { $theta += pi/2; } |
|||
elsif (/\-/) { $theta -= pi/2; } |
|||
} |
|||
$max = max(@X,@Y); |
|||
$xt = -min(@X)+10; |
|||
$yt = -min(@Y)+10; |
|||
$svg = SVG->new(width=>$max+20, height=>$max+20); |
|||
$points = $svg->get_path(x=>\@X, y=>\@Y, -type=>'polyline'); |
|||
$svg->rect(width=>"100%", height=>"100%", style=>{'fill'=>'black'}); |
|||
$svg->polyline(%$points, style=>{'stroke'=>'orange', 'stroke-width'=>1}, transform=>"translate($xt,$yt)"); |
|||
my $task = $svg->group( id => 'task-points', style => { stroke => 'red', fill => 'red' },); |
|||
$task->circle( cx => 10, cy => 10, r => 1, id => 'point1' ); |
|||
$task->circle( cx => 100, cy => 200, r => 1, id => 'point2' ); |
|||
$task->circle( cx => 200, cy => 10, r => 1, id => 'point3' ); |
|||
open $fh, '>', 'curve-3-points.svg'; |
|||
print $fh $svg->xmlify(-namespace=>'svg'); |
|||
close $fh;</lang> |
|||
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/curve-3-points.svg Hilbert curve passing through 3 defined points] (offsite image) |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |