lisp最小外接矩形算法_最小外接矩形思路以及实现

该博客介绍了如何使用LISP语言实现凸包算法,通过ConvexHull类获取几何对象的凸包,并进一步计算得到最小外接矩形。通过不断旋转并比较面积找到最小面积的矩形,最后返回旋转到正确位置的最小外接矩形。
摘要由CSDN通过智能技术生成

凸包算法实现

Geometry hull = (new ConvexHull(geom)).getConvexHull();

获得结果

public static Polygon get(Geometry geom, GeometryFactory gf) {

Geometry hull = (new ConvexHull(geom)).getConvexHull();

if (!(hull instanceof Polygon)) {

return null;

}

Polygon convexHull = (Polygon) hull;

System.out.println(convexHull);

// 直接使用中心值

Coordinate c = geom.getCentroid().getCoordinate();

System.out.println("==============旋转基点==============");

System.out.println(new GeometryFactory().createPoint(c));

System.out.println("==============旋转基点==============");

Coordinate[] coords = convexHull.getExteriorRing().getCoordinates();

double minArea = Double.MAX_VALUE;

double minAngle = 0;

Polygon ssr = null;

Coordinate ci = coords[0];

Coordinate cii;

for (int i = 0; i < coords.length - 1; i++) {

cii = coords[i + 1];

double angle = Math.atan2(cii.y - ci.y, cii.x - ci.x);

Polygon rect = (Polygon) Rotation.get(convexHull, c, -1 * angle, gf).getEnvelope();

double area = rect.getArea();

// 此处可以将 rotationPolygon 放到list中求最小值

// Polygon rotationPolygon = Rotation.get(rect, c, angle, gf);

// System.out.println(rotationPolygon);

if (area < minArea) {

minArea = area;

ssr = rect;

minAngle = angle;

}

ci = cii;

}

return Rotation.get(ssr, c, minAngle, gf);

}

测试类

@Test

public void test() throws Exception{

GeometryFactory gf = new GeometryFactory();

String wkt = "POLYGON ((87623.0828822501 73753.4143904365,87620.1073981973 73739.213216548,87629.1690996309 73730.4220136646,87641.882531493 73727.3112803367,87643.0997749692 73714.8683470248,87662.0346734872 73725.0120426595,87669.0676357939 73735.1557382941,87655.9484561064 73735.9672339449,87676.9120937514 73747.4634223308,87651.8909778525 73740.8362078495,87659.4649372597 73755.4431295634,87644.4522677204 73748.680665807,87645.5342619215 73760.7178512935,87635.2553170117 73750.9799034842,87630.5215923822 73760.3121034681,87623.0828822501 73753.4143904365))";

Polygon read = (Polygon) new WKTReader().read(wkt);

Polygon polygon = MinimumBoundingRectangle.get(read, gf);

// System.out.println(polygon);

// System.out.println(polygon.getArea());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值