Gistrec

ООП ЛАБ2_v2

Oct 6th, 2017
263
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. //
  2. // MAIN FILE
  3. //
  4. // OOP_lab_2_v2.cpp: определяет точку входа для консольного приложения.
  5. #include "MyCircle.h"
  6.  
  7. #include <windows.h>
  8. #include <windowsx.h>
  9. #include <conio.h>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. void isСontains(RECT* rt, MyCircle* figure1, MyCircle* figure2) {
  15.     if (!figure1->isValid(rt) || !figure2->isValid(rt)) return;
  16.     int* pointsF1 = figure1->getCenter();
  17.     int* pointsF2 = figure2->getCenter();
  18.     //printf("one: %d\ntwo: %d\n", figure1->getCenter(), figure2->getCenter());
  19.     //printf("Figure1 x:%d y:%d\nFigure2 x:%d y:%d", pointsF1[0], pointsF1[1], pointsF2[0], pointsF2[1]);
  20.     double distance = sqrt((pointsF1[0] - pointsF2[0]) * (pointsF1[0] - pointsF2[0]) +
  21.         (pointsF1[1] - pointsF2[1]) * (pointsF1[1] - pointsF2[1]));
  22.     if (figure1->getRadius() < (figure2->getRadius() + distance)) {
  23.         throw 4;
  24.     }
  25. }
  26.  
  27. void main() {
  28.     setlocale(LC_ALL, "Russian");
  29.     // получаем идентификатор окна
  30.     HWND hwnd = GetConsoleWindow();
  31.     // получаем контекст отображения
  32.     HDC hdc = GetDC(hwnd);
  33.     RECT rt;
  34.     // получаем размер окна
  35.     GetClientRect(hwnd, &rt);
  36.     try {
  37.         MyCircle figure1;
  38.         figure1.init(130, 200, 100, 255, 0, 255, 0, 0, 0);
  39.         MyCircle figure2;
  40.         figure2.init(150, 230, 25, 255, 0, 255, 0, 0, 0);
  41.         isСontains(&rt, &figure1, &figure2);
  42.         figure1.drowPainted(&hdc, &rt);
  43.         figure2.drowUnpainted(&hdc, &rt);
  44.     }
  45.     catch (int error) {
  46.         switch (error) {
  47.             case 1: printf("Error coordinates\n"); break;
  48.             case 2: printf("Error: Out of window\n"); break;
  49.             case 3: printf("Error: This is not rectangle\n"); break;
  50.             case 4: printf("Out of figure\n"); break;
  51.             default: printf("Unexpected error\n"); break;
  52.         }
  53.         return;
  54.     }
  55.     _getch();
  56.     // освобождаем контекст отображения
  57.     ReleaseDC(hwnd, hdc);
  58. }
  59.  
  60.  
  61. //
  62. // MyCircle.h
  63. //
  64. #pragma once
  65. #include <windows.h>
  66. #include <windowsx.h>
  67. #include <stdio.h>
  68.  
  69. class MyCircle {
  70.  
  71. private:
  72.     int unsigned centerX;
  73.     int unsigned centerY;
  74.  
  75.     int radius;
  76.  
  77.     int r, g, b;
  78.  
  79.     // Цвет заливки
  80.     int rFont, gFont, bFont;
  81.  
  82. public:
  83.     void init(int centerX, int centerY, int radius, int r, int g, int b, int rFont, int gFont, int bFont);
  84.  
  85.     void load(FILE *file);
  86.  
  87.     void save(FILE *file);
  88.  
  89.     void drowUnpainted(HDC* hdc, RECT* rt);
  90.  
  91.     void drowPainted(HDC* hdc, RECT* rt);
  92.  
  93.     int isValid(RECT* rt);
  94.  
  95.     int* getCenter();
  96.  
  97.     void setCenter(int* ponints[2]);
  98.  
  99.     int getRadius();
  100. };
  101.  
  102. //
  103. // MyCircle.cpp
  104. //
  105. #include "MyCircle.h"
  106.  
  107. void MyCircle::init(int centerX, int centerY, int radius, int r, int g, int b, int rFont, int gFont, int bFont) {
  108.     this->centerX = centerX;
  109.     this->centerY = centerY;
  110.     this->radius = radius;
  111.     this->r = r;
  112.     this->g = g;
  113.     this->b = b;
  114.     this->rFont = rFont;
  115.     this->gFont = gFont;
  116.     this->bFont = bFont;
  117. }
  118.  
  119. void MyCircle::load(FILE *file) {
  120.     fscanf(file, "%d %d %d %d %d %d %d %d %d", &centerX, &centerY, &radius, &r, &g, &b, &rFont, &gFont, &bFont);
  121. }
  122.  
  123. void MyCircle::save(FILE *file) {
  124.     fprintf(file, "%d %d %d %d %d %d %d %d %d", &centerX, &centerY, &radius, r, g, b, rFont, gFont, bFont);
  125. }
  126.  
  127. void MyCircle::drowUnpainted(HDC* hdc, RECT* rt) {
  128.     if (!isValid(rt)) return;
  129.     // Создаем перо и говорим, что будем рисовать этим пером
  130.     HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(r, g, b)));
  131.     HBRUSH brush = SelectBrush(*hdc, CreateSolidBrush(RGB(0, 0, 0))); // Фон
  132.     Ellipse(*hdc, centerX - radius, centerY - radius, centerX + radius, centerY + radius);
  133.     // удаляем красное перо
  134.     DeletePen(pen);
  135.     // удаляем зеленую кисть
  136.     DeleteBrush(brush);
  137. }
  138.  
  139. void MyCircle::drowPainted(HDC* hdc, RECT* rt) {
  140.     if (!isValid(rt)) return;
  141.     // Создаем перо и говорим, что будем рисовать этим пером
  142.     HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(r, g, b)));
  143.     HBRUSH brush = SelectBrush(*hdc, CreateSolidBrush(RGB(r, g, b))); // Фон
  144.     Ellipse(*hdc, centerX - radius, centerY - radius, centerX + radius, centerY + radius);
  145.     // удаляем красное перо
  146.     DeletePen(pen);
  147.     // удаляем зеленую кисть
  148.     DeleteBrush(brush);
  149. }
  150.  
  151. int MyCircle::isValid(RECT* rt) {
  152.     if (centerX - radius < 0 || centerY - radius < 0 ||
  153.         centerX + radius > rt->right || centerY + radius > rt->bottom) {
  154.         printf_s("Error: Out of window.");
  155.         return false;
  156.     }
  157.     else if (radius < 1) {
  158.         printf_s("Error: Negative radius");
  159.         return false;
  160.     }
  161.     return true;
  162. }
  163.  
  164. int* MyCircle::getCenter() {
  165.     int* points = new int[2];
  166.     points[0] = centerX;
  167.     points[1] = centerY;
  168.     return points;
  169. }
  170.  
  171. void MyCircle::setCenter(int* ponints[2]) {
  172.     centerX = *ponints[0];
  173.     centerY = *ponints[1];
  174. }
  175.  
  176. int MyCircle::getRadius() {
  177.     return this->radius;
  178. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment