-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance_point.h
53 lines (44 loc) · 1.54 KB
/
distance_point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef DISTANCE_POINT_H
#define DISTANCE_POINT_H
#include <QPainter>
#include <QString>
#include <QDebug>
struct DistancePoint{
static const int rectSize = 10;
static const int fontSize = 10;
int x;
int y;
QPoint widgetPosition;
QSize widgetSize;
QRect rectangle;
float distance;
DistancePoint(QPoint _widgetPosition, QSize _widgetSize, int x, int y): distance(0){
this->x = x;
this->y = y;
this->widgetSize = _widgetSize;
this->widgetPosition = _widgetPosition;
rectangle = QRect();
updateRectPosition();
}
void updateRectPosition(){
rectangle.setLeft(widgetPosition.x() - rectSize / 2);
rectangle.setTop(widgetPosition.y() - rectSize / 2);
rectangle.setWidth(rectSize);
rectangle.setHeight(rectSize);
}
void updateWidgetPos(int canvasWidth, int canvasHeight){
int newX = canvasWidth * widgetPosition.x() / widgetSize.width();
int newY = canvasHeight * widgetPosition.y() / widgetSize.height();
this->widgetPosition = QPoint(newX, newY);
this->widgetSize = QSize(canvasWidth, canvasHeight);
updateRectPosition();
}
void render(QPainter *painter){
QPen pen = painter->pen();
painter->setPen(Qt::red);
painter->drawRect(rectangle);
painter->drawText(widgetPosition.x() - rectangle.width() - fontSize, widgetPosition.y() + rectangle.height() + fontSize, QString::number(distance, 'f', 2));
painter->setPen(pen);
}
};
#endif // DISTANCE_POINT_H