Calculate scale in plotting
Posted on 2001-09-17
I'm using a CStatic derived class to represent a diagram on a dialog. Because there are several curves to be displayed in one diagram simultaneously(I'm using MM_ANISOTROPIC mapping mode to fit all of them in the window), I decide not to draw a vertival axis and a set of tic marks for each of these curves, instead, I calculate the scale of each curve while plotting and then display them besides the diagram.
The scale here is defined as actual value (in vertical dir.) in a range of 1cm on the screen. I'm using the code like afterwards, I think it's right and logical, but there's remakable error on the display(around 8%). I don't know where does it come from:
void CDiagram::CalculateScale(CDC *pDC, CSize *scale)
// calculate scales in x(scale.cx) and y(scale.cy) directions
// logical values in 1cm of length on screen
CPoint OldViewportOrg = pDC->GetViewportOrg();
CSize OldViewportExt = pDC->GetViewportExt();
CPoint OldWindowOrg = pDC->GetWindowOrg();
CSize OldWindowExt = pDC->GetWindowExt();
int OldMapMode = pDC->GetMapMode();
CSize size(100, 100); // 1cm size under MM_LOMETRIC mode
pDC->LPtoDP(&size); // pixels
pDC->DPtoLP(&size); // logical units in 1cm under current mode
*scale = size;
Of course, I have already considered the potential loss of accuracy casting doulbe value to int in MoveTo/LineTo functions, and applied zoom factors to ensure the logical value be no less than, say, 1000.
By the way, in the above segment, if I omit the code saving and restore the stuff such as viewport/window org./ext.(because there's no change in between), their setting will not be kept in subsequent plotting, why?
Thanks in advance.