【问题标题】:Add a corner to a polygone向多边形添加角
【发布时间】:2014-03-28 22:06:42
【问题描述】:

我正在创建一个工具,其中我在 JavaFX 中有一个地图。我必须在该地图上绘制一个现有的多边形,以便在其上为定位服务创建区域。然后我想点击地图上的某个地方,为这个多边形添加一个新角。现在,向多边形添加一个角并不难。当我用鼠标右键单击地图上的某个地方时,我想在那里创建一个新角。但我想在“正确”位置添加那个角,这意味着在最接近新角的现有角之前或之后,而不是在多边形的末端。此外,新的多边形不应穿过 esiting 多边形(见本文末尾的图片)。

我使用毕达哥拉斯定理找到最近的点,但我现在的问题是,我不想在这个最近的角落之前或之后添加那个角落。

Polygon poly = new Polygon(...);  //javaFX

private void insertPoint(double x, double y)
{
  int positionInPolygon = 0;
  double minDistance = Double.MAX_VALUE;

  //find that point in the existing polygon that is nearest to the new one
  for ( int i = 0; i <= poly.getPoints().size()-1; i += 2 ) 
  {
    double cornerA_x = poly.getPoints().get(i);
    double cornerA_y = poly.getPoints().get(i+1);
    double tmpDistance = distance(x, y, cornerA_x, cornerA_y);
    if(minDistance > tmpDistance)
    {
      minDistance = tmpDistance;
      positionInPolygon = i;
    }
  }
  //Now I have the nearest point in the polygon    
  //But I don't know if I have to insert that new point BEFORE or AFTER the existing one.
  ...
}


private double distance(double x1, double y1, double x2, double y2)
{
  double result = 0;
  result = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
  return result;
}

这应该是结果,老实说,我没有找到我想要的多边形是如何正确调用的。

【问题讨论】:

  • 只是一个想法......如果你有 n 个点,那么在点之间只有 n 个点可以插入新点。尝试它们中的每一个,并取结果图形中总周长最小的那个。不确定这是否总是能提供最好的结果,但这是一个开始。
  • 我很确定您正在寻找点击位置的凸壳 (en.wikipedia.org/wiki/Convex_hull)。为此有多种算法(例如“格雷厄姆扫描”)。您可能可以找到使用这些关键字的 Java 实现。
  • 您好 tokias_k,您的解决方案有效!谢谢你的提示!

标签: java polygon convex


【解决方案1】:

从答案中得到的解决方案:

代码中添加的解决方案:感谢 tokias_k(计算 周长知道在哪里插入新点)

Polygon poly = new Polygon(...);  //javaFX

private void insertPoint(double x, double y)
{
  int positionInPolygon = 0;
  double minDistance = Double.MAX_VALUE;

  //find that point in the existing polygon that is nearest to the new one
  for ( int i = 0; i <= poly.getPoints().size()-1; i += 2 ) 
  {
    double cornerA_x = poly.getPoints().get(i);
    double cornerA_y = poly.getPoints().get(i+1);
    double tmpDistance = distance(x, y, cornerA_x, cornerA_y);
    if(minDistance > tmpDistance)
    {
      minDistance = tmpDistance;
      positionInPolygon = i;
    }
  }
  //Now I have the nearest point in the polygon    

  //find out if the new point has to be inserted before or after that corner
  int[] pos = new int[2]; 
  pos[0] =positionInPolygon-2;
  pos[1] = positionInPolygon;
  //special points: first one
  if(pos[0] < 0)
    pos[0] = poly.getPoints().size()-2;

  double[] circumference = new double[2];
  for(int i = 0; i < 2; i++)
  {
    ObservableList<Double> tmp = FXCollections.observableArrayList(poly.getPoints());
    tmp.add(pos[i]+2, x);
    tmp.add(pos[i]+3, y);
    circumference[i] = getPolyconCircumference(tmp);
  }
  if(circumference[0] < circumference[1])
  {
    poly.getPoints().add(pos[0]+2, x);
    poly.getPoints().add(pos[0]+3, y);
    drawCircle(x, y, pos[0]);
  }
  else
  {
    poly.getPoints().add((pos[1]+2), x);
    poly.getPoints().add((pos[1]+3), y);
    drawCircle(x, y, pos[1]);
  }
}


private double getPolyconCircumference(List<Double> p)
{
  double result = 0;
  int size = p.size();

  //init with edge between first and last point of the polygon
  result = distance(p.get(size-2), p.get(size-1), 0, 1);

  for ( int i = 0; i <= poly.getPoints().size()-4; i += 2 ) 
  {
    result += distance(p.get(i), p.get(i+1), p.get(i+2), p.get(i+3));
  }
  return result;
}

private double distance(double x1, double y1, double x2, double y2)
{
  double result = 0;
  result = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
  return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多