Bitmap/Bézier curves/Quadratic: 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 627:
 
method set-pixel ($i, $j, Pixel $p) {
return if $j >= $!height;
self.pixel($i, $j) = $p.clone;
}
Line 633 ⟶ 634:
}
 
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 663 ⟶ 664:
}
 
method dot (($px, $py), $pix, $radius = 2) {
for $px - $radius .. $px + $radius -> $x {
for $py - $radius .. $py + $radius -> $y {
Line 671 ⟶ 672:
}
 
method quadratic ( ($x1, $y1), ($x2, $y2), ($x3, $y3), $pix, $segments = 30 ) {
my @line-segments = map -> $t {
my \a = (1-$t)²;
Line 678 ⟶ 679:
(a*$x1 + b*$x2 + c*$x3).round(1),(a*$y1 + b*$y2 + c*$y3).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 695 ⟶ 696:
my Bitmap $b = Bitmap.new( width => 600, height => 400) but PPM;
 
$b.fill( color(622,632,632) );
 
my @points = (65,25), (85,380), (570,15);
 
my %seen;
$b.quadratic( |@points, color(0,255,0) );
my $c = 0;
for @points.permutations -> @this {
%seen{@this.reverse.join.Str}++;
next if %seen{@this.join.Str};
$b.quadratic( |@pointsthis, color(0,255-$c,0127,$c+=80) );
}
 
@points.map: { $b.dot( $^x, $^y_, color(255,2550,0), 3 )}
 
$*OUT.write: $b.P6;</lang>
10,343

edits