From b36187b77d8b8a668f266953c7183caa47baab38 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Sun, 8 Dec 2024 22:18:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=206?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D0=B8=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=83=D1=8E=20=D0=B3=D0=B5=D0=BE=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D1=80=D0=B8=D1=8E,=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=D0=B8=D0=BC=D0=B5=D0=BD=D1=8C=D1=88=D0=B5?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BA=D1=80=D1=83=D0=B3=D0=B0=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC=D1=83=20=D0=92?= =?UTF-8?q?=D0=B5=D0=BB=D0=B7=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- ComputationalGeometry/6Ex/SCP.c | 81 +++++++++++++++++++++++++++++ ComputationalGeometry/6Ex/SCP.h | 21 ++++++++ ComputationalGeometry/6Ex/input.txt | 1 + ComputationalGeometry/6Ex/main.c | 23 ++++++++ ComputationalGeometry/6Ex/makefile | 42 +++++++++++++++ ComputationalGeometry/6Ex/t/e | 2 + ComputationalGeometry/6Ex/t/foi | 1 + ComputationalGeometry/6Ex/t/i | 1 + ComputationalGeometry/6Ex/t/n | 1 + ComputationalGeometry/6Ex/t/o | 1 + ComputationalGeometry/6Ex/t/r | 1 + ComputationalGeometry/6Ex/t/t | 1 + ComputationalGeometry/6Ex/t/tr | 1 + ComputationalGeometry/6Ex/t/v | 1 + ComputationalGeometry/6Ex/tools.c | 54 +++++++++++++++++++ ComputationalGeometry/6Ex/tools.h | 11 ++++ ComputationalGeometry/6Ex/types.h | 19 +++++++ 18 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 ComputationalGeometry/6Ex/SCP.c create mode 100644 ComputationalGeometry/6Ex/SCP.h create mode 100644 ComputationalGeometry/6Ex/input.txt create mode 100644 ComputationalGeometry/6Ex/main.c create mode 100644 ComputationalGeometry/6Ex/makefile create mode 100644 ComputationalGeometry/6Ex/t/e create mode 100644 ComputationalGeometry/6Ex/t/foi create mode 100644 ComputationalGeometry/6Ex/t/i create mode 100644 ComputationalGeometry/6Ex/t/n create mode 100644 ComputationalGeometry/6Ex/t/o create mode 100644 ComputationalGeometry/6Ex/t/r create mode 100644 ComputationalGeometry/6Ex/t/t create mode 100644 ComputationalGeometry/6Ex/t/tr create mode 100644 ComputationalGeometry/6Ex/t/v create mode 100644 ComputationalGeometry/6Ex/tools.c create mode 100644 ComputationalGeometry/6Ex/tools.h create mode 100644 ComputationalGeometry/6Ex/types.h diff --git a/.gitignore b/.gitignore index d116c53..12097e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.un~ *.exe *~ -*.out \ No newline at end of file +*.out +.*.sw* \ No newline at end of file diff --git a/ComputationalGeometry/6Ex/SCP.c b/ComputationalGeometry/6Ex/SCP.c new file mode 100644 index 0000000..1396c28 --- /dev/null +++ b/ComputationalGeometry/6Ex/SCP.c @@ -0,0 +1,81 @@ +#include "SCP.h" + +circle MEC(point * I, int ilen, point * N, int nlen) { + if (ilen != 0 && nlen < 3) { + point rnd = I[ilen - 1]; + circle crcl = MEC(I, ilen - 1, N, nlen); + + if (belongs(crcl, rnd)) return crcl; + else { + N[nlen++] = rnd; + return MEC(I, ilen - 1, N, nlen); + } + } else return primitive(N, 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; + return false; +} + +double powd(double number) { + return number * number; +} + +circle primitive(point * N, int nlen) { + if (nlen == 3) { + point temp; + circle crcl; + + for (int i = 0; i < nlen; ++i) { + temp = N[2]; + N[2] = N[i]; + N[i] = temp; + + crcl = centermass(N[0], N[1]); + if (belongs(crcl, N[2])) return crcl; + + N[i] = N[2]; + N[2] = temp; + } + + return byThreePoints(N); + } else if (nlen == 2) { + return centermass(N[0], N[1]); + } else if (nlen == 1) { + return (circle){N[0], 0}; + } else { + return (circle){(point){0, 0}, 0}; + } +} + +circle centermass(point p1, point p2) { + return (circle){(point){(p1.x + p2.x) / 2, (p1.y + p2.y) / 2}, distance(p1, p2) / 2}; +} + +circle byThreePoints(point * warp) { + point center; + double radius, x, y; + double ang_a = straightAngle(warp[1], warp[0]); + double ang_b = straightAngle(warp[2], warp[1]); + + x = (ang_a * ang_b * (warp[0].y - warp[2].y) + ang_b * (warp[0].x + warp[1].x) - ang_a * (warp[1].x + warp[2].x)) / (2 * (ang_b - ang_a)); + y = (-(1/ang_b) * (x - (warp[1].x + warp[2].x) / 2) + (warp[1].y + warp[2].y) / 2); + center = (point){x, y}; + + radius = distance(center, warp[0]); + return (circle){center, radius}; +} + +double straightAngle(point p1, point p2) { + return (p1.y - p2.y) / (p1.x - p2.x); +} + +double distance(point p1, point p2) { + return sqrt(powd(p1.x - p2.x) + powd(p1.y - p2.y)); +} + +void printCircle(circle crcl) { + 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/SCP.h b/ComputationalGeometry/6Ex/SCP.h new file mode 100644 index 0000000..745251c --- /dev/null +++ b/ComputationalGeometry/6Ex/SCP.h @@ -0,0 +1,21 @@ +#ifndef SCP +#define SCP + +#include +#include +#include +#include "types.h" + +#define exp 1.e-6 + +circle MEC(point * I, int ilen, point * N, int nlen); +bool belongs(circle crcl, point p); +double powd(double number); +circle primitive(point * N, int nlen); +circle centermass(point p1, point p2); +circle byThreePoints(point * warp); +double straightAngle(point p1, point p2); +double distance(point p1, point p2); +void printCircle(circle crcl); + +#endif diff --git a/ComputationalGeometry/6Ex/input.txt b/ComputationalGeometry/6Ex/input.txt new file mode 100644 index 0000000..871e879 --- /dev/null +++ b/ComputationalGeometry/6Ex/input.txt @@ -0,0 +1 @@ +1 0 -1 0 diff --git a/ComputationalGeometry/6Ex/main.c b/ComputationalGeometry/6Ex/main.c new file mode 100644 index 0000000..d22d611 --- /dev/null +++ b/ComputationalGeometry/6Ex/main.c @@ -0,0 +1,23 @@ +#include "tools.h" +#include "SCP.h" + +int main(void) { + points ps; + point N[3]; + circle crcl; + FILE * file = getFile(); + if (file == NULL) return -1; + + ps = getPoints(file); + if (ps.array == NULL) return -2; + + for (int i = 0; i < ps.length; ++i) printf("(%.2lf, %.2lf) ", ps.array[i].x, ps.array[i].y); + printf("\n"); + + crcl = MEC(ps.array, ps.length, N, 0); + printCircle(crcl); + + free(ps.array); + + return 0; +} diff --git a/ComputationalGeometry/6Ex/makefile b/ComputationalGeometry/6Ex/makefile new file mode 100644 index 0000000..49c13b0 --- /dev/null +++ b/ComputationalGeometry/6Ex/makefile @@ -0,0 +1,42 @@ +CFLAGS = -mfpmath=sse \ + -fstack-protector-all \ + -W \ + -Wall \ + -Wextra \ + -Wunused \ + -Wcast-align \ + -Werror \ + -pedantic \ + -pedantic-errors \ + -Wfloat-equal \ + -Wpointer-arith \ + -Wformat-security \ + -Wmissing-format-attribute \ + -Wformat=1 \ + -Wwrite-strings \ + -Wcast-align \ + -Wno-long-long \ + -std=gnu99 \ + -Wstrict-prototypes \ + -Wmissing-prototypes \ + -Wmissing-declarations \ + -Wold-style-definition \ + -Wdeclaration-after-statement \ + -Wbad-function-cast \ + -Wnested-externs \ + -O3 \ + -D_DEBUG -g \ + -c + +all: main.o SCP.o tools.o + gcc main.o SCP.o tools.o -lssp && del *.o + a.exe + +main.o: main.c + gcc $(CFLAGS) main.c + +SCP.o: SCP.c + gcc $(CFLAGS) SCP.c + +tools.o: tools.c + gcc $(CFLAGS) tools.c diff --git a/ComputationalGeometry/6Ex/t/e b/ComputationalGeometry/6Ex/t/e new file mode 100644 index 0000000..79a9ea1 --- /dev/null +++ b/ComputationalGeometry/6Ex/t/e @@ -0,0 +1,2 @@ + +1 1 1 1 1 1 1 1 diff --git a/ComputationalGeometry/6Ex/t/foi b/ComputationalGeometry/6Ex/t/foi new file mode 100644 index 0000000..d4436b9 --- /dev/null +++ b/ComputationalGeometry/6Ex/t/foi @@ -0,0 +1 @@ +0 0 0 2 2 0 1 1 diff --git a/ComputationalGeometry/6Ex/t/i b/ComputationalGeometry/6Ex/t/i new file mode 100644 index 0000000..871e879 --- /dev/null +++ b/ComputationalGeometry/6Ex/t/i @@ -0,0 +1 @@ +1 0 -1 0 diff --git a/ComputationalGeometry/6Ex/t/n b/ComputationalGeometry/6Ex/t/n new file mode 100644 index 0000000..573541a --- /dev/null +++ b/ComputationalGeometry/6Ex/t/n @@ -0,0 +1 @@ +0 diff --git a/ComputationalGeometry/6Ex/t/o b/ComputationalGeometry/6Ex/t/o new file mode 100644 index 0000000..b748e2d --- /dev/null +++ b/ComputationalGeometry/6Ex/t/o @@ -0,0 +1 @@ +0 0 diff --git a/ComputationalGeometry/6Ex/t/r b/ComputationalGeometry/6Ex/t/r new file mode 100644 index 0000000..64b91c4 --- /dev/null +++ b/ComputationalGeometry/6Ex/t/r @@ -0,0 +1 @@ +0 0 2 0 diff --git a/ComputationalGeometry/6Ex/t/t b/ComputationalGeometry/6Ex/t/t new file mode 100644 index 0000000..f5c875e --- /dev/null +++ b/ComputationalGeometry/6Ex/t/t @@ -0,0 +1 @@ +0 0 0 2 2 0 diff --git a/ComputationalGeometry/6Ex/t/tr b/ComputationalGeometry/6Ex/t/tr new file mode 100644 index 0000000..3333a80 --- /dev/null +++ b/ComputationalGeometry/6Ex/t/tr @@ -0,0 +1 @@ +0 0 1 0 2 0 diff --git a/ComputationalGeometry/6Ex/t/v b/ComputationalGeometry/6Ex/t/v new file mode 100644 index 0000000..0c3d931 --- /dev/null +++ b/ComputationalGeometry/6Ex/t/v @@ -0,0 +1 @@ +0 0 0 1 0 2 diff --git a/ComputationalGeometry/6Ex/tools.c b/ComputationalGeometry/6Ex/tools.c new file mode 100644 index 0000000..d0c2300 --- /dev/null +++ b/ComputationalGeometry/6Ex/tools.c @@ -0,0 +1,54 @@ +#include "tools.h" + +FILE * getFile(void) { + char filename[50]; + + printf("Enter file name: "); + if (scanf("%s", filename) == 1) { + FILE * file = fopen(filename, "r"); + if (file == NULL) { + printf("Error file!\n)"); + return NULL; + } else { + return file; + } + } else { + printf("Empty name!\n"); + return NULL; + } +} + +points getPoints(FILE * file) { + int i, size = 2; + point * array = NULL; + point p; + double current; + + if (fscanf(file, "%lf", ¤t) != 1) { + printf("File is empty!\n"); + return (points){.array=NULL, 0}; + } + + array = (point *)malloc(size * sizeof(point)); + + i = 0, p.x = current; + while (fscanf(file, "%lf", ¤t) == 1) { + if (++i / 2 >= size) { + size *= 2; + array = (point *)realloc(array, size * sizeof(point)); + } + + if (i % 2 == 1) { + p.y = current; + array[i / 2] = p; + } else p.x = current; + } + + if (i == 0) { + printf("Array is empty!\n"); + return (points){.array=NULL, 0}; + } + + return (points){array, (i + 1) / 2}; +} + diff --git a/ComputationalGeometry/6Ex/tools.h b/ComputationalGeometry/6Ex/tools.h new file mode 100644 index 0000000..a3561e1 --- /dev/null +++ b/ComputationalGeometry/6Ex/tools.h @@ -0,0 +1,11 @@ +#ifndef TOOLS +#define TOOLS + +#include +#include +#include "types.h" + +FILE * getFile(void); +points getPoints(FILE * file); + +#endif diff --git a/ComputationalGeometry/6Ex/types.h b/ComputationalGeometry/6Ex/types.h new file mode 100644 index 0000000..f42294b --- /dev/null +++ b/ComputationalGeometry/6Ex/types.h @@ -0,0 +1,19 @@ +#ifndef TYPES +#define TYPES + +typedef struct { + double x; + double y; +} point; + +typedef struct { + point * array; + int length; +} points; + +typedef struct { + point center; + double radius; +} circle; + +#endif