Возможность начала координат в центре окна, исправления рассчёта

main
Artem Proskurnev 4 years ago
parent e5d6b2ef60
commit f9d93b6d08
  1. 2
      main.cpp
  2. 86
      qtsgraph.cpp
  3. 6
      qtsgraph.h

@ -12,6 +12,7 @@ int main(int argc, char *argv[])
QTSGraph w(600, 600); QTSGraph w(600, 600);
//w.SwapYAxis = true; //w.SwapYAxis = true;
//w.MoveOtoCenter = true;
w.show(); w.show();
return a.exec(); return a.exec();
} }
@ -20,6 +21,7 @@ void QTSGraph::PaintBox()
{ {
// Начало рисования // Начало рисования
//ShowAxes();
SetColor(clGreen); SetColor(clGreen);
Line(0, 0, 600, 600); Line(0, 0, 600, 600);
SetColor(0xFF0000); SetColor(0xFF0000);

@ -50,16 +50,17 @@ void QTSGraph::Ellipse(int x1, int y1, int x2, int y2)
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(Pen); painter.setPen(Pen);
painter.setBrush(Brush); painter.setBrush(Brush);
ChangeCoord(&x1, &y1);
ChangeCoord(&x2, &y2);
if(y1 > y2) std::swap(y1, y2); if(y1 > y2) std::swap(y1, y2);
if(x1 > x2) std::swap(x1, x2); if(x1 > x2) std::swap(x1, x2);
if(SwapYAxis) painter.drawEllipse(x1, Canvas.height() - y1 - abs(y2 - y1) - 1, abs(x2-x1), abs(y2-y1)); painter.drawEllipse(x1, y1, abs(x2-x1), abs(y2-y1));
else painter.drawEllipse(x1, y1, abs(x2-x1), abs(y2-y1));
update(); update();
} }
QRgb QTSGraph::GetPixel(int x, int y) QRgb QTSGraph::GetPixel(int x, int y)
{ {
if(SwapYAxis) y = Canvas.height() - y - 1; ChangeCoord(&x, &y);
return Canvas.toImage().pixelColor(x, y).rgba() % 0x1000000; return Canvas.toImage().pixelColor(x, y).rgba() % 0x1000000;
} }
@ -95,6 +96,7 @@ QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent)
StartTimer = new QTimer(); StartTimer = new QTimer();
connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer())); connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer()));
StartTimer->start(500); StartTimer->start(500);
setMouseTracking(true);
} }
QTSGraph::~QTSGraph() QTSGraph::~QTSGraph()
@ -117,8 +119,8 @@ void QTSGraph::Circle(int x, int y, int radius)
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(Pen); painter.setPen(Pen);
painter.setBrush(Brush); painter.setBrush(Brush);
if(SwapYAxis) painter.drawEllipse(x - radius, Canvas.height() - (y - radius) - (radius * 2) - 1, radius * 2, radius * 2); ChangeCoord(&x, &y);
else painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2); painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2);
update(); update();
} }
@ -136,7 +138,7 @@ void QTSGraph::OutTextXY(int x, int y, std::string caption)
painter.setPen(Pen); painter.setPen(Pen);
painter.setFont(Font); painter.setFont(Font);
double r, b, sa, ca, sb, cb, xn, yn; double r, b, sa, ca, sb, cb, xn, yn;
if(SwapYAxis) y = Canvas.height() - y - 1; ChangeCoord(&x, &y);
b = TextDirection * 3.14159 / 180; b = TextDirection * 3.14159 / 180;
r = sqrt(x * x + y * y); r = sqrt(x * x + y * y);
sa = y / r; sa = y / r;
@ -155,8 +157,8 @@ void QTSGraph::PutPixel(int x, int y, QRgb c, int PenWidth)
{ {
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(QPen(QBrush(QColor(c)), PenWidth)); painter.setPen(QPen(QBrush(QColor(c)), PenWidth));
if(SwapYAxis) painter.drawPoint(x, Canvas.height() - y - 1); ChangeCoord(&x, &y);
else painter.drawPoint(x, y); painter.drawPoint(x, y);
update(); update();
} }
@ -185,11 +187,27 @@ int QTSGraph::ReadMouseButton()
} }
TPixel QTSGraph::ReadMousePosition() TPixel QTSGraph::ReadMousePosition()
{
TPixel t;
t.x = MouseMovePosition.x();
t.y = MouseMovePosition.y();
t.color = GetPixel(t.x, t.y);
if(SwapYAxis) t.y = Canvas.height() - t.y - 1;
if(MoveOtoCenter)
{
t.x -= Canvas.width() / 2;
t.y -= Canvas.height() / 2;
}
return t;
}
TPixel QTSGraph::GetLastMouseClickPosition()
{ {
TPixel t; TPixel t;
t.x = LastMouseClickPosition.x(); t.x = LastMouseClickPosition.x();
t.y = LastMouseClickPosition.y(); t.y = LastMouseClickPosition.y();
t.color = GetPixel(t.x, t.y); t.color = GetPixel(t.x, t.y);
ChangeCoord(&t.x, &t.y);
return t; return t;
} }
@ -198,10 +216,11 @@ void QTSGraph::Rectangle(int x1, int y1, int x2, int y2)
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(Pen); painter.setPen(Pen);
painter.setBrush(Brush); painter.setBrush(Brush);
ChangeCoord(&x1, &y1);
ChangeCoord(&x2, &y2);
if(y1 > y2) std::swap(y1, y2); if(y1 > y2) std::swap(y1, y2);
if(x1 > x2) std::swap(x1, x2); if(x1 > x2) std::swap(x1, x2);
if(SwapYAxis) painter.drawRect(x1, Canvas.height() - y1 - abs(y2 - y1) - 1, x2 - x1, y2 - y1); painter.drawRect(x1, y1, x2 - x1, y2 - y1);
else painter.drawRect(x1, y1, x2 - x1, y2 - y1);
update(); update();
} }
@ -247,8 +266,9 @@ void QTSGraph::Line(int x1, int y1, int x2, int y2)
{ {
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(Pen); painter.setPen(Pen);
if(SwapYAxis) painter.drawLine(x1, Canvas.height() - y1 - 1, x2, Canvas.height() - y2 - 1); ChangeCoord(&x1, &y1);
else painter.drawLine(x1, y1, x2, y2); ChangeCoord(&x2, &y2);
painter.drawLine(x1, y1, x2, y2);
update(); update();
} }
@ -266,6 +286,16 @@ void QTSGraph::slotStartTimer()
PaintBox(); PaintBox();
} }
void QTSGraph::ChangeCoord(int *x, int *y)
{
if(MoveOtoCenter)
{
*x += Canvas.width() / 2;
*y += Canvas.height() / 2;
}
if(SwapYAxis) *y = Canvas.height() - *y - 1;
}
void QTSGraph::slotResetTimer() void QTSGraph::slotResetTimer()
{ {
ResetTimer->stop(); ResetTimer->stop();
@ -280,6 +310,31 @@ void QTSGraph::paintEvent(QPaintEvent *event)
int correctY = 0; int correctY = 0;
int mult = 1; int mult = 1;
if(AxesVisible) if(AxesVisible)
{
if(MoveOtoCenter)
{
p.setPen(QPen(QBrush(QColor(Qt::red)), 1));
if(SwapYAxis)
{
p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2, Canvas.height() - 1);
p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2 + 10, 10);
p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2 - 10, 10);
p.drawText(Canvas.width() / 2 - 20, 12, "Y");
correctY = 1;
}
else
{
p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2, Canvas.height() - 1);
p.drawLine(Canvas.width() / 2, Canvas.height() - 1, Canvas.width() / 2 + 10, Canvas.height() - 1 - 10);
p.drawLine(Canvas.width() / 2, Canvas.height() - 1, Canvas.width() / 2 - 10, Canvas.height() - 1 - 10);
p.drawText(Canvas.width() / 2 - 20, Canvas.height() - 2, "Y");
}
p.drawLine(0, Canvas.height() / 2, Canvas.width() - 1, Canvas.height() / 2);
p.drawLine(Canvas.width() - 1, Canvas.height() / 2, Canvas.width() - 1 - 10, Canvas.height() / 2 + 10);
p.drawLine(Canvas.width() - 1, Canvas.height() / 2, Canvas.width() - 1 - 10, Canvas.height() / 2 - 10);
p.drawText(Canvas.width() - 1 - 10, Canvas.height() / 2 + 20, "X");
}
else
{ {
p.setPen(QPen(QBrush(QColor(Qt::red)), 3)); p.setPen(QPen(QBrush(QColor(Qt::red)), 3));
if(SwapYAxis) if(SwapYAxis)
@ -321,6 +376,7 @@ void QTSGraph::paintEvent(QPaintEvent *event)
p.drawText(2, 3 * Canvas.height() / 4 - 2, QString::number(mult * Canvas.height() / 4)); p.drawText(2, 3 * Canvas.height() / 4 - 2, QString::number(mult * Canvas.height() / 4));
} }
} }
}
void QTSGraph::mousePressEvent(QMouseEvent *event) void QTSGraph::mousePressEvent(QMouseEvent *event)
{ {
@ -345,6 +401,12 @@ void QTSGraph::mousePressEvent(QMouseEvent *event)
ResetTimer->start(ResetInterval); ResetTimer->start(ResetInterval);
} }
void QTSGraph::mouseMoveEvent(QMouseEvent *event)
{
MouseMovePosition = event->pos();
update();
}
void QTSGraph::keyPressEvent(QKeyEvent *event) void QTSGraph::keyPressEvent(QKeyEvent *event)
{ {
ResetTimer->stop(); ResetTimer->stop();

@ -76,6 +76,7 @@ public:
~QTSGraph(); ~QTSGraph();
bool SwapYAxis = false; bool SwapYAxis = false;
bool MoveOtoCenter = false;
void ShowAxes(); void ShowAxes();
void HideAxes(); void HideAxes();
@ -92,6 +93,7 @@ public:
int ReadKey(); int ReadKey();
int ReadMouseButton(); int ReadMouseButton();
TPixel ReadMousePosition(); TPixel ReadMousePosition();
TPixel GetLastMouseClickPosition();
void Rectangle(int x1, int y1, int x2, int y2); void Rectangle(int x1, int y1, int x2, int y2);
void SetColor(const QColor &c = Qt::black); void SetColor(const QColor &c = Qt::black);
void SetColor(const QRgb c = 0x00000000); void SetColor(const QRgb c = 0x00000000);
@ -146,6 +148,7 @@ private:
int ResetInterval; int ResetInterval;
int TextDirection = 0; int TextDirection = 0;
QPoint LastMouseClickPosition; QPoint LastMouseClickPosition;
QPoint MouseMovePosition;
QBrush Brush; QBrush Brush;
QPixmap Canvas; QPixmap Canvas;
@ -154,11 +157,14 @@ private:
QTimer *ResetTimer; QTimer *ResetTimer;
QTimer *StartTimer; QTimer *StartTimer;
void ChangeCoord(int* x, int* y);
void PaintBox(); void PaintBox();
protected: protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override; void keyPressEvent(QKeyEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
}; };

Loading…
Cancel
Save