From: Breno G. de Oliveira Date: Sun, 4 Oct 2009 06:20:50 +0000 (-0300) Subject: implemented union and union_ip X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=26622c703eb0f38206a682cb391a93256811b771;p=sdlgit%2FSDL_perl.git implemented union and union_ip --- diff --git a/lib/SDL/Game/Rect.pm b/lib/SDL/Game/Rect.pm index 274c4b8..efab7e3 100644 --- a/lib/SDL/Game/Rect.pm +++ b/lib/SDL/Game/Rect.pm @@ -411,6 +411,60 @@ sub clip_ip { return; } +=head3 union($rect) + +Returns a new rectangle that completely covers the area of the current Rect and the one passed as an argument. There may be area inside the new Rect that is not covered by the originals. + +=cut + +sub _test_union { + my ($self, $rect) = (@_); + my ($x, $y, $w, $h); + + $x = $self->x < $rect->x ? $self->x : $rect->x; # MIN + $y = $self->y < $rect->y ? $self->y : $rect->y; # MIN + + $w = ($self->x + $self->w) > ($rect->x + $rect->w) + ? ($self->x + $self->w) - $x + : ($rect->x + $rect->w) - $x + ; # MAX + + $h = ($self->y + $self->h) > ($rect->y + $rect->h) + ? ($self->y + $self->h) - $y + : ($rect->y + $rect->h) - $y + ; # MAX + + return ($x, $y, $w, $h); +} + +sub union { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + my ($x, $y, $w, $h) = _test_union($self, $rect); + return $self->new($x, $y, $w, $h); +} + +sub union_ip { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + my ($x, $y, $w, $h) = _test_union($self, $rect); + + $self->x($x); + $self->y($y); + $self->w($w); + $self->y($h); + + return; +} + 42; __END__