日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

最小外接矩形思路以及实现

發(fā)布時(shí)間:2025/7/25 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最小外接矩形思路以及实现 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最小外接矩形

外接矩形計(jì)算

對(duì)一個(gè)凸多邊形進(jìn)行外接矩形計(jì)算,需要知道當(dāng)前面的最大xy 和最小xy值,即可獲得外接矩形

最小外接矩形計(jì)算

對(duì)凸多邊形的每一條邊都繪制一個(gè)外接矩形求最小面積。下圖展示了計(jì)算流程


計(jì)算流程

  • 旋轉(zhuǎn)基礎(chǔ)算法實(shí)現(xiàn)

    • 旋轉(zhuǎn)點(diǎn)基礎(chǔ)
    /*** 旋轉(zhuǎn)點(diǎn)** @param point 被旋轉(zhuǎn)的點(diǎn)* @param center 旋轉(zhuǎn)中心* @param angle 角度* @return 旋轉(zhuǎn)后坐標(biāo)*/public static Coordinate get(Coordinate point, Coordinate center, double angle) {double cos = Math.cos(angle);double sin = Math.sin(angle);double x = point.x;double y = point.y;double centerX = center.x;double centerY = center.y;return new Coordinate(centerX + cos * (x - centerX) - sin * (y - centerY),centerY + sin * (x - centerX) + cos * (y - centerY));}
  • 凸包算法實(shí)現(xiàn)

    Geometry hull = (new ConvexHull(geom)).getConvexHull();
  • 獲得結(jié)果

    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("==============旋轉(zhuǎn)基點(diǎn)==============");System.out.println(new GeometryFactory().createPoint(c));System.out.println("==============旋轉(zhuǎn)基點(diǎn)==============");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);}
  • 測(cè)試類

    @Testpublic 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());}
  • 本文代碼及可視化代碼均放在 gitee 碼云上 歡迎star & fork

    總結(jié)

    以上是生活随笔為你收集整理的最小外接矩形思路以及实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。