unit GeoDrv;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, Buttons, ExtCtrls, ToolWin, ComCtrls, ImgList;
type
TGeoDrv=class(TObject) //设备驱动对象类
private
sscale:single;
screenleft,screentop,screenright,screenbottom:integer;
public
xworldmin,yworldmin,xworldmax,yworldmax:single;
procedure SetMapExtent(x1,y1,x2,y2:single);
procedure Resize(ll,tt,ww,hh:integer);virtual;//重新设置大小
procedure AdjustScale();//调整比例
procedure XYWorldToScreen(x,y:single;var xx,yy:integer);virtual;
//世界坐标转为屏幕坐标;
procedure XYScreenToWorld(x1,y1:integer;var xx,yy:single);virtual;
//屏幕坐标转化为世界坐标;
//初始化世界坐标和窗口坐标范围;
//世界坐标的极值为X轴(精度)-180至+180,y轴(纬度)-90至+90。
//窗口坐标总是从(0,0)开始,可绘图区域程序初始时定为(800*600)。
constructor TGeoDrv.create(ACanvas:TCanvas);
//构造图形设备接口类
begin
inherited Create;
xworldmin:=0;
yworldmin:=0;
xworldmax:=800;
yworldmax:=600;
screenleft:=0;
screentop:=0;
screenright:=800;
screenbottom:=600;
sscale:=1;
end;
//设置地图大小
procedure TGeoDrv.SetMapExtent(x1,y1,x2,y2:single);
var xx1,yy1,xx2,yy2:integer;
begin
xworldmin:=x1;
yworldmin:=y1;
xworldmax:=x2;
yworldmax:=y2;
adjustscale;
end;
//改变屏幕大小
procedure TGeoDrv.Resize(ll,tt,ww,hh:integer);
begin
screenleft:=ll;
screentop:=tt;
screenright:=ww;
screenbottom:=hh;
adjustscale();
end;
procedure TGeoDrv.XYWorldToScreen(x,y:single;var xx,yy:integer);
//世界坐标转为屏幕坐标;
begin
xx:=trunc((x-xworldmin)*sscale)+screenleft;
yy:=trunc((yworldmax-y)*sscale)+screentop;
end;
procedure TGeoDrv.XYScreenToWorld(x1,y1:integer;var xx,yy:single);
//屏幕坐标转化为世界坐标;
begin
xx:=(x1-screenleft)/sscale+xworldmin;
yy:=yworldmax-(y1-screentop)/sscale;
end;
//调整坐标,转化比例参数;
procedure TGeoDrv.AdjustScale();
var sx,sy:single;
begin
//计算坐标转化比例参数
sx:=(xworldmax-xworldmin)/(screenright-screenleft);
sy:=(yworldmax-yworldmin)/(screenbottom-screentop);
//取比较大的一个参数以保证尽可能多的显示出图形
// X轴和Y轴使用系统参数,以保证图形不变
if sx<sy then
sscale:=1.0/sy
else
sscale:=1.0/sx;
//同时更新世界坐标范围
xworldmax:=xworldmin+(screenright-screenleft)/sscale;
yworldmin:=yworldmax-(screenbottom-screentop)/sscale;
end;