Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP check if point is inside a circle

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading