给一个多边形添加一个角落

3
我正在创建一个工具,其中包含JavaFX中的地图。我需要在该地图上绘制现有多边形,以便为位置服务创建区域。然后,我想点击地图上的某个位置,以向该多边形添加新的角落。现在,向多边形添加一个角落并不难。当我用右键单击地图上某个位置时,我想在那里创建一个新的角落。但我希望将该角落添加到“正确”的位置,即最靠近新角落的现有角落之前或之后,而不是在多边形的末端。此外,新多边形不应穿过现有多边形(请参见本帖子末尾的图片)。 我使用勾股定理找到了最近的点,但我的问题是,我不知道是要在最近的角落之前还是之后添加该角落。
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;
}

这应该是结果,老实说我没找到想要的多边形的正确名称。

1


只是一个想法...如果你有n个点,那么在这些点之间只有n个位置可以插入新的点。尝试每一个位置,并选择周长最小的结果图形。不确定这是否总是能够得到最佳结果,但这是一个开始。 - tobias_k
我非常确定你正在寻找点击位置的凸包(http://en.wikipedia.org/wiki/Convex_hull)。有各种算法可以实现这一点(例如“Graham扫描”)。你可以使用这些关键字找到Java实现。 - Marco13
你好tokias_k,你的解决方案可行!谢谢你的提示! - user3354754
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
0

Solution taken from the answer:

SOLUTION ADDED IN THE CODE: thanks to tokias_k (calculate circumference to know where to insert the new point)

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,