public static double distToRect(Rectangle rect, Point2D p2){
		int side = rect.outcode(p2);
		//Sides
		if (side == Rectangle.OUT_BOTTOM || side == Rectangle.OUT_TOP) {
			return Math.min(Math.abs(rect.getMinY() - p2.getY()),
					Math.abs(rect.getMaxY() - p2.getY()));
		}
		if (side == Rectangle.OUT_LEFT || side == Rectangle.OUT_RIGHT) {
			return Math.min(Math.abs(rect.getMinX() - p2.getX()),
					Math.abs(rect.getMaxX() - p2.getX()));
		}
		//Corners
		if (side == (Rectangle.OUT_TOP | Rectangle.OUT_LEFT)){
			Point2D p = new Point2D.Double(rect.getMinX(), rect.getMinY());
			return p.distance(p2);
		}
		if (side == (Rectangle.OUT_TOP| Rectangle.OUT_RIGHT)){
			Point2D p = new Point2D.Double(rect.getMaxX(), rect.getMinY());
			return p.distance(p2);
		}
		if (side == (Rectangle.OUT_BOTTOM | Rectangle.OUT_RIGHT)){
			Point2D p = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
			return p.distance(p2);
		}
		if (side == (Rectangle.OUT_BOTTOM | Rectangle.OUT_LEFT)){
			Point2D p = new Point2D.Double(rect.getMinX(), rect.getMaxY());
			return p.distance(p2);
		}
		return 0;
	}