help for compeleting an algorithm
I have creating the b开发者_高级运维lindSearch method so as to write the convexHull Algorithm. but it doesnt work right, and doesnt remove the internal points. would you please help me?
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;
....
and the blindSearch method:
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--;
}
}
}
You should use equals
instead of ==
to compare the point objects. ==
checks to see the the two values refer to the same object, which may not always be what you want.
if(temp[i].equals(pnt[n])){
}
精彩评论