This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
YASMIN/ui/map/2D/Scaler2D.h
2016-09-28 12:16:45 +02:00

107 lines
1.9 KiB
C++

#ifndef SCALER2D_H
#define SCALER2D_H
#include <QPoint>
#include <Indoor/geo/Point2.h>
class Scaler2D {
private:
Point2 screenSize = Point2(400,400);
Point2 center_m = Point2(0,0);
float rotation_rad = 0.0f;
float scaleFactor = 1;
public:
Scaler2D() {
;
}
/** set the screen's size (in pixel) */
void setScreenSize(const float w_px, const float h_px) {
this->screenSize = Point2(w_px, h_px);
}
/** change the point displayed within the center of the screen */
void setCenter(const float x_m, const float y_m) {
this->center_m = Point2(x_m, y_m);
}
/** change the point displayed within the center of the screen */
void setCenterM(const Point2 center_m) {
this->center_m = center_m;
}
/** change the point displayed within the center of the screen */
void setCenterPX(const Point2 center_px) {
this->center_m = pxToM(center_px);
}
Point2 getCenterPX() const {
return mToPX(this->center_m);
}
/** set the map's rotation in radians */
void setRotation(const float rad) {
this->rotation_rad = rad;
}
void setScale(const float scale) {
this->scaleFactor = scale;
}
public:
float mToPX(const float m) const {
return m * scaleFactor * (screenSize.x * 0.01);
}
float pxToM(const float px) const {
return px / scaleFactor / (screenSize.x * 0.01);
}
Point2 pxToM(const Point2 pt) const {
return Point2(pxToM(pt.x), pxToM(pt.y));
}
Point2 mToPX(const Point2 pt) const {
return Point2(mToPX(pt.x), mToPX(pt.y));
}
/** convert map to screen coordinates */
Point2 mapToScreen(const Point2 pt_m) const {
Point2 pt = pt_m;
// move to (0,0)
pt -= center_m;
// rotate
pt = Point2(
std::cos(rotation_rad) * pt.x - std::sin(rotation_rad) * pt.y,
std::sin(rotation_rad) * pt.x + std::cos(rotation_rad) * pt.y
);
// scale
pt.x = mToPX(pt.x);
pt.y = mToPX(pt.y);
// add screen-center
pt += screenSize/2;
// negate y
pt.y = screenSize.y - pt.y;
// done
return pt;
}
};
#endif // SCALER2D_H