Thread: PHP GeoLocation
View Single Post
Old 12-28-2009, 07:57 PM   #13 (permalink)
Village Idiot
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,298
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by russellharrower View Post
First of all I want to thank you all,

But need to ask, (cos my math teacher did not teach us algebra, Can you make a small PHP example please.

Cos I don't understand the xPoint>(left function ...


I would be very greatful.
Thanks
You would probably be better off going on Google and looking up tutorials on mathematical functions than having me explaining it. But here is basically what I am doing (this is not all technically mathematically correct, but it is an easy way to get the job done). Function of Y means that you replace all the X variables in the equation with the given variable. Function of X means that you replace all the Y variables with the given value. As it ends up, I didn't give you any values to plug in, so that example wouldn't work.

You actually stuck me with some inspiration to write a script for this. Its been a long time since I've wrote anything in PHP so that was actually rather fun. The issue I've faced many times is that I know how to do most common tasks, this was actually a problem solving challenge so I did really enjoy making this.

PHP Code:
<?
class point
{
    function __construct($xParam,$yParam)
    {
        $this->x=$xParam;
        $this->y=$yParam;
    }

    public $x;
    public $y;
};

class equation
{
    function __construct($point1,$point2,$debug=false)
    {
        //Find the slope of the two points
        
        //If the line is horizontal
        if(($point2->y-$point1->y) == 0)
        {
            if($debug==true)
            
            $slope=0;
            $this->isHorizontal = true;        
            $this->isVertical = false;
            $this->y=$point2->y;
        }
        
        //If the line is vertical
        else if(($point2->x-$point1->x) == 0)
        {
            if($debug==true)
            $slope=0;
            $this->isVertical = true;
            $this->isHorizontal = false;
            $this->x=$point2->x;
        }
        else
        {
            if($debug==true)
            $slope = ($point2->y-$point1->y)/($point2->x-$point1->x);
            
            //Since vertical lines have no y intercept and horosontal lines don't need one to be stated, we only need to compute this if the line is at an angle
            $yInt=$point1->y-($slope*$point1->x);
            
            $this->isVertical = false;
            $this->isHorizontal = false;
        }
        $this->m = $slope;
        $this->b = $yInt;
    }
    
    public function solveForY($xParam)
    {
        if($this->isHorizontal == true)
        {
            return $this->y;
        }
        return $this->m*$xParam+$this->b;
    }
    public function solveForX($yParam)
    {
        //echo "x=$this->x m=$this->m yParam=$yParam b=$this->b ";
        if(isVertical == true)
        {
            return $this->x;
        }
        return ($yParam-$this->b)/$this->m;
    }

    public $m;
    public $b;
    
    
    private $isVertical;
    private $isHorizontal;
    public $x;
    public $y;
};

function isPointIn4Lines(&$pTopLine, &$pRightLine, &$pBottomLine, &$pLeftLine, &$testPoint)
{
    
    if(
        $testPoint->y < $pTopLine->solveForY($testPoint->x) &&// If the point lies below the top point
        $testPoint->y > $pBottomLine->solveForY($testPoint->x) &&// if the point lies above the bottom point
        $testPoint->x > $pLeftLine->solveForX($testPoint->y) && // if the point lies right of the left line
        $testPoint->x < $pRightLine->solveForX($testPoint->y) // If the point lies left of the right line
      )
    {
        return "true";
    }
    return "false";
}

//Application time
$point1 = new point(-2,3);
$point2 = new point(-2,0);
$point3 = new point(3,-4);
$point4 = new point(3,3);

$testPoint1 = new point(1,1);
$testPoint2 = new point(-1,-2);

//I don't feel like writing up a sorting function, you know how do ID them so do that yourself.
$topLeft = $point1;
$topRight = $point4;
$bottomLeft = $point2;
$bottomRight = $point3;



//Generate the lines
$topLine = new equation($topLeft,$topRight,true);
$rightLine = new equation($topRight,$bottomRight,true);
$bottomLine = new equation($bottomLeft,$bottomRight,true);
$leftLine = new equation($bottomLeft,$topLeft,true);

echo isPointIn4Lines($topLine,$rightLine,$bottomLine,$leftLine,$testPoint1) . " and " . isPointIn4Lines($topLine,$rightLine,$bottomLine,$leftLine,$testPoint2);
//will output "true and false"

?>
What this does it based off of four points, generates four lines. Then it tests to see if the given test points are within these four lines.
__________________

Village Idiot is offline  
Reply With Quote