概述
packagecom.skjd.util;importjava.util.ArrayList;importjava.util.List;/*** 坐标以及电子围栏相关的工具类
*@authorzhangyao
**/
public classPointUtil {public static voidmain(String[] args) {
String str="114.316587,30.671626#114.325921,30.683437#114.342122,30.689342#114.359009,30.695246#114.377066,30.692884#114.393304,30.678417#114.412289,30.650953#114.426107,30.625253#114.433703,30.595853#114.444368,30.560458#114.466705,30.527415#114.457264,30.517327#114.454002,30.503097#114.458465,30.474631#114.430828,30.466313#114.397697,30.460362#114.343281,30.456778#114.305086,30.457354#114.269101,30.468743#114.23449,30.489598#114.182713,30.522872#114.17296,30.535364#114.167328,30.55022#114.165783,30.575754#114.167155,30.591715#114.161492,30.606935#114.160461,30.619049#114.170589,30.627469#114.206123,30.623481#114.233932,30.629832#114.184837,30.704361#114.187927,30.776472#114.233589,30.780122#114.238567,30.717035#114.248609,30.66835#114.284744,30.659813#";
}/*** 传入字符串类型的坐标组;经纬度“,”分隔;坐标之间“#”分隔;以“#”好结束
*@return
*/
public static boolean is(String str,double x,doubley){
List list =getxyUtil(str);
Point[] ps2= newPoint[list.size()];for(int i=0;i
ps2[i]=list.get(i);
}
Point n1= newPoint(x,y);if(isPtInPoly(n1.getX() , n1.getY() , ps2)){return true;
}return false;
}/*** 传入经度,维度;和需要判断的多边形坐标组
*@paramALon
*@paramALat
*@paramps
*@return
*/
public static boolean isPtInPoly (double ALon , doubleALat , Point[] ps) {intiSum, iCount, iIndex;double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;if (ps.length < 3) {return false;
}
iSum= 0;
iCount=ps.length;for (iIndex = 0; iIndex
dLon1=ps[iIndex].getX();
dLat1=ps[iIndex].getY();
dLon2= ps[0].getX();
dLat2= ps[0].getY();
}else{
dLon1=ps[iIndex].getX();
dLat1=ps[iIndex].getY();
dLon2= ps[iIndex + 1].getX();
dLat2= ps[iIndex + 1].getY();
}//以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上
if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat 0) {//得到 A点向左射线与边的交点的x坐标:
dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 -dLat2);//如果交点在A点左侧(说明是做射线与 边的交点),则射线与边的全部交点数加一:
if (dLon
iSum++;
}
}
}
}if ((iSum % 2) != 0) {return true;
}return false;
}/*** 传入字符串坐标组;转为集合;114.288221,30.593268#114.321866,30.580559#114.311223,30.560015#
*@paramstr
*@return
*/
public static ListgetxyUtil(String str){
List list = new ArrayList<>();while (str.indexOf("#")!=-1) {
String str1= str.substring(0, str.indexOf("#"));//40.01,40.12
Point pd = new Point(Double.valueOf(str1.substring(0, str1.indexOf(","))),Double.valueOf(str1.substring(str1.indexOf(",")+1)));
list.add(pd);
str=str.substring(str.indexOf("#")+1);
}while (str.indexOf(";")!=-1) {
String str1= str.substring(0, str.indexOf(";"));//40.01,40.12
Point pd = new Point(Double.valueOf(str1.substring(0, str1.indexOf(","))),Double.valueOf(str1.substring(str1.indexOf(",")+1)));
list.add(pd);
str=str.substring(str.indexOf(";")+1);
}returnlist;
}/*** 获取多边形的中心
*@paramstr
*@return
*/
public staticPageData getCenterOfGravityPoint(String str ) {
PageData data= newPageData();
List mPoints =getxyUtil(str);double area = 0.0;//多边形面积
double Gx = 0.0, Gy = 0.0;//重心的x、y
for (int i = 1; i <= mPoints.size(); i++) {double iLat = mPoints.get(i %mPoints.size()).getX();double iLng = mPoints.get(i %mPoints.size()).getY();double nextLat = mPoints.get(i - 1).getX();double nextLng = mPoints.get(i - 1).getY();double temp = (iLat * nextLng - iLng * nextLat) / 2.0;
area+=temp;
Gx+= temp * (iLat + nextLat) / 3.0;
Gy+= temp * (iLng + nextLng) / 3.0;
}
Gx= Gx /area;
Gy= Gy /area;
data.put("x", String.valueOf(Gx).substring(0, String.valueOf(Gx).indexOf(".")+7));
data.put("y", String.valueOf(Gy).substring(0, String.valueOf(Gy).indexOf(".")+7));returndata;
}
}classPoint {privateDouble x;privateDouble y;publicPoint (Double x , Double y) {this.x =x;this.y =y;
}publicDouble getX() {returnx;
}public voidsetX(Double x) {this.x =x;
}publicDouble getY() {returny;
}public voidsetY(Double y) {this.y =y;
}
}
最后
以上就是单薄蜻蜓为你收集整理的java计算一个多边形的重心_java代码获取多边形的中心点的全部内容,希望文章能够帮你解决java计算一个多边形的重心_java代码获取多边形的中心点所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复