From c14c8ef9c2efa94b2adf433f193360c118bc4253 Mon Sep 17 00:00:00 2001 From: AZEN-SGG <74971141+AZEN-SGG@users.noreply.github.com> Date: Sat, 21 Dec 2024 01:05:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=87=D0=B8=D0=BD=D0=B8=D0=BB,=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BD=D1=8F=D0=BB,=20=D1=87=D1=82=D0=BE=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=B2=D0=B5=D1=80=D0=BD=D0=BE=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputationalGeometry/6Ex/SCP.c | 2 +- ComputationalGeometry/6Ex/main.c | 20 +++--- ComputationalGeometry/6Ex/tools.c | 101 +++++++++++++++--------------- ComputationalGeometry/6Ex/tools.h | 11 ++-- 4 files changed, 64 insertions(+), 70 deletions(-) diff --git a/ComputationalGeometry/6Ex/SCP.c b/ComputationalGeometry/6Ex/SCP.c index 9b5772f..1644590 100644 --- a/ComputationalGeometry/6Ex/SCP.c +++ b/ComputationalGeometry/6Ex/SCP.c @@ -14,7 +14,7 @@ circle MEC(point * I, int ilen, point * N, int nlen) { } bool belongs(circle crcl, point p) { - if (powd(p.x - crcl.center.x) + powd(p.y - crcl.center.y) - powd(crcl.radius) <= exp) return true; + if (powd(p.x - crcl.center.x) + powd(p.y - crcl.center.y) <= exp + powd(crcl.radius)) return true; return false; } diff --git a/ComputationalGeometry/6Ex/main.c b/ComputationalGeometry/6Ex/main.c index 44bd093..b8c4fd0 100644 --- a/ComputationalGeometry/6Ex/main.c +++ b/ComputationalGeometry/6Ex/main.c @@ -3,30 +3,24 @@ #include "hope.h" int main(void) { - points * pts; + points pts; point N[3]; circle crcl; - pts = getPoints(file); - if (pts == NULL) return -1; - - /* - printf("\nPoints:\n"); - for (int i = 0; i < ps.length; ++i) printf("(%.2lf, %.2lf) ", ps.array[i].x, ps.array[i].y); - printf("\n\n"); - */ + pts = getPoints(); + if (pts.array == NULL) return -1; - printPoints(*pts); + printPoints(pts); printf("\nFast algorithm:\n"); - crcl = MEC(ps->array, ps->length, N, 0); + crcl = MEC(pts.array, pts.length, N, 0); printCircle(crcl); printf("\nReliable algorithm:\n"); - crcl = hope(ps->array, ps->length); + crcl = hope(pts.array, pts.length); printCircle(crcl); - free(ps->array); + free(pts.array); return 0; } diff --git a/ComputationalGeometry/6Ex/tools.c b/ComputationalGeometry/6Ex/tools.c index dd013f3..5a8edc3 100644 --- a/ComputationalGeometry/6Ex/tools.c +++ b/ComputationalGeometry/6Ex/tools.c @@ -1,5 +1,36 @@ #include "tools.h" +points getPoints(void) { + int fileOrRandom; + + printf("0 - if points from file\n1 - if random generation of points\nYour choice: "); + + if (scanf("%d", &fileOrRandom) < 1) { + printf("Enter error!\n"); + return (points){NULL, 0}; + } + + if (fileOrRandom == 0) { + return getPointsFromFile(); + } else if (fileOrRandom == 1) { + return getRandomPoints(); + } else { + printf("Wrong enter!\n"); + return (points){NULL, 0}; + } +} + +points getPointsFromFile(void) { + FILE * file = getFile(); + points pts = (points){NULL, 0}; + + if (file == NULL) return (points){NULL, 0}; + pts = getFilePoints(file); + + fclose(file); + return pts; +} + FILE * getFile(void) { char filename[50]; @@ -18,7 +49,7 @@ FILE * getFile(void) { } } -points * getFilePoints(FILE * file) { +points getFilePoints(FILE * file) { int i, size = 2; point * array = NULL; point p; @@ -26,7 +57,7 @@ points * getFilePoints(FILE * file) { if (fscanf(file, "%lf", ¤t) != 1) { printf("File is empty!\n"); - return NULL; + return (points){NULL, 0}; } array = (point *)malloc(size * sizeof(point)); @@ -36,7 +67,7 @@ points * getFilePoints(FILE * file) { if (++i / 2 >= size) { size *= 2; array = (point *)realloc(array, size * sizeof(point)); - if (array == NULL) return NULL; + if (array == NULL) return (points){NULL, 0}; } if (i % 2 == 1) { @@ -48,72 +79,40 @@ points * getFilePoints(FILE * file) { if (i == 0) { printf("Array is empty!\n"); free(array); - return NULL + return (points){NULL, 0}; } - return &(points){array, (i + 1) / 2}; + return (points){array, (i + 1) / 2}; } -points * GetPointsFromFile(void) { - FILE * file = getFile(); - points * pts = NULL; - - if (file == NULL) return NULL; - getFilePoints(file) - - fclose(file); - return pts; -} - -points * getRandomPoints(void) { - points * pts = &(points){.array=NULL, .length=0}; +points getRandomPoints(void) { + points pts = (points){.array=NULL, .length=0}; int length; printf("Enter the number of points: "); - if (scanf("%d", &length) < 1) return NULL; - pts->length = length; + if (scanf("%d", &length) < 1) return pts; + pts.length = length; - pts->array = (point *)malloc(length * sizeof(point)); - if (pts->array == NULL) return NULL; + pts.array = (point *)malloc(length * sizeof(point)); + if (pts.array == NULL) return pts; - generate(pts); + generatePoints(&pts); - return pts + return pts; } -points * getPoints(void) { - int fileOrRandom; +void generatePoints(points * pts) { + srand(time(NULL)); - printf("0 - if points from file\n1 - if random generation of points\nYour choice: "); - - if (scanf("%d", &fileOrRandom) < 1) { - printf("Wrong enter!\n"); - return NULL; - } - - if (fileOrRandom == 0) { - return getPointsFromFile(); - } else (fileOrRandom == 1) { - return getRandomPoints(); - } else { - printf("Wrong enter!\n"); - return NULL; + for (int i = 0; i < pts->length; i++) { + (pts->array[i]).x = (rand() % (2*MAX_RAND_COORD + 1) - MAX_RAND_COORD) * RAND_MULTIPLIER; + (pts->array[i]).y = (rand() % (2*MAX_RAND_COORD + 1) - MAX_RAND_COORD) * RAND_MULTIPLIER; } } + void printPoints(points pts) { for (int i = 0; i < ((MAX_PRINT_POINTS < pts.length) ? MAX_PRINT_POINTS : pts.length); ++i) { printf("%c = (%.4lf, %.4lf)\n", i + ASCII_FIRST_LETTER, pts.array[i].x, pts.array[i].y); } } - -void generate(points * pts) -{ - srand(time(NULL)); - - for (int i = 0; pts->length; i++) - { - (pts->array[i]).x = (rand() % (2*MAX_RAND_COORD + 1) - MAX_RAND_COORD) * RAND_MULTIPLIER; - (pts->array[i]).y = (rand() % (2*MAX_RAND_COORD + 1) - MAX_RAND_COORD) * RAND_MULTIPLIER; - } -} diff --git a/ComputationalGeometry/6Ex/tools.h b/ComputationalGeometry/6Ex/tools.h index 821f3e3..8adc7c9 100644 --- a/ComputationalGeometry/6Ex/tools.h +++ b/ComputationalGeometry/6Ex/tools.h @@ -3,6 +3,7 @@ #include #include +#include #include "types.h" #define ASCII_FIRST_LETTER 65 @@ -10,12 +11,12 @@ #define MAX_RAND_COORD 10000 #define RAND_MULTIPLIER 1.e-2 +points getPoints(void); +points getPointsFromFile(void); FILE * getFile(void); -points * getFilePoints(FILE * file) -points * GetPointsFromFile(void); -points * getRandomPoints(void); -points * getPoints(void); +points getFilePoints(FILE * file); +points getRandomPoints(void); +void generatePoints(points * pts); void printPoints(points pts); -void generate(points * pts); #endif