Home > Backend Development > PHP Tutorial > PHP determines whether a point is inside or outside the polygon area

PHP determines whether a point is inside or outside the polygon area

藏色散人
Release: 2023-04-08 06:18:02
forward
4874 people have browsed it

PHP determines whether a point is inside or outside the polygon area;

According to the ray method of mathematical knowledge, if the number of points that the ray intersects with the geometric polygon is an odd number, it is inside the geometry;

An even number Externally;

/**
 * Created by PhpStorm.
 * function: inArea
 * Description: 判断点是否在多边形区域内
 * User: Xiaoxie
 * @param $x 
 * @param $y
 * @param $arr 几何订单坐标
 * @return int
 *
 */
public function inArea($x,$y,$arr)
{
    //点的数量
    $count = count($arr);
    $n = 0; //点与线相交的个数
    $bool = 0;//外
    for ($i = 0, $j = $count - 1; $i < $count; $j = $i, $i++) {
        //两个点一条线 取出两个连接点的定点
        $px1 = $arr[$i][0];
        $py1 = $arr[$i][1];
        $px2 = $arr[$j][0];
        $py2 = $arr[$j][1];
        //$x的水平位置画射线
        if($x>=$px1 || $x>= $px2)
        {
            //判断$y 是否在线的区域
            if(($y>=$py1 && $y<=$py2) || ($y>=$py2 && $y<= $py1)){
 
 
                    if (($y == $py1 && $x == $px1) || ($y == $py2 && $x == $px2)) {
 
                       #如果$x的值和点的坐标相同
                        $bool = 2;//在点上
                        return $bool;
 
                    }else{
                        $px = $px1+($y-$py1)/($py2-$py1)*($px2-$px1) ;
                        if($px ==$x)
                        {
                            $bool = 3;//在线上
                        }elseif($px< $x){
                            $n++;
                        }
 
                    }
            }
        }
 
    }
    if ($n%2 != 0) {
        $bool = 1;
    }
    return $bool;
}
Copy after login

Test Array

$arr = [
    [&#39;9.4&#39;,&#39;12.04&#39;],
    [&#39;6.68&#39;,&#39;8.61&#39;],
    [&#39;9.05&#39;,&#39;6.06&#39;],
    [&#39;6.24&#39;,&#39;3.87&#39;],
    [&#39;10.02&#39;,&#39;2.55&#39;],
 
    [&#39;14.06&#39;,&#39;4.13&#39;],
 
    [&#39;16.35&#39;,&#39;7.56&#39;],
 
    [&#39;11.69&#39;,&#39;8.35&#39;],
];
 
$x =15.73;
$y = 5.62;
//在外
$x = 9.97;
$y = 4.96; //在内
Copy after login

 PHP determines whether a point is inside or outside the polygon area

For more PHP-related knowledge, please visit PHP Tutorial!

The above is the detailed content of PHP determines whether a point is inside or outside the polygon area. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template