<?php
// *** 配置文件(表示区域的三维数组)其内的点,必须按顺时针方向依次给出!
$area = array(
// 天通苑店
0 => array(
array('x'=>116.38295, 'y'=>40.09416),
array('x'=>116.44037, 'y'=>40.095898),
array('x'=>116.448275,'y'=>40.083313),
array('x'=>116.448455,'y'=>40.050818),
array('x'=>116.448275,'y'=>40.038307),
array('x'=>116.441448,'y'=>40.038418),
array('x'=>116.436058,'y'=>40.038804),
array('x'=>116.417302,'y'=>40.039136),
array('x'=>116.414822,'y'=>40.039384),
array('x'=>116.412738,'y'=>40.039329),
array('x'=>116.407672,'y'=>40.039329),
array('x'=>116.388628,'y'=>40.085162),
array('x'=>116.383633,'y'=>40.084997)
),
//亚运村
1 => array(
array('x'=>116.358804,'y'=>40.028474),
array('x'=>116.41608, 'y'=>40.02875),
array('x'=>116.41723, 'y'=>40.038915),
array('x'=>116.447988,'y'=>40.037921),
array('x'=>116.447844,'y'=>40.026761),
array('x'=>116.455821,'y'=>40.024164),
array('x'=>116.446281,'y'=>39.994736),
array('x'=>116.443532,'y'=>39.995372),
array('x'=>116.376267,'y'=>39.993493),
array('x'=>116.375908,'y'=>40.000015),
array('x'=>116.372027,'y'=>39.999904),
array('x'=>116.371452,'y'=>40.007366),
array('x'=>116.359451,'y'=>40.006758)
),
//望京店
2 => array(
array('x'=>116.46387, 'y'=>40.021125),
array('x'=>116.484495,'y'=>40.020462),
array('x'=>116.515684,'y'=>39.995151),
array('x'=>116.51519, 'y'=>39.976137),
array('x'=>116.491906,'y'=>39.972985),
array('x'=>116.476239,'y'=>39.977298),
array('x'=>116.467472,'y'=>39.96917),
array('x'=>116.443325,'y'=>39.984817),
array('x'=>116.449506,'y'=>39.993109),
array('x'=>116.446357,'y'=>39.994736),
array('x'=>116.456037,'y'=>40.024109)
),
//大悦城店
3 => array(
array('x'=>116.496424,'y'=>39.96253),
array('x'=>116.479527,'y'=>39.975491),
array('x'=>116.492921,'y'=>39.972491),
array('x'=>116.508533,'y'=>39.974454),
array('x'=>116.535231,'y'=>39.980225),
array('x'=>116.553485,'y'=>39.975691),
array('x'=>116.564624,'y'=>39.975028),
array('x'=>116.571307,'y'=>39.972097),
array('x'=>116.571666,'y'=>39.946989),
array('x'=>116.547736,'y'=>39.947763),
array('x'=>116.549245,'y'=>39.936755),
array('x'=>116.564624,'y'=>39.937142),
array('x'=>116.569367,'y'=>39.92995),
array('x'=>116.570085,'y'=>39.915175),
array('x'=>116.496424,'y'=>39.914843)
),
//北洼路
4 => array(
array('x'=>116.24763, 'y'=>39.978677),
array('x'=>116.280975,'y'=>39.976244),
array('x'=>116.322872,'y'=>39.991226),
array('x'=>116.323231,'y'=>39.986859),
array('x'=>116.339975,'y'=>39.986859),
array('x'=>116.340263,'y'=>39.982215),
array('x'=>116.346443,'y'=>39.98216),
array('x'=>116.354492,'y'=>39.963415),
array('x'=>116.361822,'y'=>39.963637),
array('x'=>116.362397,'y'=>39.957664),
array('x'=>116.37792, 'y'=>39.958106),
array('x'=>116.37138, 'y'=>39.929728),
array('x'=>116.367068,'y'=>39.929341),
array('x'=>116.366637,'y'=>39.924361),
array('x'=>116.37138, 'y'=>39.924361),
array('x'=>116.369655,'y'=>39.913626),
array('x'=>116.362325,'y'=>39.912962),
array('x'=>116.363188,'y'=>39.903774),
array('x'=>116.317194,'y'=>39.902999),
array('x'=>116.259415,'y'=>39.902778)
),
//安贞店
5 => array(
array('x'=>116.372171,'y'=>39.993161),
array('x'=>116.443676,'y'=>39.994985),
array('x'=>116.448419,'y'=>39.993548),
array('x'=>116.43807, 'y'=>39.97735),
array('x'=>116.438789,'y'=>39.9656),
array('x'=>116.440298,'y'=>39.955839),
array('x'=>116.42262, 'y'=>39.955673),
array('x'=>116.378531,'y'=>39.954815),
array('x'=>116.377704,'y'=>39.963941),
array('x'=>116.368003,'y'=>39.96383),
array('x'=>116.367679,'y'=>39.973341),
array('x'=>116.361247,'y'=>39.973479),
array('x'=>116.360529,'y'=>39.987025),
array('x'=>116.37235, 'y'=>39.987716)
),
//三元桥
6 => array(
array('x'=>116.283706,'y'=>40.114137 ),
array('x'=>116.354995,'y'=>40.121613 ),
array('x'=>116.369081,'y'=>40.114661 ),
array('x'=>116.37871, 'y'=>40.114772 ),
array('x'=>116.380435,'y'=>40.106826 ),
array('x'=>116.385897,'y'=>40.107543 ),
array('x'=>116.389346,'y'=>40.07782 ),
array('x'=>116.370949,'y'=>40.075998 ),
array('x'=>116.37174, 'y'=>40.05739 ),
array('x'=>116.325746,'y'=>40.042643 )
),
//团结湖
7 => array(
array('x'=>116.43189, 'y'=>39.955341),
array('x'=>116.440316,'y'=>39.955396),
array('x'=>116.438394,'y'=>39.977225),
array('x'=>116.442849,'y'=>39.984116),
array('x'=>116.467876,'y'=>39.969194),
array('x'=>116.476608,'y'=>39.976797),
array('x'=>116.494969,'y'=>39.963637),
array('x'=>116.496999,'y'=>39.914179),
array('x'=>116.433902,'y'=>39.914511)
),
//劲松店
8 => array(
array('x'=>116.4053, 'y'=>39.90632 ),
array('x'=>116.495418,'y'=>39.911412 ),
array('x'=>116.495418,'y'=>39.87709 ),
array('x'=>116.491322,'y'=>39.854717 ),
array('x'=>116.462432,'y'=>39.851892 ),
array('x'=>116.421362,'y'=>39.852141 ),
array('x'=>116.420895,'y'=>39.863412 ),
array('x'=>116.406809,'y'=>39.863412 ),
array('x'=>116.406881,'y'=>39.863357 )
),
//黄村店
9 => array(
array('x'=>116.280184,'y'=>39.776406 ),
array('x'=>116.314751,'y'=>39.788383 ),
array('x'=>116.337586,'y'=>39.805041 ),
array('x'=>116.353252,'y'=>39.804487 ),
array('x'=>116.356001,'y'=>39.794703 ),
array('x'=>116.364912,'y'=>39.799138 ),
array('x'=>116.367787,'y'=>39.783836 ),
array('x'=>116.378495,'y'=>39.781507 ),
array('x'=>116.383094,'y'=>39.766479 ),
array('x'=>116.388628,'y'=>39.76426 ),
array('x'=>116.387478,'y'=>39.749616 ),
array('x'=>116.383238,'y'=>39.748507 ),
array('x'=>116.385178,'y'=>39.728255 ),
array('x'=>116.352408,'y'=>39.727423 ),
array('x'=>116.335592,'y'=>39.705888 ),
array('x'=>116.301726,'y'=>39.697727 )
)
);
/*
*** 配置文件(表示区域的三维数组)其内的点,必须按顺时针方向依次给出!
*** 确定一点是否在一区域(多边形)内:
1:过这一点(x0, y0),画一水平线(y=y0),与多边形的所有边进行交点判断。
2:获取交点集(其中不含多边形的顶点)
3:若该点(x0, y0)的左侧和右侧交点个数均为奇数个,则该点在区域(多边形)内。否则:不在。
*** 返回结果:
return === false : 点不在区域内
return 0, 1, 2, 3 ... 点所在的区域编号(配置文件中的区域编号。)
*** Author : Guojunzhou / Eric
*** Main : php20141104@163.com
*/
class Area{
// 一个表示区域的三维数组
protected $config = null; // 包含每个区域的四边形
protected $rectangles = null; // 每个区域(多边形)的所有边
protected $lines = null; // 要判断的点的x, y坐标
protected $_x = null;
protected $_y = null; public function __construct($config){
$this->config = $config;
$this->initRectangles();
$this->initLines();
} /*
获取包含每个配送区域的四边形
*/
private function initRectangles(){
foreach ($this->config as $k => $v) {
$this->rectangles[$k]['minX'] = $this->getMinXInEachConfig($k);
$this->rectangles[$k]['minY'] = $this->getMinYInEachConfig($k);
$this->rectangles[$k]['maxX'] = $this->getMaxXInEachConfig($k);
$this->rectangles[$k]['maxY'] = $this->getMaxYInEachConfig($k);
}
} /*
初始化每个区域(多边形)的边(线段:直线的一部分【限制x或者y坐标范围】)
n 个顶点构成的多边形,有 n-1 条边
*/
private function initLines(){
foreach ($this->config as $k => $v) {
$pointNum = count($v); // 区域的顶点个数
$lineNum = $pointNum - 1; // 区域的边条数
for($i=0; $i<$lineNum; $i++){
// y=kx+b : k
if($this->config[$k][$i]['x'] - $this->config[$k][$i+1]['x'] == 0) $this->lines[$k][$i]['k'] = 0;
else $this->lines[$k][$i]['k'] =
($this->config[$k][$i]['y'] - $this->config[$k][$i+1]['y'])/($this->config[$k][$i]['x'] - $this->config[$k][$i+1]['x']);
// y=kx+b : b
$this->lines[$k][$i]['b'] = $this->config[$k][$i+1]['y'] - $this->lines[$k][$i]['k'] * $this->config[$k][$i+1]['x'];
$this->lines[$k][$i]['lx'] = min($this->config[$k][$i]['x'], $this->config[$k][$i+1]['x']);
$this->lines[$k][$i]['rx'] = max($this->config[$k][$i]['x'], $this->config[$k][$i+1]['x']);
}
$pointNum-=1;
if($this->config[$k][$pointNum]['x'] - $this->config[$k][0]['x'] == 0) $this->lines[$k][$pointNum]['k'] = 0;
else $this->lines[$k][$pointNum]['k'] =
($this->config[$k][$pointNum]['y'] - $this->config[$k][0]['y'])/($this->config[$k][$pointNum]['x'] - $this->config[$k][0]['x']);
// y=kx+b : b
$this->lines[$k][$pointNum]['b'] = $this->config[$k][0]['y'] - $this->lines[$k][$pointNum]['k'] * $this->config[$k][0]['x'];
$this->lines[$k][$pointNum]['lx'] = min($this->config[$k][$pointNum]['x'], $this->config[$k][0]['x']);
$this->lines[$k][$pointNum]['rx'] = max($this->config[$k][$pointNum]['x'], $this->config[$k][0]['x']);
}
} /*
获取一组坐标中,x坐标最小值
*/
private function getMinXInEachConfig($index){
$minX = 200;
foreach ($this->config[$index] as $k => $v) {
if($v['x'] < $minX){
$minX = $v['x'];
}
}
return $minX;
} /*
获取一组坐标中,y坐标最小值
*/
private function getMinYInEachConfig($index){
$minY = 200;
foreach ($this->config[$index] as $k => $v) {
if($v['y'] < $minY){
$minY = $v['y'];
}
}
return $minY;
} /*
获取一组坐标中,x坐标最大值
*/
public function getMaxXInEachConfig($index){
$maxX = 0;
foreach ($this->config[$index] as $k => $v) {
if($v['x'] > $maxX){
$maxX = $v['x'];
}
}
return $maxX;
} /*
获取一组坐标中,y坐标最大值
*/
public function getMaxYInEachConfig($index){
$maxY = 0;
foreach ($this->config[$index] as $k => $v) {
if($v['y'] > $maxY){
$maxY = $v['y'];
}
}
return $maxY;
} /*
获取 y=y0 与特定区域的所有边的交点,并去除和顶点重复的,再将交点分为左和右两部分
*/
private function getCrossPointInCertainConfig($index){
$crossPoint = null;
foreach ($this->lines[$index] as $k => $v) {
if($v['k'] == 0) return true;
$x0 = ($this->_y - $v['b']) / $v['k']; // 交点x坐标
if($x0 == $this->_x) return true; // 点在边上
if($x0 > $v['lx'] && $x0 < $v['rx']){
if($x0 < $this->_x) $crossPoint['left'][] = $x0;
if($x0 > $this->_x) $crossPoint['right'][] = $x0;
}
}
return $crossPoint;
} /*
检测一个点,是否在区域内
返回结果:
return === false : 点不在区域内
return 0, 1, 2, 3 ... 点所在的区域编号(配置文件中的区域编号。)
*/
public function checkPoint($x, $y){
$this->_x = $x;
$this->_y = $y;
$contain = null;
foreach ($this->rectangles as $k => $v) {
if($x > $v['maxX'] || $x < $v['minX'] || $y > $v['maxY'] || $y < $v['minY']){
continue;
}else{
$contain = $k;
break;
}
}
if($contain === null) return false;
$crossPoint = $this->getCrossPointInCertainConfig($contain);
if($crossPoint === true) return $contain;
if(count($crossPoint['left'])%2 == 1 && count($crossPoint['right'])%2 == 1) return $contain;
return false;
}
}
$area = new Area($area);
var_dump($area->checkPoint(116.531748,39.944229));
?>

  

https://blog.csdn.net/u014290054/article/details/47418065

判断一个点是否在某个区域内。百度,高德,腾讯都能用。(php版)的更多相关文章

  1. Android判断一个点是否在矩形区域内

    个人遇到的问题判断按钮的点击事件还是滑动事件 private boolean button1Down = false; private boolean button2Down = false; pri ...

  2. 判断一个点是否在多边形区域内--C算法

    /*函数的输入:(1)当前点的坐标p(2)区域顶点数组pt[]:(3)顶点数nCount 输出: 在区域内返回TRUE,否则返回FALSE.  Point类型是一个结构: struct Point { ...

  3. php 判断一个点是否在一个多边形区域内

    <?php class pointMap{ private static $coordArray; private static $vertx = []; private static $ver ...

  4. 百度地图java 判断当前位置是否在多边形区域内

    package com.haiyisoft.cAssistant.adapter.hessian; import java.awt.geom.Point2D;import java.util.Arra ...

  5. Mysql中判断一个点是否落在多边形内

    关于地理空间数据,经常需要处理两个空间数据的关联关系.有很多种方法可以处理,通过编写程序算法,或者是调用数据库中对应的function.在mysql数据库中,https://dev.mysql.com ...

  6. 推断一个点是否在某个区域内。百度,高德,腾讯都能用。(php版)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  7. iOS 如何判断一个点在某个指定区域中

    在iOS 开发中会遇到 判断位置的情况 iOS 自己都有函数实现的这些功能. 判断一个点是否在这个rect区域中 bool CGRectContainsPoint(CGRect rect,CGPoin ...

  8. Spring Data MongDB空间索引(判断一个点Point是否在一个区域Polygon内)

    这里要连接MongoDB数据库,在配置文件里:spring.data.mongodb.uri = mongodb://root:root@localhost:27017/happy 两个root分别是 ...

  9. java实现判断一个经纬度坐标是否在一个多边形内(经自己亲测)

    1.在高德地图上绘制的多边形:经纬度逗号分隔格式:上面是用来方便存坐标的对象:下面是方法测试:直接复制代码即可运行 public class Point { private Double x; pri ...

随机推荐

  1. 《前端之路》之 this 的使用技巧总结

    06: JS 中 this 的使用技巧总结 this 是 JavaScript 中的关键字. 一.基本认识 在 JS 中我们把 this 关键字当作成一个 快捷方式,用来引用当前调用者. 解释上面这句 ...

  2. spring-security实现的token授权

    在我的用户密码授权文章里介绍了spring-security的工作过程,不了解的同学,可以先看看用户密码授权这篇文章,在 用户密码授权模式里,主要是通过一个登陆页进行授权,然后把授权对象写到sessi ...

  3. springboot~环境搭建与Helloworld

    转了,非转了 只是项目需要,从.net到java,以后可以学习java的思想把它应用到.net上来,让咱们的.net越来越强大,springbool是一个强大的框架,几乎有了你想要的所有功能模块,大叔 ...

  4. 图像检索(3):BoW实现

    在上一篇文章中图像检索(2):均值聚类-构建BoF中,简略的介绍了基于sift特征点的BoW模型的构建,以及基于轻量级开源库vlfeat的一个简单实现. 本文重新梳理了一下BoW模型,并给出不同的实现 ...

  5. SLAM+语音机器人DIY系列:(一)Linux基础——3.Linux命令行基础操作

    摘要 由于机器人SLAM.自动导航.语音交互这一系列算法都在机器人操作系统ROS中有很好的支持,所以后续的章节中都会使用ROS来组织构建代码:而ROS又是安装在Linux发行版ubuntu系统之上的, ...

  6. #3 Python面向对象(二)

    前言 上一节主要记录面向对象编程的思想以及Python类的简单创建,这节继续深入类中变量的相关知识,Here we go! Python中类的各种变量 1.1 类变量 类变量定义:在类中,在函数体(方 ...

  7. webpack 样式表抽离成专门的单独文件并且设置版本号

    先以下的 css 的处理我们都把 mode 设置为 production. webpack4 开始使用: mini-css-extract-plugin插件, 1-3 的版本可以用: extract- ...

  8. DevExpress TreeList 禁止节点拖动到其他节点上

    背景 在做一个类似文件树的控件,支持节点从树上向其它的控件拖动程序,但是要保证树上的节点不能拖动上其他的节点上. 代码 /// <summary> /// 拖动节点完成 /// </ ...

  9. java压缩指定目录下的所有文件和文件夹的代码

    将代码过程较好的代码段备份一下,下边资料是关于java压缩指定目录下的所有文件和文件夹的代码,希望对码农有帮助. String sourceDir="E:\test";int pa ...

  10. jquery实现静态柱形图(写死的数据,只为系统首页UI更美观)

    这段时间比较空闲,便阅读公司做好的项目的源代码,学习学习同事的开发思路. 在项目中使用图表可以很好地提高人机交互的友好度,开发的时候看到项目的首页有两个很小的柱形图,很漂亮,便找到对应的代码看了看,发 ...