-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridscale.cpp
90 lines (78 loc) · 1.61 KB
/
gridscale.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "gridscale.h"
#include <float.h>
#include <math.h>
#include "floatroutine.h"
#include <QDebug>
//******************************************************************************************************
/*!
*\class GridScale
*\brief Шкала ячеек Document.
*/
//******************************************************************************************************
GridScale::GridScale()
:m_gridSize(32)
,m_screenSize(128)
,m_gridCount(4)
,m_redemption(0)
{
}
void GridScale::setGridSize(int value)
{
int v = (value > 0)? value : 1;
if (m_gridSize != v)
{
m_gridSize = v;
compute();
}
}
int GridScale::gridSize() const
{
return m_gridSize;
}
void GridScale::setScreenSize(double value)
{
double v = (value > 0)? value : 1;
if (m_screenSize != v)
{
m_screenSize = v;
compute();
}
}
double GridScale::screenSize() const
{
return m_screenSize;
}
int GridScale::gridCount() const
{
return m_gridCount;
}
double GridScale::gridToScreen(int grid) const
{
if (grid == 0)
{
return 0;
}
else if (grid == m_gridCount)
{
return m_screenSize;
}
else
{
return m_redemption + grid*m_gridSize;
}
}
int GridScale::screenToGrid(double screen) const
{
return iround( (screen - m_redemption)/double(m_gridSize) );
}
double GridScale::roundScreen(double screen) const
{
int grid = screenToGrid(screen);
return gridToScreen(grid);
}
void GridScale::compute()
{
int isize = int(m_screenSize);
m_gridCount = isize / m_gridSize;
m_redemption = isize % m_gridSize / 2;
}