在本文中,我们通过圆的内接多边形法来实现。原理就是根据提供的圆心位置和半径,生成一个接近圆形的内接48边形,然后利用这个多边形对象再结合Filter进行空间查询。当然,内接多边形的边数越多就越接近圆,查询结果就越精确。示例代码如下:
//Java public Polygon createCirlePolygon(Point pnt,double dRadius){ double sineTheta, cosineTheta, twoPi; int numPoints = 48; //内接多边形的边数 Polygon poly=new Polygon(); Points pnts=new Points(); twoPi = 3.1415 * 2.0; //构造多边形 for(int i=0;i<numPoints;i++)
{ Point cpt = new Point(); sineTheta = Math.sin(twoPi * (i*1.0/ numPoints)); cosineTheta = Math.cos(twoPi * (i*1.0 / numPoints)); cpt.setX(pnt.getX() + dRadius * cosineTheta); cpt.setY(pnt.getY() + dRadius * sineTheta); pnts.addPointObject(cpt);
} pnts.addPointObject(pnts.getPointObject(0)); Ring ring=new Ring(); ring.setPoints(pnts); poly.addRing(ring); return poly; }
//ASP.NET public IMSPolygon createCirlePolygon(IMSPoint pnt,double dRadius) { IMSPolygon poly=new IMSPolygon(); double sineTheta, cosineTheta, twoPi; int numPoints = 48; //内接多边形的边数
IMSPoints pnts=new IMSPoints(); twoPi = 3.1415 * 2.0;
//构造多边形 for(int i=0;i<numPoints;i++) { IMSPoint cpt = new IMSPoint(); sineTheta = Math.Sin(twoPi * (i*1.0/ numPoints)); cosineTheta = Math.Cos(twoPi * (i*1.0 / numPoints)); cpt.X=(pnt.X + dRadius * cosineTheta); cpt.Y=(pnt.Y + dRadius * sineTheta); pnts.Add(cpt); } pnts.Add(pnts[0]); IMSParts parts=new IMSParts(); parts.Add(pnts); poly.Parts=parts; return poly; }
|