diff --git a/Makefile b/Makefile index 89f7f57..246921c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -Isrc/include -DDEBUG -g LDFLAGS=-lm -SRC=src/points.c src/timer.c src/random.c src/read_file.c +SRC=src/points.c src/timer.c src/read_file.c src/distance.c src/brute_force.c OBJ=$(SRC:.c=.o) all: points informe diff --git a/src/brute_force.c b/src/brute_force.c new file mode 100644 index 0000000..89863ab --- /dev/null +++ b/src/brute_force.c @@ -0,0 +1,41 @@ +/* + * 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 +#include "points.h" + +/** + * Encontrar los 2 puntos más cercano usando fuerza bruta + * @param points Los puntos a calcular + * @param n La cantidad de puntos en el array points + * @param minimum_dist La distancia mininmo encontrado + * @return Retorna los 2 puntos mas cercanos + */ +point_t * brute_force(point_t *points, unsigned int n, double *minimum_dist) { + point_t *closest_pair = malloc(sizeof(point_t) * 2); + int i; + int j; + double dist; + for (i = 0; i < n - 1; i++) { + for (j = i + 1; j < n; j++) { + if ((dist = distance(points[i], points[j])) < *minimum_dist) { + *minimum_dist = dist; + closest_pair[0] = points[i]; + closest_pair[1] = points[j]; + } + } + } + return closest_pair; +} diff --git a/src/random.c b/src/distance.c similarity index 76% rename from src/random.c rename to src/distance.c index 16f1aa3..9fb0c6c 100644 --- a/src/random.c +++ b/src/distance.c @@ -13,24 +13,15 @@ * 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 -#include +#include +#include "points.h" /** - * Flag para inicilizar srand solo una vez + * Calcular la distancia entre 2 puntos + * @param point1 El primer punto + * @param point2 El segundo punto + * @return Retorna la distancia entre los 2 puntos */ -static int sort_rand_initialized = 0; - -/** - * Generar un númer al azar entre un mínimo y máximo - * @param min El limite míninmo - * @param max El limite máximo - * @return Retorna el número generado - */ -int gen_rand(int min, int max) { - if (sort_rand_initialized == 0) { - srand((unsigned int) time(NULL)); - sort_rand_initialized = 1; - } - return rand() % (max + 1 - min) + min; +double distance(point_t point1, point_t point2) { + return sqrt(pow(point1.x - point2.x, 2) + pow(point1.y - point2.y, 2)); } diff --git a/src/include/brute_force.h b/src/include/brute_force.h new file mode 100644 index 0000000..e119917 --- /dev/null +++ b/src/include/brute_force.h @@ -0,0 +1,19 @@ +/* + * 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. + */ + +#ifndef _POINTS_BRUTE_FORCE + #define _POINTS_BRUTE_FORCE + point_t * brute_force(point_t *points, unsigned int n, double *dist); +#endif diff --git a/src/include/random.h b/src/include/distance.h similarity index 93% rename from src/include/random.h rename to src/include/distance.h index 7722fc8..51ef03f 100644 --- a/src/include/random.h +++ b/src/include/distance.h @@ -13,7 +13,7 @@ * 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. */ -#ifndef _POINTS_RANDOM - #define _POINTS_RANDOM - int gen_rand(int min, int max); +#ifndef _POINTS_DISTANCE + #define _POINTS_DISTANCE + double distance(point_t point1, point_t point2); #endif diff --git a/src/include/points.h b/src/include/points.h index 1c9b51c..77e85f7 100644 --- a/src/include/points.h +++ b/src/include/points.h @@ -19,4 +19,5 @@ float x; float y; } point_t; + double distance(point_t point1, point_t point2); #endif diff --git a/src/points.c b/src/points.c index 3117d2a..8b049a7 100644 --- a/src/points.c +++ b/src/points.c @@ -17,9 +17,12 @@ #include #include #include -#include "timer.h" +#include #include "points.h" +#include "distance.h" +#include "timer.h" #include "read_file.h" +#include "brute_force.h" #define POINTS_VERSION "1.0.0" @@ -36,6 +39,25 @@ void print_usage() { fprintf(stdout, " -v, --version mostrar la versión del programa\n"); } +/** + * Empezar los pasos antes de correr + */ +void start_algorithm(const char *message, int n) { + fprintf(stdout, "%s", message); + fflush(stdout); + start_timer(); +} + +/** + * Empezar los pasos después de correr + */ +void end_algorithm() { + stop_timer(); + fprintf(stdout, "done\n"); + print_timer(); + fprintf(stdout, "\n"); +} + /** * La entrada del programa * @param argc La cantidad de argumentos pasado al programa @@ -43,7 +65,9 @@ void print_usage() { * @return Retorna el codigo de error o 0 por exito */ int main (int argc, char **argv) { + double dist = DBL_MAX; point_t *points = NULL; + point_t *closest_points = NULL; char *filename = NULL; int brute = 0; int divide = 0; @@ -116,8 +140,7 @@ int main (int argc, char **argv) { return 2; } - points = malloc(sizeof(point_t)); - if ((opt = read_file(filename, &points, &n)) == 1) { + if ((opt = read_file(filename, &points, &n))) { if (filename != NULL) { free(filename); } @@ -127,10 +150,38 @@ int main (int argc, char **argv) { } return 3; } - if (filename != NULL) { free(filename); } + if (n < 2) { + fprintf(stderr, "Error: Se necesita un minimo de 2 puntos!\n"); + return 4; + } + + if (brute) { + start_algorithm("Brute force corriendo... ", n); + closest_points = brute_force(points, n, &dist); + end_algorithm(); + printf("point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y); + printf("point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y); + printf("distance: %lf\n\n", dist); + free(closest_points); + closest_points = NULL; + } + + if (divide) { + start_algorithm("Divide and conquer corriendo... ", n); + //closest_points = divide_and_conquer(points, n, &dist); + end_algorithm(); + /*printf("point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y); + printf("point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y); + printf("distance: %lf\n", dist); + free(closest_points); + closest_points = NULL;*/ + } + + free(points); + return 0; } diff --git a/100.pto b/test/100.pto similarity index 100% rename from 100.pto rename to test/100.pto diff --git a/test/5.pto b/test/5.pto new file mode 100644 index 0000000..7531c91 --- /dev/null +++ b/test/5.pto @@ -0,0 +1,6 @@ +5 +2 3 +7 9 +1 100 +1 1 +5 10 diff --git a/test/Makefile b/test/Makefile index c203f6d..e01eebf 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,9 +1,10 @@ CC=gcc CFLAGS=-Wall -I../src/include -DDEBUG -g +LDFLAGS=-lm SRC=test.c OBJ=$(SRC:.c=.o) -OBJ+=../src/random.o ../src/read_file.o +OBJ+=../src/read_file.o ../src/distance.o ../src/brute_force.o all: test diff --git a/test/test.c b/test/test.c index e31d493..53ea290 100644 --- a/test/test.c +++ b/test/test.c @@ -17,18 +17,75 @@ #include #include #include +#include +#include +#include "points.h" +#include "read_file.h" +#include "brute_force.h" + +/** + * Comparar 2 double para ver si son casi igual + * @param d1 El primer double + * @param d2 El segudno double + * @return Retorna 1 si son igual o 0 sino + */ +int compare_double(double d1, double d2) { + double precision = 0.000000001; + if (((d1 - precision) < d2) && ((d1 + precision) > d2)) { + return 1; + } + else { + return 0; + } +} /** * El programa de test + * @param argc La cantidad de argumentos + * @param argv Los argumentos + * @return Retorna 0 por exito o 1 si falló algun test */ int main(int argc, char **argv) { - //int pass; int passed = 0; int failed = 0; + point_t *points = NULL; + unsigned int n = 0; + double dist = DBL_MAX; + + fprintf(stdout, "Running tests:\n"); + + if (read_file("test/100.pto", &points, &n) == 0) { + fprintf(stdout, "\tread file: passed\n"); + passed++; + } + else { + fprintf(stdout, "\tread file: failed\n"); + failed++; + } + + if (failed > 0) { + fprintf(stdout, "\tbrute force: failed\n"); + failed++; + } + else { + fprintf(stdout, "\tbrute force: "); + fflush(stdout); + brute_force(points, n, &dist); + if (compare_double(dist, 0.067687840)) { + fprintf(stdout, "passed\n"); + passed++; + } + else { + fprintf(stdout, "failed\n"); + failed++; + } + } fprintf(stdout, "%d tests passed\n", passed); fprintf(stdout, "%d tests failed\n", failed); + free(points); + if (failed == 0) { return 0; }