Java多边形重心计算
多边形重心计算
三角形重心
- 顶点为a,b,c的三角形重心为x=(xa+xb+xc)/3,y=(ya+yb+yc)/3
多边形重心
- x=(x1w1+x2w2+…+xnwn)/W
- y=(y1w1+y2w2+…+ynwn)/W
importorg.locationtech.jts.geom.Coordinate; importorg.locationtech.jts.geom.Polygon; importorg.locationtech.jts.io.ParseException; importjava.util.*; importjava.util.stream.Collectors; /** *Title:PolygonNodeTriangle
*Description:多边形自身节点组成三角形
*@authorhuifer *@date2018/10/15 */ publicclassPolygonNodeTriangle{ privateintNUM=3; privateSetresult_p=newHashSet(); publicstaticvoidmain(String[]args){ //0 double[]point1=newdouble[]{0,0}; //1 double[]point2=newdouble[]{10,0}; //2 double[]point3=newdouble[]{20,0}; //3 double[]point4=newdouble[]{10,10}; ListallPoint=newArrayList(); allPoint.add(point1); allPoint.add(point3); allPoint.add(point4); PolygonNodeTrianglepolygonCenterPoint=newPolygonNodeTriangle(); //外围 Polygonwaiwei=polygonCenterPoint.waiwei(point1,point3,point4); //节点三角形 List sanjiaoxing=polygonCenterPoint.triangleMothed(allPoint); //外围内所有三角形 List rangeTriangle=polygonCenterPoint.getRangeTriangle(waiwei,sanjiaoxing); //重心xy double[]gravityCenterXY=polygonCenterPoint.getGravityCenterXY(rangeTriangle); System.out.println(rangeTriangle.size()); System.out.println("================================================"); double[]doubles=polygonCenterPoint.polygonGravityPoint("POLYGON((00,200,1010,00))"); } /*** *polygonwkt计算重心 *@paramwkt *@return */ privatedouble[]polygonGravityPoint(Stringwkt){ if(!wkt.startsWith("POLYGON")){ returnnull; } Operationoperation=newOperation(); //外围数据转list Polygonwaiwei=null; try{ waiwei=operation.createPolygonByWKT(wkt); }catch(ParseExceptione){ e.printStackTrace(); } Coordinate[]coordinates=waiwei.getCoordinates(); List allP=newArrayList<>(); Arrays.stream(coordinates).forEach( s->{ doublenowX=s.x; doublenowY=s.y; allP.add(newdouble[]{nowX,nowY}); } ); List polygons=triangleMothed(allP); List rangeTriangle1=getRangeTriangle(waiwei,polygons); doublearea=waiwei.getArea(); double[]gravityCenterXY1=getGravityCenterXY(rangeTriangle1); returngravityCenterXY1; } /*** *重心值 *@paramrangeTriangle *@return[x,y] */ privatedouble[]getGravityCenterXY(List rangeTriangle){ doublexArea=0.0; doubleyArea=0.0; doubleaArea=0.0; for(Polygontriangle:rangeTriangle){ Coordinate[]coordinates=triangle.getCoordinates(); doublearea=triangle.getArea(); double[]oneGR=triangleCenterOfGravity(coordinates[0],coordinates[1],coordinates[2]); xArea+=oneGR[0]*area; yArea+=oneGR[1]*area; aArea+=area; } System.out.println("重心X"+xArea/aArea); System.out.println("重心Y"+yArea/aArea); returnnewdouble[]{xArea/aArea,yArea/aArea}; } /*** *范围内三角形 *@paramwaiwei *@paramsanjiaoxing *@return */ privateList getRangeTriangle(Polygonwaiwei,List sanjiaoxing){ List triangle=newArrayList<>(); //判断三角形是否在面内 for(inti=0;i ceshimian=newArrayList(); ceshimian.add(point1); //ceshimian.add(point2); //ceshimian.add(point7); ceshimian.add(point4); //ceshimian.add(point6); //ceshimian.add(point5); ceshimian.add(point3); StringpolygonForList=createPolygonForList(ceshimian); Operationop=newOperation(); PolygonpolygonByWKT=null; try{ polygonByWKT=op.createPolygonByWKT(polygonForList); returnpolygonByWKT; }catch(ParseExceptione){ e.printStackTrace(); } returnnull; } /*** *生成所有三角形 *@paramallPoint *@return */ privateList triangleMothed(List allPoint){ //索引->点坐标 Map indexOfPoint=newHashMap(); for(inti=0;i allTriangle=newArrayList(); for(ObjectoneDataObj:result_p){ //这一行数据 SetoneDataList=(Set)oneDataObj; //这一行数据的三角形数据 List trianglePoint=newArrayList(); oneDataList.forEach( s->trianglePoint.add(indexOfPoint.get(s) )); Polygontriangle=createTriangle(trianglePoint); if(triangle!=null){ allTriangle.add(triangle); } } //所有三角形结束 returnallTriangle; } /*** *从点坐标集合中创建一个面 *@parampoints *@return */ privatestaticStringcreatePolygonForList(List points){ Stringend="))"; Stringres="POLYGON(("; Operationop=newOperation(); for(double[]point:points){ Stringx=Double.toString(point[0]); Stringy=Double.toString(point[1]); res+=x+""+y+","; } res+=Double.toString(points.get(0)[0])+""+Double.toString(points.get(0)[1]); res+=end; try{ op.createPolygonByWKT(res); }catch(ParseExceptione){ e.printStackTrace(); } returnres; } /*** *创建三角形 *@paramtrianglePoint *@returnpolygon */ privatestaticPolygoncreateTriangle(List trianglePoint){ Operationop=newOperation(); StringtriangleWkt; booleanisTri=isTriangle(trianglePoint); if(isTri){ triangleWkt="POLYGON(("+trianglePoint.get(0)[0]+""+trianglePoint.get(0)[1]+","+trianglePoint.get(1)[0]+""+trianglePoint.get(1)[1]+","+trianglePoint.get(2)[0]+""+trianglePoint.get(2)[1]+","+trianglePoint.get(0)[0]+""+trianglePoint.get(0)[1]+"))"; try{ PolygonpolygonByWKT=op.createPolygonByWKT(triangleWkt); returnpolygonByWKT; //returntriangleWkt; }catch(ParseExceptione){ e.printStackTrace(); } } returnnull; } /*** *判断三角形 *@paramtrianglePoint *@return */ privatestaticbooleanisTriangle(List trianglePoint){ double[]doubles=trianglePoint.get(0); double[]doubles1=trianglePoint.get(1); double[]doubles2=trianglePoint.get(2); doublelen=Math.sqrt(Math.pow(doubles[0]-doubles1[0],2)+Math.pow(doubles[1]-doubles1[1],2)); doublelen1=Math.sqrt(Math.pow(doubles[0]-doubles2[0],2)+Math.pow(doubles[1]-doubles2[1],2)); doublelen2=Math.sqrt(Math.pow(doubles1[0]-doubles2[0],2)+Math.pow(doubles1[1]-doubles2[1],2)); if((len+len1>len2)&&(len+len2>len1)&&(len1+len2>len)){ returntrue; } returnfalse; } /*** *不重复排列(元素不相同) *@paramdatas *@paramtarget */ privatevoidsort(Listdatas,Settarget){ if(target.size()==this.NUM){ this.result_p.add(target); return; } for(inti=0;i 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接