#======================================================== # Rect #======================================================== package Rect; use Common; @ISA = qw(Common); #公開したいサブルーチン #@EXPORT = qw(DelSpace Reduce01 MakePath RegExpQuote); use strict; use Utils; sub new { my ($module, $x0, $y0, $x1, $y1) = @_; my $this = {}; bless $this; $this->SetRect($x0, $y0, $x1, $y1); return $this; } sub DESTROY { my $this = shift; } sub x0 { return shift->{'x0'}; }; sub x1 { return shift->{'x1'}; }; sub y0 { return shift->{'y0'}; }; sub y1 { return shift->{'y1'}; }; sub SetRect { my ($this, $x0, $y0, $x1, $y1) = @_; #print "Set1 x0=$x0 y0=$y0 x1=$x1 y1=$y1\n"; return unless(defined $x0); Utils::Sort($x0, $x1); Utils::Sort($y0, $y1); #print "Set2 x0=$x0 y0=$y0 x1=$x1 y1=$y1\n"; $this->{'x0'} = $x0; $this->{'y0'} = $y0; $this->{'x1'} = $x1; $this->{'y1'} = $y1; } sub Merge { my ($this, $x0, $y0, $x1, $y1) = @_; unless(defined $y0) { #print "0 x0=", $x0->x0(), " y0=", $x0->y0(), # " x1=", $x0->x1(), " x1=", $x0->{'x1'}, " y1=", $x0->y1(), "\n"; return $this->MergeByBox( $x0->x0(), $x0->y0(), $x0->x1(), $x0->y1() ); } #print "1 x0=$x0 y0=$y0 x1=$x1 y1=$y1\n"; return $this->MergeByBox($x0, $y0, $x1, $y1); } sub MergeByBox { my ($this, $x0, $y0, $x1, $y1) = @_; #print "1a x0=$x0 y0=$y0 x1=$x1 y1=$y1\n"; Utils::Sort($x0, $x1) if(defined $x0 and defined $x1); Utils::Sort($y0, $y1) if(defined $y0 and defined $y1); #print "2a x0=$x0 y0=$y0 x1=$x1 y1=$y1\n"; unless(defined $this->{'x0'}) { $this->{'x0'} = $x0 if(defined $x0); $this->{'y0'} = $y0 if(defined $y0); $this->{'x1'} = $x1 if(defined $x1); $this->{'y1'} = $y1 if(defined $y1); } $this->{'x0'} = $x0 if(defined $x0 and $x0 < $this->{'x0'}); $this->{'y0'} = $y0 if(defined $y0 and $y0 < $this->{'y0'}); $this->{'x1'} = $x1 if(defined $x1 and $this->{'x1'} < $x1); $this->{'y1'} = $y1 if(defined $y1 and $this->{'y1'} < $y1); return $this; } sub IsInside { my ($this, $x, $y) = @_; my $x0 = $this->{'x0'}; my $y0 = $this->{'y0'}; my $x1 = $this->{'x1'}; my $y1 = $this->{'y1'}; my $r = ($x - $x0) / ($x1 - $x0); if($r < 0 or 1.0 < $r) { # print "X outside\n"; return 0; } # print "X inside\n"; $r = ($y - $y0) / ($y1 - $y0); if($r < 0 or 1.0 < $r) { # print "Y outside\n"; return 0; } # print "Y inside\n"; return 1; } 1;