#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}
#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include <QPoint>
#include <QtMath>
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    void paintEvent(QPaintEvent *) override {
        QPainter painter(this);
              painter.setRenderHint(QPainter::Antialiasing, true);

              // 设置画笔的颜色和样式
              painter.setPen(Qt::black);

              // 获取Widget的尺寸
              int width = this->width();
              int height = this->height();

              // 定义坐标原点的位置
              int centerX = width / 2;
              int centerY = height / 2;

              // 绘制X轴
              painter.drawLine(centerX, 0, centerX, height);

              // 绘制Y轴
              painter.drawLine(0, centerY, width, centerY);

              // 可选地，绘制网格线
              int step = 50; // 网格线之间的间隔
              painter.setPen(Qt::lightGray);
              for (int i = step; i < width; i += step) {
                  painter.drawLine(i, 0, i, height);
                  painter.drawLine(-i + centerX * 2, 0, -i + centerX * 2, height);
              }
              for (int i = step; i < height; i += step) {
                  painter.drawLine(0, i, width, i);
                  painter.drawLine(0, -i + centerY * 2, width, -i + centerY * 2);
              }

              // 绘制坐标轴上的刻度
              painter.setPen(Qt::darkGray);
              QFontMetrics fm = fontMetrics();
              int textWidth = fm.horizontalAdvance(QString::number(step));
              int textHeight = fm.height();
              for (int i = step; i <= width / 2; i += step) {
                  painter.drawText(QPoint(centerX + i - textWidth / 2, centerY + textHeight), QString::number(i));
                  painter.drawText(QPoint(centerX - i - textWidth / 2, centerY + textHeight), QString::number(-i));
                  painter.drawText(QPoint(centerX - textWidth / 2, centerY - i), QString::number(-i));
                  painter.drawText(QPoint(centerX - textWidth / 2, centerY + i), QString::number(i));
              }
              // 绘制箭头指示坐标方向
              painter.setPen(Qt::black);
              painter.drawLine(centerX - 5, centerY, centerX + 5, centerY);
              painter.drawLine(centerX, centerY - 5, centerX, centerY + 5);

      }

  private:
      QPoint calculateCenter(QPoint p1, QPoint p2, QPoint p3) {
          // 这里需要实现计算圆心的逻辑
          return QPoint(100, 100); // 假设的圆心位置
      }

      int calculateRadius(QPoint center, QPoint point) {
          // 计算半径
          return qSqrt(qPow(point.x() - center.x(), 2) + qPow(point.y() - center.y(), 2));
      }

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
