Gistrec

ООП ЛАБ1_v2 моя

Sep 22nd, 2017
263
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. // ФАЙЛ 1
  2. // 75 75 50 0 255 0
  3.  
  4. // ФАЙЛ 2
  5. // 125 125 75 0 255 0
  6.  
  7. // ФАЙЛ 3
  8. // 75 75 50
  9. // 60 60 28
  10. // 0 255 0
  11.  
  12.  
  13.  
  14. // ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
  15. //
  16.  
  17. #include "stdafx.h"
  18.  
  19.  
  20. #include <windows.h>
  21. #include <windowsx.h>
  22. #include <conio.h>
  23. #include <iostream>
  24.  
  25. struct Circle {
  26.     int centerX, centerY;
  27.     int radius;
  28. };
  29.  
  30. int isValidCircle(struct Circle* circle, RECT* rt) {
  31.     if (circle->centerX - circle->radius < 0 || circle->centerY - circle->radius < 0 ||
  32.         circle->centerX + circle->radius > rt->right || circle->centerY + circle->radius > rt->bottom) {
  33.         printf_s("Error: Out of window.");
  34.         return false;
  35.     }
  36.     return true;
  37. }
  38.  
  39. void drowUnpaintedCircle(HDC* hdc, RECT* rt, FILE* input) {
  40.     struct Circle circle;
  41.     fscanf_s(input, "%d %d %d", &circle.centerX, &circle.centerY, &circle.radius);
  42.     if (!isValidCircle(&circle, rt)) return;
  43.     // Создаем перо и говорим, что будем рисовать этим пером
  44.     int color[3];
  45.     fscanf_s(input, "%d %d %d", &color[0], &color[1], &color[2]);
  46.     HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(color[0], color[1], color[2])));
  47.     HBRUSH brush = SelectBrush(*hdc, CreateSolidBrush(RGB(0, 0, 0))); // Фон
  48.     Ellipse(*hdc, circle.centerX - circle.radius, circle.centerY - circle.radius, circle.centerX + circle.radius, circle.centerY + circle.radius);
  49.     // удаляем красное перо
  50.     DeletePen(pen);
  51.     // удаляем зеленую кисть
  52.     DeleteBrush(brush);
  53. }
  54.  
  55. void drowPaintedCircle(HDC* hdc, RECT* rt, FILE* input) {
  56.     struct Circle circle;
  57.     fscanf_s(input, "%d %d %d", &circle.centerX, &circle.centerY, &circle.radius);
  58.     if (!isValidCircle(&circle, rt)) return;
  59.     // Создаем перо и говорим, что будем рисовать этим пером
  60.     int color[3];
  61.     fscanf_s(input, "%d %d %d", &color[0], &color[1], &color[2]);
  62.     HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(color[0], color[1], color[2])));
  63.     HBRUSH brush = SelectBrush(*hdc, CreateSolidBrush(RGB(color[0], color[1], color[2]))); // Фон
  64.     Ellipse(*hdc, circle.centerX - circle.radius, circle.centerY - circle.radius, circle.centerX + circle.radius, circle.centerY + circle.radius);
  65.     // удаляем красное перо
  66.     DeletePen(pen);
  67.     // удаляем зеленую кисть
  68.     DeleteBrush(brush);
  69. }
  70.  
  71. void drowTwoCircle(HDC* hdc, RECT* rt, FILE* input) {
  72.     struct Circle circle;
  73.     fscanf_s(input, "%d %d %d", &circle.centerX, &circle.centerY, &circle.radius);
  74.     if (!isValidCircle(&circle, rt)) return;
  75.     struct Circle circle2;
  76.     fscanf_s(input, "%d %d %d", &circle2.centerX, &circle2.centerY, &circle2.radius);
  77.     if (!isValidCircle(&circle, rt)) return;
  78.  
  79.     double distance = sqrt((circle.centerX - circle2.centerX) * (circle.centerX - circle2.centerX) +
  80.                             (circle.centerY - circle2.centerY) * (circle.centerY - circle2.centerY));
  81.     if (circle.radius < (circle2.radius + distance)) {
  82.         printf("Ne vlozen!");
  83.         return;
  84.     }
  85.     HBRUSH brush;
  86.     int color[3];
  87.     fscanf_s(input, "%d %d %d", &color[0], &color[1], &color[2]);
  88.     HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(color[0], color[1], color[2])));
  89.     brush = SelectBrush(*hdc, CreateSolidBrush(RGB(color[0], color[1], color[2]))); // Фон
  90.     Ellipse(*hdc, circle.centerX - circle.radius, circle.centerY - circle.radius, circle.centerX + circle.radius, circle.centerY + circle.radius);
  91.     brush = SelectBrush(*hdc, CreateSolidBrush(RGB(0, 0, 0))); // Фон
  92.     Ellipse(*hdc, circle2.centerX - circle2.radius, circle2.centerY - circle2.radius, circle2.centerX + circle2.radius, circle2.centerY + circle2.radius);
  93. }
  94.  
  95.  
  96.  
  97. void main() {
  98.     setlocale(LC_ALL, "Russian");
  99.     // получаем идентификатор окна
  100.     HWND hwnd = GetConsoleWindow();
  101.     // получаем контекст отображения
  102.     HDC hdc = GetDC(hwnd);
  103.     RECT rt;
  104.     // получаем размер окна
  105.     GetClientRect(hwnd, &rt);
  106.  
  107.     printf("Choose programm (1, 2, 3):");
  108.     int answer = _getch();
  109.  
  110.     FILE* input;
  111.  
  112.     switch (answer) {
  113.         case '1':
  114.             system("cls");
  115.             input = fopen("1.txt", "r");
  116.             drowUnpaintedCircle(&hdc, &rt, input);
  117.             break;
  118.         case '2':
  119.             system("cls");
  120.             input = fopen("2.txt", "r");
  121.             drowPaintedCircle(&hdc, &rt, input);
  122.             break;
  123.         case '3':
  124.             system("cls");
  125.             input = fopen("3.txt", "r");
  126.             drowTwoCircle(&hdc, &rt, input);
  127.             break;
  128.         default:
  129.             printf("Not found!");
  130.             break;
  131.     }
  132.     _getch();
  133.     // освобождаем контекст отображения
  134.     ReleaseDC(hwnd, hdc);
  135.     return;
  136. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment