Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MAIN FILE
- //
- // OOP_lab_2_v2.cpp: определяет точку входа для консольного приложения.
- #include "MyCircle.h"
- #include <windows.h>
- #include <windowsx.h>
- #include <conio.h>
- #include <iostream>
- using namespace std;
- void isСontains(RECT* rt, MyCircle* figure1, MyCircle* figure2) {
- if (!figure1->isValid(rt) || !figure2->isValid(rt)) return;
- int* pointsF1 = figure1->getCenter();
- int* pointsF2 = figure2->getCenter();
- //printf("one: %d\ntwo: %d\n", figure1->getCenter(), figure2->getCenter());
- //printf("Figure1 x:%d y:%d\nFigure2 x:%d y:%d", pointsF1[0], pointsF1[1], pointsF2[0], pointsF2[1]);
- double distance = sqrt((pointsF1[0] - pointsF2[0]) * (pointsF1[0] - pointsF2[0]) +
- (pointsF1[1] - pointsF2[1]) * (pointsF1[1] - pointsF2[1]));
- if (figure1->getRadius() < (figure2->getRadius() + distance)) {
- throw 4;
- }
- }
- void main() {
- setlocale(LC_ALL, "Russian");
- // получаем идентификатор окна
- HWND hwnd = GetConsoleWindow();
- // получаем контекст отображения
- HDC hdc = GetDC(hwnd);
- RECT rt;
- // получаем размер окна
- GetClientRect(hwnd, &rt);
- try {
- MyCircle figure1;
- figure1.init(130, 200, 100, 255, 0, 255, 0, 0, 0);
- MyCircle figure2;
- figure2.init(150, 230, 25, 255, 0, 255, 0, 0, 0);
- isСontains(&rt, &figure1, &figure2);
- figure1.drowPainted(&hdc, &rt);
- figure2.drowUnpainted(&hdc, &rt);
- }
- catch (int error) {
- switch (error) {
- case 1: printf("Error coordinates\n"); break;
- case 2: printf("Error: Out of window\n"); break;
- case 3: printf("Error: This is not rectangle\n"); break;
- case 4: printf("Out of figure\n"); break;
- default: printf("Unexpected error\n"); break;
- }
- return;
- }
- _getch();
- // освобождаем контекст отображения
- ReleaseDC(hwnd, hdc);
- }
- //
- // MyCircle.h
- //
- #pragma once
- #include <windows.h>
- #include <windowsx.h>
- #include <stdio.h>
- class MyCircle {
- private:
- int unsigned centerX;
- int unsigned centerY;
- int radius;
- int r, g, b;
- // Цвет заливки
- int rFont, gFont, bFont;
- public:
- void init(int centerX, int centerY, int radius, int r, int g, int b, int rFont, int gFont, int bFont);
- void load(FILE *file);
- void save(FILE *file);
- void drowUnpainted(HDC* hdc, RECT* rt);
- void drowPainted(HDC* hdc, RECT* rt);
- int isValid(RECT* rt);
- int* getCenter();
- void setCenter(int* ponints[2]);
- int getRadius();
- };
- //
- // MyCircle.cpp
- //
- #include "MyCircle.h"
- void MyCircle::init(int centerX, int centerY, int radius, int r, int g, int b, int rFont, int gFont, int bFont) {
- this->centerX = centerX;
- this->centerY = centerY;
- this->radius = radius;
- this->r = r;
- this->g = g;
- this->b = b;
- this->rFont = rFont;
- this->gFont = gFont;
- this->bFont = bFont;
- }
- void MyCircle::load(FILE *file) {
- fscanf(file, "%d %d %d %d %d %d %d %d %d", ¢erX, ¢erY, &radius, &r, &g, &b, &rFont, &gFont, &bFont);
- }
- void MyCircle::save(FILE *file) {
- fprintf(file, "%d %d %d %d %d %d %d %d %d", ¢erX, ¢erY, &radius, r, g, b, rFont, gFont, bFont);
- }
- void MyCircle::drowUnpainted(HDC* hdc, RECT* rt) {
- if (!isValid(rt)) return;
- // Создаем перо и говорим, что будем рисовать этим пером
- HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(r, g, b)));
- HBRUSH brush = SelectBrush(*hdc, CreateSolidBrush(RGB(0, 0, 0))); // Фон
- Ellipse(*hdc, centerX - radius, centerY - radius, centerX + radius, centerY + radius);
- // удаляем красное перо
- DeletePen(pen);
- // удаляем зеленую кисть
- DeleteBrush(brush);
- }
- void MyCircle::drowPainted(HDC* hdc, RECT* rt) {
- if (!isValid(rt)) return;
- // Создаем перо и говорим, что будем рисовать этим пером
- HPEN pen = SelectPen(*hdc, CreatePen(PS_SOLID, 1, RGB(r, g, b)));
- HBRUSH brush = SelectBrush(*hdc, CreateSolidBrush(RGB(r, g, b))); // Фон
- Ellipse(*hdc, centerX - radius, centerY - radius, centerX + radius, centerY + radius);
- // удаляем красное перо
- DeletePen(pen);
- // удаляем зеленую кисть
- DeleteBrush(brush);
- }
- int MyCircle::isValid(RECT* rt) {
- if (centerX - radius < 0 || centerY - radius < 0 ||
- centerX + radius > rt->right || centerY + radius > rt->bottom) {
- printf_s("Error: Out of window.");
- return false;
- }
- else if (radius < 1) {
- printf_s("Error: Negative radius");
- return false;
- }
- return true;
- }
- int* MyCircle::getCenter() {
- int* points = new int[2];
- points[0] = centerX;
- points[1] = centerY;
- return points;
- }
- void MyCircle::setCenter(int* ponints[2]) {
- centerX = *ponints[0];
- centerY = *ponints[1];
- }
- int MyCircle::getRadius() {
- return this->radius;
- }
Advertisement