Delphi - mapping world and screen

the source code of this page may not appear correctly in certain browsers
due to special characters. Have a look at the source of this HTML page
with notepad instead

Mapping world and screen coordinate systems to each other is
done by scaling with respect to an offset.
be the world window be defined as:
[xLow..xHigh,yLow..yHigh], the coordinates are of type float
be the screen window be defined as:
[tlx..brx,tly..bry], the coordinates are of type integer
the following function do the conversion :
function mapW2SxLin(xf:float):integer;
begin
result:=round(tlx+(xf-xlow)*(brx-tlx)/(xhigh-xlow));
end;
function mapW2SyLin(yf:float):integer;
begin
result:=round(bry-(yf-ylow)*(bry-tly)/(yhigh-ylow));
end;
function mapS2WxLin(xs:integer):float;
begin
result:=xlow+(xs-tlx)*(xhigh-xlow)/(brx-tlx);
end;
function mapS2WyLin(ys:integer):float;
begin
result:=yhigh-(ys-tly)*(yhigh-ylow)/(bry-tly);
end;
Logarithmic axis may used to display signal amplitudes amongst
other things. For a logarithmic scale use :
function mapW2SxLog(xf:float):integer;
begin
result:=round(tlx+(ln(xf/xlow)/ln(xhigh/xlow))*(brx-tlx));
end;
function mapW2SyLog(yf:float):integer;
begin
result:=round(bry-round((ln(yf/ylow)/ln(yhigh/ylow))*(bry-tly)));
end;
function mapS2WxLog(xs:integer):float;
begin
result:=xlow*exp(((xs-tlx)/(brx-tlx))*ln(xhigh/xlow));
end;
function mapS2WyLog(ys:integer):float;
begin
result:=yhigh/exp(((ys-tly)/(bry-tly)*ln(yhigh/ylow)));
end;

Improvements

Snap
The procedures above are the raw mapping procedures.
However when using the mouse one would wish to display
sensible numbers. Assume the screen is 100 pixels wide,
the cooresponding world 0..1, making 0.01 per pixel.
When the screen is only 99 pixels wide, the numbers become
ugly. One should define a snap of 0.01 and the world is reduced
to 0..0.99.
Some thoughts about implementing snap are here

Grid
A grid should be drawn at sensible nubers. In a world of 0..1
on 100 pixels, I'd want the grid at 0.0 0.2 0.4 0.6 0.8 1.0 and
certainly not at 0.03 0.23 0.43 0.63 0.83.
Some thoughts about implementing a grid are here