From 610ed3990ab1edfb031ece14eca170d59a220541 Mon Sep 17 00:00:00 2001 From: AZEN-SGG <74971141+AZEN-SGG@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:49:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=20=D0=BC=D0=B5=D0=B6=D0=B4=D1=83?= =?UTF-8?q?=20=D1=80=D0=B0=D0=BD=D0=B4=D0=BE=D0=BC=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B5=D0=B9=20?= =?UTF-8?q?=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=BC=20=D0=B2=206?= =?UTF-8?q?=D0=BC=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputationalGeometry/6Ex/SCP.c | 3 +- ComputationalGeometry/6Ex/main.c | 26 +++++------ ComputationalGeometry/6Ex/tools.c | 73 +++++++++++++++++++++++++++++-- ComputationalGeometry/6Ex/tools.h | 12 ++++- 4 files changed, 95 insertions(+), 19 deletions(-) diff --git a/ComputationalGeometry/6Ex/SCP.c b/ComputationalGeometry/6Ex/SCP.c index 70825a2..9b5772f 100644 --- a/ComputationalGeometry/6Ex/SCP.c +++ b/ComputationalGeometry/6Ex/SCP.c @@ -85,6 +85,7 @@ double distance(point p1, point p2) { } void printCircle(circle crcl) { - printf("Center of circle at point (%.2lf, %.2lf)\nRadius is %.2lf\n", crcl.center.x, crcl.center.y, crcl.radius); + printf("(x - %.4lf)^2 + (y - %.4lf)^2 = %.4lf^2\n", crcl.center.x, crcl.center.y, crcl.radius); + // printf("Center of circle at point (%.2lf, %.2lf)\nRadius is %.2lf\n", crcl.center.x, crcl.center.y, crcl.radius); } diff --git a/ComputationalGeometry/6Ex/main.c b/ComputationalGeometry/6Ex/main.c index 785a46d..44bd093 100644 --- a/ComputationalGeometry/6Ex/main.c +++ b/ComputationalGeometry/6Ex/main.c @@ -3,30 +3,30 @@ #include "hope.h" int main(void) { - points ps; + points * pts; point N[3]; circle crcl; - FILE * file = getFile(); - if (file == NULL) return -1; - ps = getPoints(file); - if (ps.array == NULL) return -2; + 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"); - - printf("Fast algorithm:\n"); - crcl = MEC(ps.array, ps.length, N, 0); + */ + + printPoints(*pts); + + printf("\nFast algorithm:\n"); + crcl = MEC(ps->array, ps->length, N, 0); printCircle(crcl); - printf("\n"); - - printf("Reliable algorithm:\n"); - crcl = hope(ps.array, ps.length); + printf("\nReliable algorithm:\n"); + crcl = hope(ps->array, ps->length); printCircle(crcl); - free(ps.array); + free(ps->array); return 0; } diff --git a/ComputationalGeometry/6Ex/tools.c b/ComputationalGeometry/6Ex/tools.c index d0c2300..dd013f3 100644 --- a/ComputationalGeometry/6Ex/tools.c +++ b/ComputationalGeometry/6Ex/tools.c @@ -18,7 +18,7 @@ FILE * getFile(void) { } } -points getPoints(FILE * file) { +points * getFilePoints(FILE * file) { int i, size = 2; point * array = NULL; point p; @@ -26,7 +26,7 @@ points getPoints(FILE * file) { if (fscanf(file, "%lf", ¤t) != 1) { printf("File is empty!\n"); - return (points){.array=NULL, 0}; + return NULL; } array = (point *)malloc(size * sizeof(point)); @@ -36,6 +36,7 @@ points getPoints(FILE * file) { if (++i / 2 >= size) { size *= 2; array = (point *)realloc(array, size * sizeof(point)); + if (array == NULL) return NULL; } if (i % 2 == 1) { @@ -46,9 +47,73 @@ points getPoints(FILE * file) { if (i == 0) { printf("Array is empty!\n"); - return (points){.array=NULL, 0}; + free(array); + return NULL } - 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}; + int length; + + printf("Enter the number of points: "); + if (scanf("%d", &length) < 1) return NULL; + pts->length = length; + + pts->array = (point *)malloc(length * sizeof(point)); + if (pts->array == NULL) return NULL; + + generate(pts); + + return pts +} + +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("Wrong enter!\n"); + return NULL; + } + + if (fileOrRandom == 0) { + return getPointsFromFile(); + } else (fileOrRandom == 1) { + return getRandomPoints(); + } else { + printf("Wrong enter!\n"); + return NULL; + } +} + +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 a3561e1..821f3e3 100644 --- a/ComputationalGeometry/6Ex/tools.h +++ b/ComputationalGeometry/6Ex/tools.h @@ -5,7 +5,17 @@ #include #include "types.h" +#define ASCII_FIRST_LETTER 65 +#define MAX_PRINT_POINTS 10 +#define MAX_RAND_COORD 10000 +#define RAND_MULTIPLIER 1.e-2 + FILE * getFile(void); -points getPoints(FILE * file); +points * getFilePoints(FILE * file) +points * GetPointsFromFile(void); +points * getRandomPoints(void); +points * getPoints(void); +void printPoints(points pts); +void generate(points * pts); #endif