I have the following code, now I want to calculate the distance between the points I have created, but I can't find a way to call the distance method...
<?php
class puntoR2{
public $x;
public $y;
public function __construct($x, $y){
$this->x = $x;
$this->y =$y;
}
public function mostrar(){
echo "El par ordenado es: ($this->x, $this->y)";
}
public function distancia($a1, $b1){
$dx = $a1 -> $this->x - $b1 -> $this->x;
$dy = $a1 -> $this->y - $b1 -> $this->y;
$distancia = sqrt($dx*$dx + $dy*$dy);
echo "La distancia entre los puntos es: $distancia";
}
}
$p1 = new puntoR2(5, 4);
$p2 = new puntoR2(4, 8);
?>
I leave you an example of how it could be
You can create a function
distanciaCon
to compare the distance of the point to another point.You can create a static function that compares the distance of 2 points
(I mixed both methods)
Also I corrected your distance function, you had:
And it should be: