Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <fstream>
- //Обход Грэхема
- using namespace std;
- #define INFILE "input.txt"
- #define OUTFILE "outfile.txt"
- //#define - для констант
- struct Vector{
- double x;
- double y;
- };
- int vp_sgn(Vector a, Vector b);
- int angle_sgn(Vector *A, int i_a, int i_b, int i_Base);
- void bubble_sort(Vector* A, int n, int i_Base, int* arr_index);
- BOOL DrawLine(HDC hdc, int x1, int y1, int x2, int y2)
- {
- POINT pt;
- MoveToEx(hdc, x1, y1, &pt);
- return LineTo(hdc, x2, y2);
- }
- void Draw(HWND hWnd, HDC hdc){
- ifstream in(INFILE);
- ofstream out(OUTFILE);
- int n; // кол-во точек
- in >> n;
- out << " Кол-во точек = " << n << endl;
- //посчитаем габариты нашего многоугольника
- double* x = new double[n];
- double* y = new double[n];
- for (int i = 0; i < n; i++){
- in >> x[i] >> y[i];
- out << x[i]<< " " << y[i] << endl;
- }
- double xmin = x[0];
- double xmax = x[0];
- double ymin = y[0];
- double ymax = y[0];
- for (int i = 1; i < n; i++){
- if (x[i] < xmin) {
- xmin = x[i];
- }
- if (x[i] > xmax) {
- xmax = x[i];
- }
- if (y[i] < ymin) {
- ymin = y[i];
- }
- if (y[i] > ymax) {
- ymax = y[i];
- }
- }
- out << "Min: " << xmin << " " << ymin << endl;
- out << "Max: " << xmax << " " << ymax << endl;
- RECT rc;
- GetClientRect(hWnd,&rc);
- int Width = rc.right - rc.left + 1;
- int Height = rc.bottom - rc.top + 1;
- out << "rc.right" << rc.right << endl;
- out << "rc.left" << rc.left << endl;
- out << "rc.bottom" << rc.bottom << endl;
- out << "rc.top" << rc.top << endl;
- out << "Width = " << Width << " Height = " << Height << endl;
- /*
- Rectangle(hdc, xmin, ymax, xmax, ymin);
- for (int i = 0; i < n;i++)
- Ellipse(hdc, x[i] - 5, y[i]+5, x[i]+5,y[i]-5);
- */
- double W, H,koef;
- W = xmax - xmin;
- H = ymax - ymin;
- if ((W*Height) <= (H*Width)) koef = Height / (W*(1 + 0.1));
- else koef = Width / (W*(1 + 0.1));
- Vector X0,x0;//X0 - середина в метрах нашего габарита, x0 - середина в пикселях - центр экрана
- X0.x = 0.5*(xmax + xmin);
- X0.y = 0.5*(ymax + ymin);
- x0.x = 0.5*(Width);
- x0.y = 0.5*(Height);
- Vector *A = new Vector[n]; // Массив точек
- for (int i = 0; i < n; i++){
- A[i].x = koef*(x[i] - X0.x) + x0.x;
- A[i].y = koef*(y[i] - X0.y) + x0.y;
- }
- double a1 = koef*(xmax - X0.x) + x0.x;
- double a2 = koef*(ymax - X0.y) + x0.y;
- double a3 = koef*(xmin - X0.x) + x0.x;
- double a4 = koef*(ymin - X0.y) + x0.y;
- Rectangle(hdc, a3, a2, a1, a4);
- for (int i = 0; i < n; i++)
- Ellipse(hdc, (int)A[i].x - 5, (int)A[i].y + 5, (int)A[i].x + 5, (int)A[i].y - 5);
- DrawLine(hdc, x0.x, x0.y * 2, x0.x, 0);
- DrawLine(hdc, 0, x0.y, x0.x*2, x0.y);
- Vector *Point = new Vector[n];
- Point[0].x = A[0].x;
- Point[0].y = A[0].y;
- /* Vector *Point2 = new Vector[n];
- Point[0].x = A[0].x;
- Point[0].y = A[0].y;
- */
- int i_Base = 0;
- double left_x = A[0].x;
- for (int i = 1; i < n; i++){
- if (A[i].x < left_x){
- left_x =A[i].x;
- i_Base = i;
- }
- }
- /*
- Point[1].x = A[0].x;
- Point[1].y = A[0].y;
- for (int i = 1; i < n; i++){
- if (Point[1].x <= A[i].x){
- Point[1].x = A[i].x;
- Point[1].y = A[i].y;
- }
- } out << "array of points" << endl;
- for (int i = 1; i < n; i++){
- out << A[i].x << " ";
- out<<A[i].y<<endl;
- }*/
- //out << "Точка-min x , y" << Point[0].x << " " << Point[0].y << endl;
- // out << "Точка-max x , y" << Point[1].x << " " << Point[1].y << endl;
- /*for (int i = 0; i < n - 1; i++){
- Point2[i].x = A[i].x;
- Point2[i].y = A[i].y;
- }
- Point2[n].x = Point[1].x;
- Point2[n].y = Point[1].y;
- Point2[0].x = Point[0].x;
- Point2[0].y = Point[0].y;
- */
- out << "array of points" << endl;
- for (int i = 0; i < n; i++){
- out << A[i].x << " ";
- out << A[i].y << endl;
- }
- out << "крайняя левая" << endl;
- out << A[i_Base].x << " " << A[i_Base].y << " " << i_Base<<endl;
- // int signum = vp_sgn(A[1], A[2]);
- // out<< signum<<endl;
- int* arr_index = new int[n];
- for (int i = 0; i < n; i++){
- arr_index[i] = i;
- }
- //поставим i_base на первое место
- arr_index[0] = i_Base;
- arr_index[i_Base] = 0;
- //пытаемся отсортировать по возрпастанию сверху вниз
- bubble_sort(A, n, i_Base, arr_index);
- out << "array of index" << endl;
- for (int i = 0; i < n; i++){
- out << arr_index[i] << " ";
- }
- for (int i = 0; i < n; i++){
- DrawLine(hdc, left_x, A[i_Base].y, A[i].x, A[i].y);//чертим
- }
- for (int i = 0; i < n; i++){
- DrawLine(hdc, A[arr_index[i]].x, A[arr_index[i]].y, A[arr_index[i + 1]].x, A[arr_index[i + 1]].y);//чертим
- }
- }
- //Знак векторного произведения
- int vp_sgn(Vector *a, Vector *b){
- double r = a->x*b->y - a->y*b->x;
- if (r > 0.) return 1;
- return -1;
- }
- int vp_sgn(Vector a, Vector b){
- double r = a.x*b.y - a.y*b.x;
- if (r > 0.) return 1;
- return -1;
- }
- //
- int angle_sgn(Vector *A, int i_a, int i_b,int i_Base){
- Vector *a = new Vector;
- Vector *b = new Vector;
- a->y = A[i_a].y - A[i_Base].y;
- b->y = A[i_b].y - A[i_Base].y;
- if (a->y < 0.){
- if (b->y >= 0.)
- return 1;
- }
- else{
- if (b->y < 0.)
- return -1;
- }
- a->x = A[i_a].x - A[i_Base].x;
- b->x = A[i_b].x - A[i_Base].x;
- return vp_sgn(a,b);
- }
- void bubble_sort(Vector* A,int n,int i_Base,int* arr_index){
- for (int j = 0; j < n - 1; j++){
- for (int i = 1; i < n - j - 1; i++){
- int b = arr_index[i];
- int c = arr_index[i + 1];
- if (angle_sgn(A, b, c, i_Base) == -1){
- arr_index[i] = c;
- arr_index[i + 1] = b;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement