File indexing completed on 2025-02-16 05:15:50
0001 0002 #ifndef UTILS_H 0003 #define UTILS_H 0004 0005 #include <QPoint> 0006 #include <cmath> 0007 0008 inline int roundDown(int x, int roundness) 0009 { 0010 if (x < 0) 0011 return (x - roundness + 1) / roundness; 0012 else 0013 return (x / roundness); 0014 } 0015 inline int roundDown(double x, int roundness) 0016 { 0017 return roundDown(int(x), roundness); 0018 } 0019 0020 inline QPoint roundDown(const QPoint &p, int roundness) 0021 { 0022 return QPoint(roundDown(p.x(), roundness), roundDown(p.y(), roundness)); 0023 } 0024 0025 inline int toCanvas(int pos) 0026 { 0027 return pos * 8 + 4; 0028 } 0029 inline int fromCanvas(int pos) 0030 { 0031 return roundDown(pos - 4, 8); 0032 } 0033 0034 inline QPoint toCanvas(const QPoint *pos) 0035 { 0036 return QPoint(toCanvas(pos->x()), toCanvas(pos->y())); 0037 } 0038 inline QPoint fromCanvas(const QPoint *pos) 0039 { 0040 return QPoint(fromCanvas(pos->x()), fromCanvas(pos->y())); 0041 } 0042 0043 inline QPoint toCanvas(const QPoint &pos) 0044 { 0045 return QPoint(toCanvas(pos.x()), toCanvas(pos.y())); 0046 } 0047 inline QPoint fromCanvas(const QPoint &pos) 0048 { 0049 return QPoint(fromCanvas(pos.x()), fromCanvas(pos.y())); 0050 } 0051 0052 inline int roundDouble(double x) 0053 { 0054 return int(std::floor(x + 0.5)); 0055 } 0056 0057 inline double qpoint_distance(const QPoint &p1, const QPoint &p2) 0058 { 0059 double dx = p1.x() - p2.x(); 0060 double dy = p1.y() - p2.y(); 0061 0062 return std::sqrt(dx * dx + dy * dy); 0063 } 0064 0065 inline int snapToCanvas(int x) 0066 { 0067 return roundDown(x, 8) * 8 + 4; 0068 } 0069 inline int snapToCanvas(double x) 0070 { 0071 return snapToCanvas(int(x)); 0072 } 0073 0074 #endif