Jump to content

Bitmap/Bézier curves/Cubic: Difference between revisions

→‎{{header|Perl 6}}: Updated to show that the end and control points can be in any order
m (→‎{{header|Perl 6}}: remove some debug code)
(→‎{{header|Perl 6}}: Updated to show that the end and control points can be in any order)
Line 805:
}
 
method line(($x0 is copy, $y0 is copy), ($x1 is copy, $y1 is copy), $pix) {
my $steep = abs($y1 - $y0) > abs($x1 - $x0);
if $steep {
Line 835:
}
 
method dot (($px, $py), $pix, $radius = 2) {
for $px - $radius .. $px + $radius -> $x {
for $py - $radius .. $py + $radius -> $y {
Line 843:
}
 
method cubic ( ($x1, $y1), ($x2, $y2), ($x3, $y3), ($x4, $y4), $pix, $segments = 30 ) {
my @line-segments = map -> $t {
my \a = (1-$t)³;
Line 851:
(a*$x1 + b*$x2 + c*$x3 + d*$x4).round(1),(a*$y1 + b*$y2 + c*$y3 + d*$y4).round(1)
}, (0, 1/$segments, 2/$segments ... 1);
for @line-segments.rotor(2=>-1) -> (($x1p1, $y1),($x2,$y2)p2) { self.line( $x1,$y1,$x2p1, $y2p2, $pix) };
}
 
Line 868:
my Bitmap $b = Bitmap.new( width => 600, height => 400) but PPM;
 
$b.fill( color(622,632,632) );
 
my @points = (5585,390), (5,5), (580,370), 570(270,10);
 
my %seen;
$b.cubic( |@points, color(255,0,255) );
my $c = 0;
for @points.permutations -> @this {
%seen{@this.reverse.join.Str}++;
next if %seen{@this.join.Str};
$b.cubic( |@pointsthis, color(255-$c,0127,255$c+=22) );
}
 
@points.map: { $b.dot( $^x, $^y_, color(255,0,0), 3 )}
 
$*OUT.write: $b.P6;</lang>
10,327

edits

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