メソッドの中で、同じクラスの別のメソッドを呼ぶ時は、$this->
が必要!という学びがあった。
3つのクラスを1つのクラスで呼び出せるように、書き直した!
2つの円の中心の座標を (xc, yc)、半径をそれぞれ r_1, r_2 (r_1 < r_2) とします。 暴風域にいる条件 r1^2 <= (x - xc)^2 + (y - yc)^2 <= r2^2 n は与えられる人の数を表す整数 1行目:xc yc r_1 r_2 ・1 ≦ n ≦ 100 ・1 ≦ r_1 < r_2 ≦ 100 ・-100 ≦ xc, yc, x_i, y_i ≦ 100 */ class CalTyphoonSize { public function __construct(private int $radius1, private int $radius2) { $this->radius1 = $radius1; $this->radius2 = $radius2; } // 小さい半径の2乗の計算 public function calSmallSize(): int { return pow($this->radius1, 2); } // 大きい半径の2乗の計算 public function calLargeSize(): int { return pow($this->radius2, 2); } } class RangeOfXAndY { public function __construct ( private int $x, private int $y, private int $xC, private int $yC ) { $this->x = $x; $this->y = $y; $this->xC = $xC; $this->yC = $yC; } public function rangeOfXAndY(): int { // 他のメソッドを呼び出す時は、$this-> が必要! $calX = $this->calX($this->x, $this->xC); $calY = $this->calY($this->y, $this->yC); return $calX + $calY; } private function calX(int $xPeople, int $xTyphoon): int { // (x - xc)^2 $calculatedX = abs($xPeople - $xTyphoon); return pow($calculatedX, 2); } private function calY($yPeople, $yTyphoon): int { $calculatedY = abs($yPeople - $yTyphoon); return pow($calculatedY, 2); } } class IsZone { public function __construct ( private int $calculatedR1, private int $calculatedR2, private int $sumXY ) { $this->calculatedR1 = $calculatedR1; $this->calculatedR2 = $calculatedR2; $this->sumXY = $sumXY; } public function isZone():bool { return ($this->calculatedR1 <= $this->sumXY && $this->sumXY <= $this->calculatedR2) ? true : false; } } class TyphoonZone { private $calTyphoonSize; private $rangeOfXAndY; private $isZone; public function __construct ( int $radius1, int $radius2, int $x, int $y, int $xC, int $yC ) { $this->calTyphoonSize = new CalTyphoonSize($radius1, $radius2); $this->rangeOfXAndY = new RangeOfXAndY($x, $y, $xC, $yC); $sizeOfSmallTyphoon = $this->calTyphoonSize->calSmallSize(); $sizeOfLargeTyphoon = $this->calTyphoonSize->calLargeSize(); $resultRangeOfXAndY = $this->rangeOfXAndY->rangeOfXAndY(); $this->isZone = new IsZone($sizeOfSmallTyphoon, $sizeOfLargeTyphoon, $resultRangeOfXAndY); } public function isInZone(): bool { return $this->isZone->isInZone(); } } fscanf(STDIN, "%d %d %d %d", $xC, $yC, $radius1, $radius2); fscanf(STDIN, "%d", $numberOfPeople); for ($i = 0; $i < $numberOfPeople; $i++) { fscanf(STDIN, "%d %d", $x, $y); $typhoonZone = new TyphoonZone($radius1, $radius2, $x, $y ,$xC, $yC); if ($typhoonZone->isInZone()) { echo 'yes' . PHP_EOL; } else { echo 'no' . PHP_EOL; } }