Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
1
Добавлен:
01.11.2025
Размер:
3.23 Кб
Скачать
#include <iostream>
#include <locale>
#include <cmath>
using namespace std;

class Point {
protected:
    double x, y;
public:
    Point(double x_val = 0, double y_val = 0) : x(x_val), y(y_val) {}
    virtual void draw() const {
        cout << "Точка (" << x << ", " << y << ")" << endl;
    }
    virtual void move(double dx, double dy) {
        x += dx;
        y += dy;
    }
    
    // Методы доступа к координатам
    double getX() const { return x; }
    double getY() const { return y; }
    void setX(double x_val) { x = x_val; }
    void setY(double y_val) { y = y_val; }
    
    virtual ~Point() {}
};

class Segment : public Point {
protected:
    Point endPoint;
public:
    Segment(double x1 = 0, double y1 = 0, double x2 = 1, double y2 = 1) 
        : Point(x1, y1), endPoint(x2, y2) {}
    
    void draw() const override {
        cout << "Отрезок от (" << x << ", " << y << ") до (" 
             << endPoint.getX() << ", " << endPoint.getY() << ")" << endl;
    }
    
    void move(double dx, double dy) override {
        Point::move(dx, dy);
        endPoint.move(dx, dy);
    }
    
    void rotate(double angle) {
        // Простой поворот вокруг начальной точки
        double cos_a = cos(angle);
        double sin_a = sin(angle);
        
        double dx = endPoint.getX() - x;
        double dy = endPoint.getY() - y;
        
        endPoint.setX(x + dx * cos_a - dy * sin_a);
        endPoint.setY(y + dx * sin_a + dy * cos_a);
    }
};

class Triangle : public Segment {
    Point thirdPoint;
public:
    Triangle(double x1 = 0, double y1 = 0, double x2 = 1, double y2 = 0, double x3 = 0, double y3 = 1) 
        : Segment(x1, y1, x2, y2), thirdPoint(x3, y3) {}
    
    void draw() const override {
        cout << "Треугольник с вершинами: (" << x << ", " << y << "), (" 
             << endPoint.getX() << ", " << endPoint.getY() << "), (" 
             << thirdPoint.getX() << ", " << thirdPoint.getY() << ")" << endl;
    }
    
    void move(double dx, double dy) override {
        Segment::move(dx, dy);
        thirdPoint.move(dx, dy);
    }
    
    void rotate(double angle) {
        Segment::rotate(angle);
        
        // Поворот третьей точки вокруг первой вершины
        double cos_a = cos(angle);
        double sin_a = sin(angle);
        
        double dx = thirdPoint.getX() - x;
        double dy = thirdPoint.getY() - y;
        
        thirdPoint.setX(x + dx * cos_a - dy * sin_a);
        thirdPoint.setY(y + dx * sin_a + dy * cos_a);
    }
    
    void fill() const {
        cout << "Закрашиваем треугольник" << endl;
    }
};

int main() {
    setlocale(LC_ALL, "ru_RU.UTF-8");
    
    Point point(1, 1);
    Segment segment(0, 0, 2, 2);
    Triangle triangle(0, 0, 3, 0, 0, 3);
    
    point.draw();
    segment.draw();
    triangle.draw();
    
    triangle.move(2, 1);
    cout << "После перемещения: ";
    triangle.draw();
    
    triangle.fill();
    
    return 0;
}
Соседние файлы в папке Лаба5