in reply to color problem

The simple approach is to draw the elements from back to front.

The hard part is to find out which element is in the back and which is in the front, the "z-order" of the elements.

Replies are listed 'Best First'.
Re^2: color problem
by Anonymous Monk on Dec 29, 2013 at 18:51 UTC

    Let's assume for a second i knew which object are in the back, even then, how do i need to draw this? I've added my code.

    use GD; use constant PI => 4 * atan2(1, 1); $im = new GD::Image(400,400); $black = $im->colorAllocate(0,0,0); $red=$im->colorAllocate(255,0,0); $green=$im->colorAllocate(0,255,0); $im->filledEllipse(200,200,50,50,$red); $im->filledEllipse(300,200,25,25,$green); for($xy=0;$xy<6.28;$xy+=0.01){ $x = 100*cos($xy); $y = (43.75/2)*sin($xy); push @punten,[200+$x,200+$y]; } $data =$im->gifanimbegin(1,0); $data.=$im->gifanimadd(1,0,0,1); for($z=0;$z<=$#punten;$z+=20){ $ima = new GD::Image(400,400); $black = $ima->colorAllocate(0,0,0); $red=$ima->colorAllocate(255,0,0); $green=$ima->colorAllocate(0,255,0); $ima->filledEllipse(200,200,50,50,$red); $xc = $punten[$z][0]; $yc = $punten[$z][1]; $ima->filledEllipse($xc,$yc,25,25,$green); $data.= $ima->gifanimadd(1,0,0,1); } $data.=$ima->gifanimend; binmode STDOUT; print $data;

      Instead of drawing $red and then drawing $green, you need to find out which of the two is in the back, and draw that one first.

      For example, do it like this:

      # Define the two planets my @large= ($red, 200,200,50,50 ); my @small= ($green, $xc, $yc, 25, 25 ); my( $front, $back ); if( $red_is_in_front_of_green ) { $front= \@large; $back= \@small; } else { $front= \@small; $back= \@large; }; # Just in case we ever go beyond two objects my @objects_to_draw= ($back, $front); for my $item (@objects_to_draw) { my( $color, @position )= @$item; $ima->filledEllipse( @position, $color ); };

        Thank you.