我正在做一个Jarvis的进行(包装)程序,在该程序中,我需要返回凸包上的一组点。我得到了正确的分数,例如[L0,L1,L2],但它必须是[“ L1”,“ L2”,“ L3”]。
将结果添加到集合中时,我使用了“ \”“ +温度点+” \“”以及'“'+温度点+'”',但是它们最终都交换了我的最终结果
public static Set<String> getBoundary (String sectorFile){
Set<String> points = new HashSet<>();
public static Set<String> getBoundary(String sectorFile){
Set<String> points=new HashSet<>();
Set<Point> vals=new HashSet<>();
Vector<Point> hull=new Vector<Point>();
try(Scanner s=new Scanner(new File(sectorFile))){
s.useDelimiter("[\\\\|\\n\\r]+");
while(s.hasNext()){
String point=s.next();
double x=s.nextDouble();
double y=s.nextDouble();
Point xy=new Point(point,x,y);
xy.setPoint(point);
xy.setX(x);
xy.setY(y);
vals.add(xy);
}
Point[]values=new Point[vals.size()];
vals.toArray(values);
int l=0;
for(int i=1;i<vals.size();i++)
if(values[i].x<values[l].x)
l=i;
// Start from leftmost point, keep moving
// counterclockwise until reach the start point
int p=l,q;
do
{
// Add current point to result
hull.add(values[p]);
q=(p+1)%vals.size();
for(int i=0;i<vals.size();i++)
{
if(orientation(values[p],values[i],values[q])==2)
q=i;
}
p=q;
}while(p!=l); // While we don't come to first
// point
for(Point temp:hull)
points.add(temp.point);
}catch(FileNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return points;
}
private static class Point
{
String point;
double x, y;
Point(String point, double x, double y){
this.point = point;
this.x=x;
this.y=y;
}
private void setPoint(String i) {
this.point = i;
}
private void setX (double x) {
this.x = x;
}
private void setY (double y) {
this.y = y;
}
}
期望
["L0", "L1", "L2"]
实际
["L2", "L1", "L0"]
答案 0 :(得分:4)
大误解:集不下达命令。您不能完全依靠HashSet为元素提供相同的“顺序” 。
如果可以的话,您可以使用LinkedHashSet(以插入的顺序接收元素)。另一个选择是TreeSet,它对所有元素进行隐式排序。如果这两个选项不能满足您的要求,那么您应该考虑使用完全不同的容器结构。
还请注意:为了合理地使用HashSet中的对象,它们的类应@Override equals()
和hashCode()
(例如,参见here)。您的点班没有,那迟早会导致非常意外的行为!
最后:当然,当您有两个集合时,您可以将它们进行比较(就像集合理论中一样:它们具有相同的内容,一个交集或根本没有交集)。并假设您的Point类具有合理的equals()
方法,那可能就是您想要的:比较两组包含完全相同的Point。