Написал 10ю задачу по Геометрии

This commit is contained in:
AZEN-SGG 2024-12-15 13:42:03 +03:00
parent 19e0da8dc6
commit 241b43fa19
20 changed files with 432 additions and 0 deletions

View file

@ -0,0 +1 @@
-1 1 1 1 1 -1 -1 -1

View file

@ -0,0 +1,40 @@
#include "tools.h"
#include "polygon.h"
int main(void) {
points ps;
double shift;
polygon plgn, new_polygon;
FILE * file = getFile();
if (file == NULL) return -1;
ps = getPoints(file);
if (ps.arr == NULL) return -2;
if (ps.len < 3) {
printf("Incorrect polygon!\n");
return -4;
}
shift = getShift();
if (-eps < shift && shift < eps) return -3;
plgn.pts = ps;
repairPolygon(&plgn);
if (plgn.pts.len <= 1) {
printf("Incorrect polygon!\n");
return -4;
}
printPolygon(plgn, "\nOriginal polygon:\n");
new_polygon = getPolygon(&plgn, shift);
printPolygon(new_polygon, "\n\nNew polygon:\n");
free(ps.arr);
free(new_polygon.pts.arr);
return 0;
}

View file

@ -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 polygon.o tools.o
gcc main.o polygon.o tools.o -lssp && del *.o
a.exe
main.o: main.c
gcc $(CFLAGS) main.c
polygon.o: polygon.c
gcc $(CFLAGS) polygon.c
tools.o: tools.c
gcc $(CFLAGS) tools.c

View file

@ -0,0 +1,122 @@
#include "polygon.h"
line getLine(point pt1, point pt2) {
return (line){.start=pt1, .direction=(point){pt2.x - pt1.x, pt2.y - pt1.y}};
}
lines getLines(points pts) {
line * lns = (line *)malloc(pts.len * sizeof(line));
int len = pts.len;
for (int i = 0; i < len; ++i) {
lns[i] = getLine(pts.arr[i], pts.arr[(i + 1) % len]);
}
return (lines){lns, len};
}
points getVectors(points pts) {
point * vectors = (point *)malloc(pts.len * sizeof(point));
int len = pts.len;
point vector;
double vlen;
for (int i = 0; i < len; ++i) {
vector = normal(getLine(pts.arr[i], pts.arr[(i + 1) % len]));
vlen = vector_len(vector);
vector.x /= vlen, vector.y /= vlen;
vectors[i] = vector;
}
return (points){vectors, len};
}
point normal(line ln) {
return (point){ln.direction.y, -ln.direction.x};
}
double vector_len(point vector) {
return sqrt(qpow(vector.x) + qpow(vector.y));
}
double qpow(double number) {
return number * number;
}
void repairPolygon(polygon * plgn) {
lines lns = getLines(plgn -> pts);
int len = lns.len;
for (int i = 0; i < len; ++i) {
point fdirect = lns.arr[i].direction;
point sdirect = lns.arr[(i + 1) % len].direction;
if (isNull(fdirect)) {
polygonDelPoint(plgn, i), --len;
delLine(&lns, i--);
fdirect = sdirect, sdirect = lns.arr[(i + 1) % len].direction;
}
if (isCollinear(fdirect, sdirect)) {
polygonDelPoint(plgn, i), --len;
delLine(&lns, i--);
}
}
}
bool isNull(point vector) {
return (fabs(vector.x) < eps && fabs(vector.y) < eps);
}
void polygonDelPoint(polygon * plgn, int index) {
delVector(&(plgn -> pts), index);
}
bool isCollinear(point fvec, point svec) {
return fabs(fabs(fvec.x * svec.x + fvec.y * svec.y) - (vector_len(fvec) * vector_len(svec))) < eps;
}
DELFUNC(Vector, points)
DELFUNC(Line, lines)
//void delVector(points * vectors, int index) {
// DEL(vectors -> arr, index, vectors -> len)
// vectors -> len--;
//}
//
//void delLine(lines * lns, int index) {
// for (int i = index + 1; i < lns -> len; ++i) {
// lns -> arr[i] = lns -> arr[i - 1];
// }
//
// lns -> len--;
//}
polygon getPolygon(polygon * plgn, double shift) {
points vectors = getVectors(plgn -> pts);
int secind, len = plgn -> pts.len;
point * pts = (point *)malloc(len * sizeof(point));
memcpy(pts, plgn -> pts.arr, len * sizeof(point));
for (int i = 0; i < len; ++i) {
vectors.arr[i].x *= shift;
vectors.arr[i].y *= shift;
secind = ((i + 1) % len);
pts[i].x += vectors.arr[i].x;
pts[secind].x += vectors.arr[i].x;
pts[i].y += vectors.arr[i].y;
pts[secind].y += vectors.arr[i].y;
}
free(vectors.arr);
return (polygon){(points){pts, len}};
}

View file

@ -0,0 +1,25 @@
#ifndef POLYGON
#define POLYGON
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "types.h"
line getLine(point pt1, point pt2);
lines getLines(points pts);
points getVectors(points pts);
point normal(line ln);
double vector_len(point vector);
double qpow(double number);
void repairPolygon(polygon * plgn);
bool isNull(point vector);
bool isCollinear(point fvec, point svec);
void polygonDelPoint(polygon * plgn, int index);
void delVector(points * vectors, int index);
void delLine(lines * lns, int index);
polygon getPolygon(polygon * plgn, double shift);
#endif

View file

@ -0,0 +1,3 @@
0 0
1 0
0 1

View file

@ -0,0 +1,2 @@
0 0
0 0

View file

@ -0,0 +1,5 @@
0 0
1 1
2 2
3 3
4 4

View file

@ -0,0 +1,4 @@
0 0
2 0
2 1
0 1

View file

@ -0,0 +1,3 @@
0 0
3 0
1 2

View file

@ -0,0 +1,5 @@
0 0
2 0
2 2
0 2
0 0

View file

@ -0,0 +1,4 @@
1000000000 1000000000
2000000000 1000000000
2000000000 2000000000
1000000000 2000000000

View file

@ -0,0 +1,5 @@
0 0
1 0
2 0
3 0
4 0

View file

@ -0,0 +1,5 @@
0 0
4 0
4 4
2 2
0 4

View file

@ -0,0 +1,4 @@
0 0
4 0
0 4
4 4

View file

@ -0,0 +1,3 @@
0 0
1 1
2 0

View file

@ -0,0 +1,32 @@
t/test1.txt
1
t/test2.txt
2
t/test3.txt
0.5
t/test4.txt
1
t/test5.txt
100000
t/test6.txt
0.2
t/test7.txt
1.5
t/test8.txt
2
t/test9.txt
0.8
t/test10.txt
2
t/test11.txt
0.5

View file

@ -0,0 +1,74 @@
#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", &current) != 1) {
printf("File is empty!\n");
return (points){.arr=NULL, 0};
}
array = (point *)malloc(size * sizeof(point));
i = 0, p.x = current;
while (fscanf(file, "%lf", &current) == 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){.arr=NULL, 0};
}
return (points){array, (i + 1) / 2};
}
double getShift(void) {
double shift;
printf("Enter the Shift: ");
if (scanf("%lf", &shift) == 1) return shift;
else {
printf("\nThe Shift is zero!\n");
return 0;
}
}
void printPolygon(polygon plgn, const char * str) {
printf("%s", str);
for (int i = 0; i < plgn.pts.len; ++i) {
printf("%c = (%.2lf, %.2lf)\n", i + 65, plgn.pts.arr[i].x, plgn.pts.arr[i].y);
}
}

View file

@ -0,0 +1,13 @@
#ifndef TOOLS
#define TOOLS
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
FILE * getFile(void);
points getPoints(FILE * file);
double getShift(void);
void printPolygon(polygon plgn, const char * str);
#endif

View file

@ -0,0 +1,40 @@
#ifndef TYPES
#define TYPES
#define eps 1.e-6
#define ARR(TYPE, NAME) \
typedef struct {\
TYPE * arr;\
int len;\
} NAME
#define DELFUNC(NAME, TYPE) \
void del##NAME(TYPE * array, int index) { \
DEL(array->arr, index, array->len) \
array -> len--; \
}
#define DEL(DATA, INDEX, LEN) \
for (int i = INDEX; i < (LEN) - 1; ++i) { \
(DATA)[i] = (DATA)[i + 1]; \
}
typedef struct {
double x;
double y;
} point;
typedef struct {
point start;
point direction;
} line;
ARR(point, points);
ARR(line, lines);
typedef struct {
points pts;
} polygon;
#endif