Exemplo n.º 1
0
float ClosestPair(Point* pX, int n){
	if(n <= 3) return bruteForce(pX, n);
	
	//Chia mang thanh hai phan bang nhau
	int mid = n/2;
	Point midPoint = pX[mid];

	float dL = ClosestPair(pX, mid);
	printf("ClosestPairL %f\n", dL);
	float dR = ClosestPair(pX + mid,n-mid);
	printf("ClosestPairR %f\n", dR);
	
	float d = min(dL, dR);
	printf("ClosestPair %f\n", d);

	Point* pLR = (Point*) malloc (n * sizeof(Point));
	int k = 0;
	for (int i = 0; i < n; ++i){
		if (abs(pX[i].x - midPoint.x) <= d){
			pLR[k] = pX[i];
			k++;
		}
	}
	if (dmin > min(stripClosest(pLR, k, d), d)){
		dmin = min(stripClosest(pLR, k, d), d);
	}
	return min(stripClosest(pLR, k, d), d);;
}
Exemplo n.º 2
0
// A recursive function to find the smallest distance. The array P contains
// all points sorted according to x coordinate
float closestUtil(Point P[], int n)
{
    // If there are 2 or 3 points, then use brute force
    if (n <= 3)
        return bruteForce(P, n);
 
    // Find the middle point
    int mid = n/2;
    Point midPoint = P[mid];
 
    // Consider the vertical line passing through the middle point
    // calculate the smallest distance dl on left of middle point and
    // dr on right side
    float dl = closestUtil(P, mid);
    float dr = closestUtil(P + mid, n-mid);
 
    // Find the smaller of two distances
    float d = min(dl, dr);
 
    // Build an array strip[] that contains points close (closer than d)
    // to the line passing through the middle point
    Point strip[n];
    int j = 0;
    for (int i = 0; i < n; i++)
        if (abs(P[i].x - midPoint.x) < d)
            strip[j] = P[i], j++;
 
    // Find the closest points in strip.  Return the minimum of d and closest
    // distance is strip[]
    return min(d, stripClosest(strip, j, d) );
}
Exemplo n.º 3
0
float closest(point *A,long long N,point *u,point *v)
{
	if(N<3)
	{
		return bruteForce(A,N,u,v);
	}

	long long mid=N/2;
	point midPoint = A[mid];
	point a,b,r,s;
	long long i,j;


	double dl = closest(A, mid,&a,&b);
    double dr = closest(A + mid, N-mid,&r,&s);
    double d = min(dl, dr);
    if(dl<dr)
    {
    	u[0].x=a.x;u[0].y=a.y;
    	v[0].x=b.x;v[0].y=b.y;
    }
    else
    {
    	u[0].x=r.x;u[0].y=r.y;
    	v[0].x=s.x;v[0].y=s.y;
    }

    point strip[N];
    j=0;
    for (i = 0; i < N; i++)
        if (abs(A[i].x - midPoint.x) < d)
        {
            strip[j] = A[i];
            j++;
        }

    // return min(d, stripClosest(strip, j, d) );
    double minStrip = stripClosest(strip,j,d,&a,&b);
    if(d<minStrip)
    {
    	return d;
    }
    else
    {
    	u[0].x=a.x;u[0].y=a.y;
    	v[0].x=b.x;v[0].y=b.y;
    	return minStrip;
    }
}
Exemplo n.º 4
0
struct PointPair closestpair(struct Point p[],int n)
{
	if(n<=3)
		return brute(p,n);
	int mid=n/2;
	struct Point midp=p[mid];
	struct PointPair dl = closestpair(p,mid);
	struct PointPair dr = closestpair(p+mid,n-mid);
	struct PointPair d = minpp(dl,dr);
	struct Point strip[n];
    int i=0,j = 0;
    for (i = 0; i < n; i++)
        if (abs(p[i].x - midp.x) < d.dist)
            strip[j] = p[i], j++;
	return minpp(d,stripClosest(strip,j,d));
}
float closestUtil(Point P[], int n)
{
    if (n <= 3)
        return bruteForce(P, n);
 
    int mid = n/2;
    Point midPoint = P[mid];
 
    float dl = closestUtil(P, mid);
    float dr = closestUtil(P + mid, n-mid);
 
    float d = min(dl, dr);
 
    Point strip[n];
    int j = 0;
    for (int i = 0; i < n; i++)
        if (abs(P[i].x - midPoint.x) < d)
            strip[j] = P[i], j++;
 
    return min(d, stripClosest(strip, j, d) );
}