points/src/divide_and_conquer.c

128 lines
5.4 KiB
C
Raw Normal View History

2018-12-12 11:43:12 -03:00
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "points.h"
#include "brute_force.h"
#include "distance.h"
/**
* Encontrar los 2 puntos más cercano usando el metodo de dividir para conquistar
2018-12-12 12:26:33 -03:00
* @param point_ts Los puntos a calcular
* @param n La cantidad de puntos en el array point_ts
2018-12-12 11:43:12 -03:00
* @param minimum_dist La distancia minimo encontrado
* @return Retorna los 2 puntos mas cercanos
*/
2018-12-12 12:09:56 -03:00
int compareX(const void* a, const void* b){ //ordena el arreglo de puntos de acuerdo a X
2018-12-12 12:26:33 -03:00
point_t *p1 = (point_t *)a, *p2 = (point_t *)b;
2018-12-12 11:43:12 -03:00
return (p1->x - p2->x);
2018-12-12 12:09:56 -03:00
}
2018-12-12 11:43:12 -03:00
2018-12-12 12:09:56 -03:00
int compareY(const void* a, const void* b){ //ordena el arreglo de puntos de acuerdo a Y
2018-12-12 12:26:33 -03:00
point_t *p1 = (point_t *)a, *p2 = (point_t *)b;
2018-12-12 11:43:12 -03:00
return (p1->y - p2->y);
2018-12-12 12:09:56 -03:00
}
2018-12-12 11:43:12 -03:00
2018-12-12 12:26:33 -03:00
float min(float x, float y){ // Función para encontrar el mayor entre dos valores flotantes
2018-12-12 12:09:56 -03:00
return (x < y)? x : y;
}
2018-12-12 12:26:33 -03:00
float closestUtil(point_t P[], int n){ // Función para encontrar la distancia más corta entre puntos
int mid = n/2;
point_t midpoint_t = P[mid];
// Consider the vertical line passing through the middle point_t
// calculate the smallest distance dl on left of middle point_t and
// dr on right side
float dl = closestUtil(P, mid);
float dr = closestUtil(P + mid, n-mid);
float d = min(dl, dr); // Encontrar la distancia minima de dos puntos
/* Crea un arreglo que contiene los puntos cercanos, mas cerca que d*/
point_t strip[n];
int j = 0;
for (int i = 0; i < n; i++)
if (abs(P[i].x - midpoint_t.x) < d)
strip[j] = P[i], j++;
/*Encontrar el punto más cercano en la cinta, retornando el minimo de d y el más cercano
distance es strip[]*/
return min(d, stripClosest(strip, j, d) );
}
2018-12-12 12:09:56 -03:00
2018-12-12 12:26:33 -03:00
float stripClosest(point_t strip[], int size, float d) // Función para encontrar la distancia entre los puntos más cerca del arreglo dado
2018-12-12 12:09:56 -03:00
{
2018-12-12 11:43:12 -03:00
float min = d; // inicializa en la distancia minima d
2018-12-12 12:26:33 -03:00
qsort(strip, size, sizeof(point_t), compareY);
2018-12-12 11:43:12 -03:00
for (int i = 0; i < size; ++i)
for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j)
if (distance(strip[i],strip[j]) < min)
min = dist(strip[i], strip[j]);
return min;
2018-12-12 12:09:56 -03:00
}
point_t * divide_and_conquer(point_t *points, unsigned int n, double *minimum_dist) {
2018-12-12 12:26:33 -03:00
point_t *closest_pair = malloc(sizeof(point_t) * 2);
2018-12-12 12:09:56 -03:00
2018-12-12 12:26:33 -03:00
float closest(point_t P[], int n)
{
qsort(P, n, sizeof(point_t), compareX);
return closestUtil(P, n); /*Uso recursivo de la funcion closestUtil() para encontrar la distancia más pequeña*/
}
return closest_pair;
}
2018-12-12 11:43:12 -03:00
2018-12-12 12:26:33 -03:00
float closestUtil(point_t P[], int n) /*Función recursiva para encontrar la distancia más pequeña. El arreglo
P contiene todos los puntos ordenados respecto a la cordenada X */
2018-12-12 11:43:12 -03:00
{
int mid = n/2;
2018-12-12 12:26:33 -03:00
point_t midpoint_t = P[mid];
/*Considerando la linea vertical que pasa a travez del punto medio, calcula el distanca mas corta en dl
de la izquierda de la mitad y dr en el lado derecho*/
2018-12-12 11:43:12 -03:00
float dl = closestUtil(P, mid);
float dr = closestUtil(P + mid, n-mid);
// Find the smaller of two distances
float d = min(dl, dr);
2018-12-12 12:26:33 -03:00
// Build an array strip[] that contains point_ts close (closer than d)
// to the line passing through the middle point_t
point_t strip[n];
2018-12-12 11:43:12 -03:00
int j = 0;
for (int i = 0; i < n; i++)
2018-12-12 12:26:33 -03:00
if (abs(P[i].x - midpoint_t.x) < d)
2018-12-12 11:43:12 -03:00
strip[j] = P[i], j++;
2018-12-12 12:26:33 -03:00
// Find the closest point_ts in strip. Return the minimum of d and closest
2018-12-12 11:43:12 -03:00
// distance is strip[]
return min(d, stripClosest(strip, j, d) );
}
// The main functin that finds the smallest distance
// This method mainly uses closestUtil()
2018-12-12 12:26:33 -03:00
float closest(point_t P[], int n)
2018-12-12 11:43:12 -03:00
{
2018-12-12 12:26:33 -03:00
qsort(P, n, sizeof(point_t), compareX);
2018-12-12 11:43:12 -03:00
// Use recursive function closestUtil() to find the smallest distance
return closestUtil(P, n);
}