欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java多边形重心计算

程序员文章站 2024-03-02 23:30:28
多边形重心计算 三角形重心 顶点为a,b,c的三角形重心为x = (xa + xb + xc) / 3,y = (ya + yb + yc) / 3 多边...

多边形重心计算

三角形重心

  • 顶点为a,b,c的三角形重心为x = (xa + xb + xc) / 3,y = (ya + yb + yc) / 3

多边形重心

  • x = (x1w1 + x2w2 + … + xnwn)/w
  • y = (y1w1 + y2w2 + … + ynwn)/w
import org.locationtech.jts.geom.coordinate;
import org.locationtech.jts.geom.polygon;
import org.locationtech.jts.io.parseexception;
import java.util.*;
import java.util.stream.collectors;
/**
 * <p>title : polygonnodetriangle </p>
 * <p>description : 多边形自身节点组成三角形</p>
 * @author huifer
 * @date 2018/10/15
 */
public class polygonnodetriangle {
  private int num = 3;
  private set result_p = new hashset();
  public static void main(string[] args) {
    //0
    double[] point1 = new double[]{0, 0};
    //1
    double[] point2 = new double[]{10, 0};
    //2
    double[] point3 = new double[]{20, 0};
    //3
    double[] point4 = new double[]{10, 10};
    list<double[]> allpoint = new arraylist();
    allpoint.add(point1);
    allpoint.add(point3);
    allpoint.add(point4);
    polygonnodetriangle polygoncenterpoint = new polygonnodetriangle();
    // 外围
    polygon waiwei = polygoncenterpoint.waiwei(point1, point3, point4);
    // 节点三角形
    list<polygon> sanjiaoxing = polygoncenterpoint.trianglemothed(allpoint);
    // 外围内所有三角形
    list<polygon> rangetriangle = polygoncenterpoint.getrangetriangle(waiwei, sanjiaoxing);
    // 重心xy
    double[] gravitycenterxy = polygoncenterpoint.getgravitycenterxy(rangetriangle);
    system.out.println(rangetriangle.size());
    system.out.println("================================================");
    double[] doubles = polygoncenterpoint. polygongravitypoint("polygon((0 0, 20 0, 10 10, 0 0))");
  }
  /***
   * polygon wkt 计算重心
   * @param wkt
   * @return
   */
  private double[] polygongravitypoint(string wkt) {

    if (!wkt.startswith("polygon")) {
      return null;
    }
    operation operation = new operation();
    // 外围数据转 list<double[]>
    polygon waiwei = null;
    try {
      waiwei = operation.createpolygonbywkt(wkt);
    } catch (parseexception e) {
      e.printstacktrace();
    }
    coordinate[] coordinates = waiwei.getcoordinates();
    list<double[]> allp = new arraylist<>();
    arrays.stream(coordinates).foreach(
        s -> {
          double nowx = s.x;
          double nowy = s.y;
          allp.add(new double[]{nowx, nowy});
        }
    );
    list<polygon> polygons = trianglemothed(allp);
    list<polygon> rangetriangle1 = getrangetriangle(waiwei, polygons);
    double area = waiwei.getarea();
    double[] gravitycenterxy1 = getgravitycenterxy(rangetriangle1);
    return gravitycenterxy1;
  }
  /***
   * 重心值
   * @param rangetriangle
   * @return [x, y]
   */
  private double[] getgravitycenterxy(list<polygon> rangetriangle) {
    double xarea = 0.0;
    double yarea = 0.0;
    double aarea = 0.0;
    for (polygon triangle : rangetriangle) {
      coordinate[] coordinates = triangle.getcoordinates();
      double area = 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);
    return new double[]{xarea / aarea, yarea / aarea};
  }
  /***
   * 范围内三角形
   * @param waiwei
   * @param sanjiaoxing
   * @return
   */
  private list<polygon> getrangetriangle(polygon waiwei, list<polygon> sanjiaoxing) {
    list<polygon> triangle = new arraylist<>();
    // 判断三角形是否在面内
    for (int i = 0; i < sanjiaoxing.size(); i++) {
      polygon polygon = sanjiaoxing.get(i);
      boolean within = polygon.within(waiwei);
      if (within) {
        triangle.add(polygon);
      }
    }
    return triangle;
  }
  /***
   * 三角形重心计算
   * @param a
   * @param b
   * @param c
   * @return
   */
  private double[] trianglecenterofgravity(coordinate a, coordinate b, coordinate c) {
    double gravityx = (a.x + b.x + c.x) / 3;
    double gravityy = (a.y + b.y + c.y) / 3;
    double[] result = new double[]{gravityx, gravityy};
    return result;
  }
  /***
   * 测试用外包图形
   * @return
   */
  private polygon waiwei(double[] point1, double[] point3, double[] point4) {
    list<double[]> ceshimian = new arraylist();
    ceshimian.add(point1);
//    ceshimian.add(point2);
//    ceshimian.add(point7);
    ceshimian.add(point4);
//    ceshimian.add(point6);
//    ceshimian.add(point5);
    ceshimian.add(point3);
    string polygonforlist = createpolygonforlist(ceshimian);
    operation op = new operation();
    polygon polygonbywkt = null;
    try {
      polygonbywkt = op.createpolygonbywkt(polygonforlist);
      return polygonbywkt;
    } catch (parseexception e) {
      e.printstacktrace();
    }
    return null;
  }
  /***
   * 生成所有三角形
   * @param allpoint
   * @return
   */
  private list<polygon> trianglemothed(list<double[]> allpoint) {
    // 索引 -> 点坐标
    map<string, double[]> indexofpoint = new hashmap();
    for (int i = 0; i < allpoint.size(); i++) {
      indexofpoint.put(string.valueof(i), allpoint.get(i));
    }
    // 排序结果
    sort((list) indexofpoint.keyset().stream().collect(collectors.tolist()), new hashset());
    // 删除元素相同后的集合
    // 所有三角形
    list<polygon> alltriangle = new arraylist();
    for (object onedataobj : result_p) {
      //这一行数据
      set onedatalist = (set) onedataobj;
      // 这一行数据的三角形数据
      list<double[]> trianglepoint = new arraylist();
      onedatalist.foreach(
          s -> trianglepoint.add(indexofpoint.get(s)
          ));
      polygon triangle = createtriangle(trianglepoint);
      if (triangle != null) {
        alltriangle.add(triangle);
      }
    }
    // 所有三角形结束
    return alltriangle;
  }
  /***
   * 从点坐标集合中创建一个面
   * @param points
   * @return
   */
  private static string createpolygonforlist(list<double[]> points) {
    string end = "))";
    string res = "polygon((";
    operation op = new operation();
    for (double[] point : points) {
      string x = double.tostring(point[0]);
      string y = 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 (parseexception e) {
      e.printstacktrace();
    }
    return res;
  }
  /***
   * 创建三角形
   * @param trianglepoint
   * @return polygon
   */
  private static polygon createtriangle(list<double[]> trianglepoint) {
    operation op = new operation();
    string trianglewkt;
    boolean istri = 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 {
        polygon polygonbywkt = op.createpolygonbywkt(trianglewkt);
        return polygonbywkt;
//        return trianglewkt;
      } catch (parseexception e) {
        e.printstacktrace();
      }
    }
    return null;
  }
  /***
   * 判断三角形
   * @param trianglepoint
   * @return
   */
  private static boolean istriangle(list<double[]> trianglepoint) {
    double[] doubles = trianglepoint.get(0);
    double[] doubles1 = trianglepoint.get(1);
    double[] doubles2 = trianglepoint.get(2);
    double len = math.sqrt(math.pow(doubles[0] - doubles1[0], 2) + math.pow(doubles[1] - doubles1[1], 2));
    double len1 = math.sqrt(math.pow(doubles[0] - doubles2[0], 2) + math.pow(doubles[1] - doubles2[1], 2));
    double len2 = 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)) {
      return true;
    }
    return false;
  }
  /***
   * 不重复排列 (元素不相同)
   * @param datas
   * @param target
   */
  private void sort(list datas, set target) {
    if (target.size() == this.num) {
      this.result_p.add(target);
      return;
    }
    for (int i = 0; i < datas.size(); i++) {
      list newdatas = new arraylist(datas);
      set newtarget = new hashset(target);
      newtarget.add(newdatas.get(i));
      newdatas.remove(i);
      sort(newdatas, newtarget);
    }
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接