Home > other >  PHP check if point is inside a circle
PHP check if point is inside a circle

Time:02-08

I have codes below

circle.php

class point{
    private $x;
    private $y;

    function __construct($x, $y){
        $this->x = $x;
        $this->y = $y;
    }

    public function getX(){
        return $this->x;
    }

    public function getY(){
        return $this->y;
    }

}

class circle{
    private $x_circle;
    private $y_circle;
    private $radius;

    function __construct($x_circle, $y_circle, $radius){
        $this->x_circle = $x_circle;
        $this->y_circle = $y_circle;
        $this->radius = $radius;
    }

    public function getCircleX(){
        return $this->x_circle;
    }

    public function getCircleY(){
        return $this->y_circle;
    }

    public function getRadius(){
        return $this->radius;
    }

    public function checkIfInside(){
        if(pow(getX() - getCircleX(), 2)   pow(getY() - getCircleY(), 2) < pow(getRadius(),2)){
            return true;
        } else {
            return false;
        }
       
    }
}

index.php

include 'circle.php';

$point= new point(3, 4);
$circle= new circle(10, 10, 100);
$circle->checkIfInside();

I try to check if point is inside a circle. With point class we declare point coordinates(x,y). With circle class we declare the coordinates of circle(x,y) and radius. But I get this error: Uncaught Error: Call to undefined function getX()

CodePudding user response:

You need to fix your checkIfInside function. It should receive a point as an argument. Also it lacks $point and $this references when calling methods.

public function checkIfInside($point) {
    return pow($point->getX() - $this->getCircleX(), 2)   pow($point->getY() - $this->getCircleY(), 2) < pow($this->getRadius(),2))      
}

Also, you should pass the point as an argument in your main code.

include 'circle.php';

$point= new point(3, 4);
$circle= new circle(10, 10, 100);
$circle->checkIfInside($point);

CodePudding user response:

The main issues:

  • in checkIfInside methods are called as if they are global functions. You should instead include the object on which the method should be called, using the -> notation. For instance, it should not be getCircleX(), but $this->getCircleX()

  • This function should know which point you want to check with, so it should be an argument: public function checkIfInside($point). The caller should pass this as argument in the call.

  • Not a problem, but if (boolean) { return true; } else { return false; } is an anti-pattern. Just return the boolean.

Corrected method:

    public function checkIfInside($point){
        return pow($point->getX() - $this->getCircleX(), 2)   pow($point->getY() - $this->getCircleY(), 2) 
            < pow($this->getRadius(),2);
    }

Corrected call in main program:

$result = $circle->checkIfInside($point);

CodePudding user response:

getX() is not a global function, but a method of the point class. Methods must be called as $object->method(). (While inside a method such as checkIfInside(), the reference to the current object is $this.)

The same applies to the other methods you are calling in the statement below:

if(pow(getX() - getCircleX(), 2)   pow(getY() - getCircleY(), 2) < pow(getRadius(),2)){
  •  Tags:  
  • Related