帮助完成算法

时间:2010-11-22 08:58:00

标签: java algorithm

我创建了blindSearch方法,以便编写convexHull算法。但它不起作用,并没有删除内部点。你能帮帮我吗?

public Point pnt[] = new Point[1000000];
public Point temp[] = new Point[1000000];
private int count = 0;
private int pointCount = 0;
Point point1;
Point point2;
Point point3;
Point temp1;
Point temp2;

...

和blindSearch方法:

     public void blindSearch()
     {
      if(pointCount<3)
        {
        if(pointCount == 1)
            System.out.print("Done!");
        else if(pointCount == 2)
        {
            point1 = pnt[0];
            point2 = pnt[1];
            draw2points(this.getGraphics());

        }
    }
    for(int i = 0; i<pointCount - 2 ; i++)
    {
        for (int j = 1; j<pointCount - 1; j++)
        {
            for (int k = 2; k<pointCount ; k++)
            {
                Point p1 = pnt[i];
                Point p2 = pnt[j];
                Point p3 = pnt[k];
                point1 = p1;
                point2 = p2;
                point3 = p3;

                for(int m = 3; m <pointCount ; m++)
                {
                    if(det(point1, point2, point3, p[m]))
                    {
                        remove( m);
                    }

                }

            }
        }
    }
    for(int i = 0; i<count - 1;i++)
    {
        for(int j = 1; j<count; j++)
        {
            temp1 = temp[i];
            temp2 = temp[j];
            finaDrawing(this.getGraphics());
        }
    }

}
private void finaDrawing(Graphics graphics) {
    graphics.drawLine(temp1.x, temp1.y, temp2.x, temp2.y);

}
public boolean det(Point pt1, Point pt2, Point pt3, Point pt4)
    {
        int det1 = pt1.x*(pt2.y-pt4.y)-pt2.x*(pt1.y-pt4.y)+pt4.x*(pt1.y-pt2.y);
        int det2 = pt2.x*(pt3.y-pt4.y)-pt3.x*(pt2.y-pt4.y)+pt4.x*(pt2.y-pt3.y);
        int det3 = pt3.x*(pt1.y-pt4.y)-pt1.x*(pt3.y-pt4.y)+pt4.x*(pt3.y-pt1.y);
        if (det1>0 && det2>0 && det3>0)
            return true;
            else
                return false;


    }
    public void remove(int n)
    {
        for(int i = 0; i<count; i++)
        {
            if(temp[i] == pnt[n])
            {
                for(int j = i+1;j<count; j++ )
                {
                    temp[j-1] = temp[j];
                }
                count--;

            }

        }

    }

1 个答案:

答案 0 :(得分:2)

您应该使用equals代替==来比较点对象。 ==检查两个值是否指向同一个对象,这可能并不总是你想要的。

if(temp[i].equals(pnt[n])){

}