public void buildPolygon(Point[] points,int mergeDistance) throws IOException{
        //can't draw anything out of a single point!
        if(points.length < 2 )
            throw new IOException();

        Polygon polygon = new Polygon();
        boolean[] pointAdded =  new boolean[points.length];
        boolean done = false;
        boolean first = true;
        boolean allowBuild = true;
        int i = 0;


        Point currentPoint = points[0];
        //add the current point to the shape
        polygon.addPoint(currentPoint.x,currentPoint.y);
        pointAdded[0] = true;
        //find the next closest point
        i = findClosestPoint(currentPoint,points,pointAdded,mergeDistance);

		//connect points until all points in the shape are connected
        while(!done){
            if(i < 0){
                allowBuild = false;
                break;
            }
            //we've connected all the points
            if(!first && currentPoint.equals(points[0]))
                done = true;

            if(done)
                break;

			//add the current point to the shape and find the next closest point
            currentPoint = points[i];
            polygon.addPoint(currentPoint.x,currentPoint.y);
            pointAdded[i] = true;
            i = findClosestPoint(currentPoint,points,pointAdded,mergeDistance);
            first = false;
        }
        //draw the polygon if there are no problems
        if(allowBuild)
            completePolygons.add(polygon);
    }