| 1 |
unit gridaxes; |
| 2 |
{$IFDEF FPC} |
| 3 |
{$MODE DELPHI}{$H+} |
| 4 |
{$ENDIF} |
| 5 |
(***************************************) |
| 6 |
(* Copyright (C) 2009, SHIRAISHI Kazuo *) |
| 7 |
(***************************************) |
| 8 |
|
| 9 |
|
| 10 |
interface |
| 11 |
|
| 12 |
function drawaxes0(x,y:extended):boolean; |
| 13 |
function drawgrid0(x,y:extended):boolean; |
| 14 |
function drawaxes2(x,y:extended):boolean; |
| 15 |
function drawgrid2(x,y:extended):boolean; |
| 16 |
function drawcircle(x,y:extended):boolean; |
| 17 |
function drawdisk(x,y:extended):boolean; |
| 18 |
|
| 19 |
|
| 20 |
implementation |
| 21 |
|
| 22 |
uses Types, SysUtils,Graphics, |
| 23 |
base,affine,graphsys,arithmet; |
| 24 |
|
| 25 |
{**************} |
| 26 |
{axes and grids} |
| 27 |
{**************} |
| 28 |
|
| 29 |
function convtodevice(x,y:extended; var i,j:integer):boolean; |
| 30 |
begin |
| 31 |
result:=currenttransform.transform(x,y) |
| 32 |
and MyGraphSys.ConvToDevicex(x,i) |
| 33 |
and MyGraphSys.ConvToDevicey(y,j); |
| 34 |
end; |
| 35 |
|
| 36 |
function convtovirtual(i,j:integer; var x,y:extended):boolean; |
| 37 |
begin |
| 38 |
x:=MyGraphSys.virtualx(i); |
| 39 |
y:=MyGraphSys.virtualy(j); |
| 40 |
result:=currenttransform.invtransform(x,y); |
| 41 |
end; |
| 42 |
|
| 43 |
function getboundary(var xmin,xmax,ymin,ymax:extended):boolean; |
| 44 |
function imax(a,b:integer):integer; |
| 45 |
begin |
| 46 |
if a<b then imax:=b |
| 47 |
else imax:=a |
| 48 |
end; |
| 49 |
function max(a,b:extended):extended; |
| 50 |
begin |
| 51 |
if a<b then max:=b |
| 52 |
else max:=a |
| 53 |
end; |
| 54 |
function min(a,b:extended):extended; |
| 55 |
begin |
| 56 |
if a<b then min:=a |
| 57 |
else min:=b |
| 58 |
end; |
| 59 |
var |
| 60 |
p:array[1..4]of array[1..2] of extended; |
| 61 |
i:integer; |
| 62 |
w:word; |
| 63 |
cont:boolean; |
| 64 |
begin |
| 65 |
cont:=true; |
| 66 |
with MyGraphSys.ClipRect do |
| 67 |
begin |
| 68 |
cont:=convtovirtual(left,top,p[1][1],p[1][2]) |
| 69 |
and convtovirtual(left,bottom,p[2][1],p[2][2]) |
| 70 |
and convtovirtual(right,top,p[3][1],p[3][2]) |
| 71 |
and convtovirtual(right,bottom,p[4][1],p[4][2]); |
| 72 |
end; |
| 73 |
{ |
| 74 |
w:=iMax(system.round(MyGraphSys.Gwidth),System.Round(MyGraphSys.GHeight))*3 div 2; |
| 75 |
convtovirtual(0,0,p[1][1],p[1][2]); |
| 76 |
convtovirtual(0,w,p[2][1],p[2][2]); |
| 77 |
convtovirtual(w,0,p[3][1],p[3][2]); |
| 78 |
convtovirtual(w,w,p[4][1],p[4][2]); |
| 79 |
} |
| 80 |
if cont then |
| 81 |
begin |
| 82 |
xmin:=p[1][1]; |
| 83 |
for i:=2 to 4 do xmin:=min(p[i][1],xmin); |
| 84 |
xmax:=p[1][1]; |
| 85 |
for i:=2 to 4 do xmax:=max(p[i][1],xmax); |
| 86 |
ymin:=p[1][2]; |
| 87 |
for i:=2 to 4 do ymin:=min(p[i][2],ymin); |
| 88 |
ymax:=p[1][2]; |
| 89 |
for i:=2 to 4 do ymax:=max(p[i][2],ymax); |
| 90 |
end; |
| 91 |
result:=cont; |
| 92 |
end; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
procedure line1(a1,b1,a2,b2:integer); |
| 98 |
begin |
| 99 |
with MyGraphSys do |
| 100 |
line(a1,b1,a2,b2,axescolor,psSolid,linewidth); |
| 101 |
end; |
| 102 |
|
| 103 |
procedure line2(a1,b1,a2,b2:integer); |
| 104 |
begin |
| 105 |
MyGraphSys.line(a1,b1,a2,b2,axescolor,psDot,1); |
| 106 |
end; |
| 107 |
|
| 108 |
function axessub:boolean; |
| 109 |
var |
| 110 |
xmin,xmax,ymin,ymax:extended; |
| 111 |
i1,i2,j1,j2:integer; |
| 112 |
begin |
| 113 |
axessub:=true; |
| 114 |
|
| 115 |
if getboundary(xmin,xmax,ymin,ymax)then |
| 116 |
begin |
| 117 |
if convtodevice(xmin,0,i1,j1) and |
| 118 |
convtodevice(xmax,0,i2,j2) then |
| 119 |
line1(i1,j1,i2,j2); |
| 120 |
|
| 121 |
if convtodevice(0,ymin,i1,j1) and |
| 122 |
convtodevice(0,ymax,i2,j2) then |
| 123 |
line1(i1,j1,i2,j2); |
| 124 |
end; |
| 125 |
end; |
| 126 |
|
| 127 |
function ceil(x:extended):extended;forward; |
| 128 |
function floor(x:extended):extended; |
| 129 |
begin |
| 130 |
if x>=0 then floor:=int(x) |
| 131 |
else floor:=-ceil(-x) |
| 132 |
end; |
| 133 |
|
| 134 |
function ceil(x:extended):extended; |
| 135 |
begin |
| 136 |
if x>=0 then |
| 137 |
begin |
| 138 |
if int(x)=x then |
| 139 |
ceil:=int(x) |
| 140 |
else |
| 141 |
ceil:=int(x)+1 |
| 142 |
end |
| 143 |
else ceil:=-floor(-x) |
| 144 |
end; |
| 145 |
|
| 146 |
function marksub(sx,sy:extended):boolean; |
| 147 |
var |
| 148 |
x,y:extended; |
| 149 |
xmin,xmax,ymin,ymax:extended; |
| 150 |
i,j:integer; |
| 151 |
svpointstyle:byte; |
| 152 |
svpointcolor:integer; |
| 153 |
begin |
| 154 |
marksub:=true; |
| 155 |
if (sx=0) or (sy=0) then exit; |
| 156 |
|
| 157 |
svpointstyle:=MyGraphSys.pointstyle; |
| 158 |
svpointcolor:=MyGraphSys.pointcolor; |
| 159 |
MyGraphSys.pointstyle:=2; |
| 160 |
MyGraphSys.PointColor:=axescolor; |
| 161 |
|
| 162 |
if getboundary(xmin,xmax,ymin,ymax) then |
| 163 |
begin |
| 164 |
xmin:=floor(xmin/sx)*sx; |
| 165 |
ymin:=floor(ymin/sy)*sy; |
| 166 |
xmax:=ceil(xmax/sx)*sx; |
| 167 |
ymax:=ceil(ymax/sy)*sy; |
| 168 |
|
| 169 |
x:=xmin; |
| 170 |
if (sx>0) and ((xmax-xmin)/sx<1024) then |
| 171 |
while (x<=xmax +sx/2) do |
| 172 |
begin |
| 173 |
if convtodevice(x,0,i,j) then |
| 174 |
MyGraphSys.putmark0(i,j); |
| 175 |
x:=x+sx; |
| 176 |
idle; |
| 177 |
end; |
| 178 |
|
| 179 |
y:=ymin; |
| 180 |
if (sy>0) and ((ymax-ymin)/sy<1024) then |
| 181 |
while (y<=ymax +sy/2) do |
| 182 |
begin |
| 183 |
if convtodevice(0,y,i,j) then |
| 184 |
MyGraphSys.putmark0(i,j); |
| 185 |
y:=y+sy; |
| 186 |
idle; |
| 187 |
end; |
| 188 |
end; |
| 189 |
MyGraphSys.pointstyle:=svpointstyle; |
| 190 |
MyGraphSys.pointcolor:=svpointcolor; |
| 191 |
end; |
| 192 |
|
| 193 |
|
| 194 |
function gridsub(sx,sy:extended):boolean; |
| 195 |
var |
| 196 |
x,y:extended; |
| 197 |
xmin,xmax,ymin,ymax:extended; |
| 198 |
i1,i2,j1,j2:integer; |
| 199 |
begin |
| 200 |
gridsub:=true; |
| 201 |
if (sx=0) or (sy=0) then exit; |
| 202 |
|
| 203 |
getboundary(xmin,xmax,ymin,ymax); |
| 204 |
xmin:=floor(xmin/sx)*sx; |
| 205 |
ymin:=floor(ymin/sy)*sy; |
| 206 |
xmax:=ceil(xmax/sx)*sx; |
| 207 |
ymax:=ceil(ymax/sy)*sy; |
| 208 |
|
| 209 |
x:=xmin; |
| 210 |
if (sx>0) and ((xmax-xmin)/sx<1024) then |
| 211 |
while (x<=xmax +sx/2) do |
| 212 |
begin |
| 213 |
if convtodevice(x,ymin,i1,j1) and |
| 214 |
convtodevice(x,ymax,i2,j2) then |
| 215 |
line2(i1,j1,i2,j2); |
| 216 |
x:=x+sx; |
| 217 |
idle; |
| 218 |
end; |
| 219 |
|
| 220 |
y:=ymin; |
| 221 |
if (sy>0) and ((ymax-ymin)/sy<1024) then |
| 222 |
while (y<=ymax +sy/2) do |
| 223 |
begin |
| 224 |
if convtodevice(xmin,y,i1,j1) and |
| 225 |
convtodevice(xmax,y,i2,j2) then |
| 226 |
line2(i1,j1,i2,j2); |
| 227 |
y:=y+sy; |
| 228 |
idle; |
| 229 |
end; |
| 230 |
|
| 231 |
end; |
| 232 |
|
| 233 |
function str3(x,sx:extended):string; |
| 234 |
function int(sx:extended):longint; |
| 235 |
begin |
| 236 |
result:=trunc(sx); |
| 237 |
if (sx<0) and (result<>sx) then dec(result) |
| 238 |
end; |
| 239 |
var |
| 240 |
a,b,n:number; |
| 241 |
i:longint; |
| 242 |
begin |
| 243 |
convert(x,a); |
| 244 |
i:=2-int(system.ln(sx)/system.ln(10)); |
| 245 |
initlongint(n,i); |
| 246 |
arithmet.round(a,n,b); |
| 247 |
result:=DSTR(b); |
| 248 |
end; |
| 249 |
|
| 250 |
function CoordinateSub(sx,sy:extended):boolean; |
| 251 |
var |
| 252 |
x,y:extended; |
| 253 |
xmin,xmax,ymin,ymax:extended; |
| 254 |
i,j:integer; |
| 255 |
svtextcolor:integer; |
| 256 |
svTjH:tjHorizontal; |
| 257 |
svTjV:tjVirtical; |
| 258 |
s:string; |
| 259 |
begin |
| 260 |
result:=true; |
| 261 |
if (sx=0) or (sy=0) then exit; |
| 262 |
svtextcolor:=MyGraphSys.textcolor; |
| 263 |
MyGraphSys.settextcolor(axesColor); |
| 264 |
svTjH:=MyGraphSys.Hjustify; |
| 265 |
svTjV:=MyGraphSys.Vjustify; |
| 266 |
MyGraphSys.Hjustify:=TjRight; |
| 267 |
MyGraphSys.Vjustify:=TjTop; |
| 268 |
|
| 269 |
if getboundary(xmin,xmax,ymin,ymax) then |
| 270 |
begin |
| 271 |
xmin:=floor(xmin/sx)*sx; |
| 272 |
ymin:=floor(ymin/sy)*sy; |
| 273 |
xmax:=ceil(xmax/sx)*sx; |
| 274 |
ymax:=ceil(ymax/sy)*sy; |
| 275 |
|
| 276 |
x:=xmin; |
| 277 |
if (sx<>0) and ((xmax-xmin)/sx<1024) then |
| 278 |
while (x<=xmax +sx/2) do |
| 279 |
begin |
| 280 |
s:=str3(x,sx); |
| 281 |
if convtodevice(x,0,i,j) then |
| 282 |
MyGraphSys.textout(i,j,s, MyGraphSys.xdirection(x,0)); |
| 283 |
x:=x+sx; |
| 284 |
idle; |
| 285 |
end; |
| 286 |
|
| 287 |
y:=ymin; |
| 288 |
if (sy<>0) and ((ymax-ymin)/sy<1024) then |
| 289 |
while (y<=ymax +sy/2) do |
| 290 |
begin |
| 291 |
s:=str3(y,sy); |
| 292 |
if convtodevice(0,y,i,j) then |
| 293 |
MyGraphSys.textout(i,j,s, MyGraphSys.xdirection(0,y)); |
| 294 |
y:=y+sy; |
| 295 |
idle; |
| 296 |
end; |
| 297 |
end; |
| 298 |
|
| 299 |
MyGraphSys.settextcolor(svTextColor); |
| 300 |
MyGraphSys.Hjustify:=svTjH; |
| 301 |
MyGraphSys.Vjustify:=svTjV; |
| 302 |
end; |
| 303 |
|
| 304 |
|
| 305 |
function drawaxes0(x,y:extended):boolean; |
| 306 |
begin |
| 307 |
drawaxes0:=axessub and marksub(x,y) |
| 308 |
end; |
| 309 |
|
| 310 |
function drawgrid0(x,y:extended):boolean; |
| 311 |
begin |
| 312 |
drawgrid0:=gridsub(x,y) and axessub |
| 313 |
end; |
| 314 |
|
| 315 |
function drawaxes2(x,y:extended):boolean; |
| 316 |
begin |
| 317 |
drawaxes2:=axessub and marksub(x,y) and CoordinateSub(x,y) |
| 318 |
end; |
| 319 |
|
| 320 |
function drawgrid2(x,y:extended):boolean; |
| 321 |
begin |
| 322 |
drawgrid2:=gridsub(x,y) and axessub and CoordinateSub(x,y) |
| 323 |
end; |
| 324 |
|
| 325 |
function drawcircle(x,y:extended):boolean; |
| 326 |
var |
| 327 |
points:array[0..360] of TPoint; |
| 328 |
i,j:integer; |
| 329 |
n,k:integer; |
| 330 |
begin |
| 331 |
k:=0; |
| 332 |
for n:=0 to 360 do |
| 333 |
begin |
| 334 |
x:=cos(n/180*pi); |
| 335 |
y:=sin(n/180*pi); |
| 336 |
if convtodevice(x,y,i,j) then |
| 337 |
begin |
| 338 |
points[k].x:=restrict(i); |
| 339 |
points[k].y:=restrict(j); |
| 340 |
inc(k) |
| 341 |
end; |
| 342 |
end; |
| 343 |
MyGraphSys.Polyline(Slice(Points,k)) ; |
| 344 |
result:=true; |
| 345 |
end; |
| 346 |
|
| 347 |
|
| 348 |
function drawdisk(x,y:extended):boolean; |
| 349 |
var |
| 350 |
points:array[0..359] of TPoint; |
| 351 |
i,j:integer; |
| 352 |
n,k:integer; |
| 353 |
begin |
| 354 |
k:=0; |
| 355 |
for n:=0 to 359 do |
| 356 |
begin |
| 357 |
x:=cos(n/180*pi); |
| 358 |
y:=sin(n/180*pi); |
| 359 |
if convtodevice(x,y,i,j) then |
| 360 |
begin |
| 361 |
points[k].x:=restrict(i); |
| 362 |
points[k].y:=restrict(j); |
| 363 |
inc(k); |
| 364 |
end; |
| 365 |
end; |
| 366 |
MyGraphSys.Polygon(Slice(Points,k)); |
| 367 |
result:=true; |
| 368 |
end; |
| 369 |
|
| 370 |
|
| 371 |
end. |
| 372 |
|