Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <climits>
- #include <cstdlib>
- #include <clocale>
- #include <string>
- #include <cstdarg>
- #include <cmath>
- using namespace std;
- inline char* format(const char* fmt, ...) {
- int size = 512;
- char * buffer = new char[size];
- va_list vl;
- va_start(vl, fmt);
- int nsize = vsnprintf(buffer, size, fmt, vl);
- if (size <= nsize) {
- delete[] buffer;
- buffer = new char[nsize + 1];
- vsnprintf(buffer, size, fmt, vl);
- }
- return buffer;
- }
- void Error(char * const text)
- {
- static FILE *file_error = nullptr;
- const char* path_error = "err.txt";
- if (file_error != nullptr || fopen_s(&file_error, path_error, "w") == 0)
- {
- printf("Ошибка. %s\n", text);
- fprintf(file_error, "Ошибка. %s\n", text);
- }
- else
- {
- printf("Ошибка. Не удалось открыть файл %s для записи.\n", path_error);
- }
- }
- void Protocol(char * const text)
- {
- static FILE *file_protocol = nullptr;
- const char* path_protocol = "protocol.txt";
- if (file_protocol != nullptr || fopen_s(&file_protocol, path_protocol, "w") == 0)
- {
- printf("%s\n", text);
- fprintf(file_protocol, "%s\n", text);
- }
- else
- {
- Error(format("Не удалось открыть файл %s для записи.", path_protocol));
- }
- }
- void ArrayPrint(int size, double ** A)
- {
- Protocol(format("Загружено точек - %d\nЗагруженные точки : ", size));
- for (int i = 0; i < size; i++)
- {
- Protocol(format("(%.1lf;%.1lf)",
- A[i][0], A[i][1]));
- }
- Protocol("");
- }
- bool ArrayRead(int &size, int maxSize, double ** &A)
- {
- const char* path_input = "in.txt";
- FILE *file_input;
- if (fopen_s(&file_input, path_input, "r") == 0)
- {
- int w = 2;
- int g;
- int index = 0;
- while ((g = fscanf_s(file_input, "%lf %lf\n", &A[size][0], &A[size][1])) != EOF)
- {
- index++;
- if (size >= maxSize - 1)
- {
- Error(format("Достигнут предел размерности массива. Загрежено %d из %d возмозжынх.", size + 1, maxSize));
- break;
- }
- switch (g)
- {
- case 2:
- size++;
- break;
- case 0:
- Error(format("Отсутствуют валидные координаты на строке %d.", index));
- fscanf_s(file_input, "%*[^\n]\n", NULL);
- continue;
- case 1:
- Error(format("Отсутствует одна из координат координаты на строке %d.", index));
- fscanf_s(file_input, "%*[^\n]\n", NULL);
- continue;
- default:;
- }
- }
- if (size <= 0)
- {
- Error(format("Не удалось корректно загрузить массив. Загруженый массив имеет размерность [%dx%d].", size, w));
- return false;
- }
- return true;
- }
- Error(format("Не удалось открыть файл %s для чтения.", path_input));
- return false;
- }
- double Hypotenuse(double a1, double a2)
- {
- return sqrtf(powf(a1, 2) + powf(a2, 2));
- }
- double Scalar(double x1, double y1, double x2, double y2)
- {
- return x1 * x2 + y1 * y2;
- }
- double Angle(double x1, double y1, double x2, double y2)
- {
- constexpr auto M_PI = 3.14159265358979323846;
- double angel = roundf((acosf(Scalar(x1, y1, x2, y2) / (Hypotenuse(x1, y1) *Hypotenuse(x2, y2)))* 180.0 / M_PI) * 100 / 100);
- return (angel == 180 ? 0 : angel);
- }
- bool CheckTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
- {
- double angles = 0;
- double _x1 = x1 - x3;
- double _y1 = y1 - y3;
- double _x2 = x2 - x3;
- double _y2 = y2 - y3;
- angles += Angle(_x1, _y1, _x2, _y2);
- _x1 = x3 - x2;
- _y1 = y3 - y2;
- _x2 = x1 - x2;
- _y2 = y1 - y2;
- angles += Angle(_x1, _y1, _x2, _y2);
- _x1 = x2 - x1;
- _y1 = y2 - y1;
- _x2 = x3 - x1;
- _y2 = y3 - y1;
- angles += Angle(_x1, _y1, _x2, _y2);
- return (angles != 0 && fmod(angles, 180) == 0);
- }
- bool Contains(bool *** array, int i1, int i2, int i3)
- {
- return
- array[i1][i2][i3] || array[i1][i3][i2] ||
- array[i2][i1][i3] || array[i2][i3][i1] ||
- array[i3][i1][i2] || array[i3][i2][i1];
- }
- char* StringTriangle(double x1, double y1, double x2, double y2, double x3, double y3, double x, double y)
- {
- return format("точка пересечения медиан - (%.1lf;%.1lf), координаты вершин - { (%.1lf;%.1lf) , (%.1lf;%.1lf) , (%.1lf;%.1lf) }\n",
- x, y,
- x1, y1,
- x2, y2,
- x3, y3);
- }
- void FindIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double &x, double &y)
- {
- x = (x1 + x2 + x3) / 3.0;
- y = (y1 + y2 + y3) / 3.0;
- }
- int FindTriangles(int size, bool *** &array, int &count, double ** A)
- {
- for (int i1 = 0; i1 < size; i1++)
- {
- for (int i2 = 0; i2 < size; i2++)
- {
- if (i1 == i2) continue;
- double x1 = A[i1][0];
- double y1 = A[i1][1];
- double x2 = A[i2][0];
- double y2 = A[i2][1];
- if (x1 == x2 && y1 == y2) continue;
- for (int i3 = 0; i3 < size; i3++)
- {
- if (i3 == i1 ||
- i3 == i2) continue;
- double x3 = A[i3][0];
- double y3 = A[i3][1];
- if (x3 == x1 && y3 == y1 ||
- x3 == x2 && y3 == y2) continue;
- if (CheckTriangle(x1, y1, x2, y2, x3, y3))
- if (!Contains(array, i1, i2, i3))
- {
- array[i1][i2][i3] = true;
- double x = 0;
- double y = 0;
- FindIntersection(x1, y1, x2, y2, x3, y3, x, y);
- Protocol(format("Найден треугольник : %s", StringTriangle(x1, y1, x2, y2, x3, y3, x, y)));
- count++;
- }
- }
- }
- }
- return 0;
- }
- void main()
- {
- setlocale(LC_CTYPE, "Russian");
- /*printf("Программа выполняет поиск точек, являющихся вершинами ромба.\n");
- printf("А так же поиск ромба с максимальным содержанием точек (включая вершины и точки на ребрах).\n");
- printf("Автор : Визгунов Андрей Дмитриевич ФКТИ 7302\n");
- printf("Версия : 7.Б.1\n\n");*/
- int size = 0;
- int const maxSize = 1000;
- double** A = new double*[maxSize];
- for (int i = 0; i < maxSize; i++)
- {
- A[i] = new double[2];
- }
- if (ArrayRead(size, maxSize, A))
- {
- bool*** triangles = new bool**[maxSize];
- for (int i = 0; i < maxSize; i++)
- {
- triangles[i] = new bool*[maxSize];
- for (int j = 0; j < maxSize; j++)
- {
- triangles[i][j] = new bool[maxSize];
- for (int k = 0; k < maxSize; k++)
- triangles[i][j][k] = false;
- }
- }
- ArrayPrint(size, A);
- int count = 0;
- FindTriangles(size, triangles, count, A);
- Protocol(format("\n===========РЕЗУЛЬТАТ===========\nВсего найдено треугольников - %d\n", count));
- }
- else
- {
- Error(format("Массив не был загружен. Завершение работы программы."));
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement