The problem I am having is that the slope of the triangle is not smooth
The problem you are seeing is an inherent effect of digital images often referred to as "the jaggies".
The technique to lessen the apparent jagginess is called AntiAliasing. Recent versions of GD have support for antialiasing built-in, but they require the programmer to use them. This basically consists of using setAntiAliased( $color ) to set the color you wish to draw in and then passing gdAntiAliased in place of the color on the actual drawing primative. A simple demonstration of using them:
#! perl -slw
use strict;
use GD;
our $X ||= 500;
our $Y ||= 500;
my $img = new GD::Image( 2*$X, $Y, 1 );
my $white = $img->colorAllocate( 255, 255, 255 );
my $black = $img->colorAllocate( 0, 0, 0 );
$img->filledRectangle( 0,0, $img->getBounds, $white );
## No anti-aliasing
for my $y ( map{ $_ * 20 } 0 .. $Y/ 20 ) {
$img->line( 0, 0, $X, $y, $black );
}
for my $x ( map{ $_ * 20 } 0 .. $X/20 ) {
$img->line( 0, 0, $x, $Y, $black );
}
## Same thing antialiased
$img->setAntiAliased( $black );
for my $y ( map{ $_ * 20 } 0 .. $Y/20 ) {
$img->line( $X, 0, 2*$X, $y, gdAntiAliased );
}
for my $x ( map{ $_ * 20 } 0 .. $X/20 ) {
$img->line( $X, 0, $X+$x, $Y, gdAntiAliased );
}
open PNG, '>:raw', 'test.png' or die $!;
print PNG $img->png( 9 );
close PNG;
system "test.png"; ## Adjust to start your favourite picture viewer.
The left-hand half of the image is drawn without antialiasing, the right-hand half with.
I'm not convinced that libgd's antialiasing algorithm is the best around, but it does make a difference.
BTW. As you appear to have made the transition from using gifs to pngs, there is no reason not to use full compression $img->png( 9 ); when saving your pngs to disk.
Being a non-lossy algorithm the compression doesn't affect the quality of the image as it does with jpgs, and it saves a huge amount of disk space.
It will take a few milliseconds longer to decompress the images, but generally, the time saved loading the compressed image into memory (especially across the network) will more than compensate for the extra time spent decompressing it.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
|