JGGraphExt.pas:
- unit JGGraphExt;
- interface
- uses Graph,SysUtils;
- type
- tFuncDoubleOfDouble=function(x:double): double;
- tAxis= (AxisX,AxisY);
- tWinSet= record
- i1,i2,j1,j2: integer;
- x1,x2,y1,y2: double
- end;
- var
- WinSettings: tWinSet;
- function WinX(x: double): integer;
- function WinY(y: double): integer;
- procedure WinLine(x1,y1,x2,y2: double);
- procedure WinSet(u1,v1,u2,v2: double; a1,b1,a2,b2: integer);
- procedure Axis(c: tAxis; t1,t2,t0,d: double; l,col: integer);
- procedure PlotFunc(d,minX,maxX: double; c: integer; f: tFuncDoubleofDouble);
- procedure Frame(c: integer);
- procedure GrOn;
- procedure GrOff;
- implementation
- procedure GrOn;
- var
- gd,gm: integer;
- begin
- gd:= detect;
- InitGraph(gd,gm,'')
- end;
- procedure GrOff;
- begin
- CloseGraph
- end;
- function WinX(x: double): integer;
- begin
- with WinSettings do WinX:= Round((x-x1)/(x2-x1)*(i2-i1))
- end;
- function WinY(y: double): integer;
- begin
- with WinSettings do WinY:= j2-Round((y-y1)/(y2-y1)*(j2-j1))-j1
- end;
- procedure WinLine(x1,y1,x2,y2: double);
- begin
- Line(WinX(x1),WinY(y1),WinX(x2),WinY(y2))
- end;
- procedure WinSet(u1,v1,u2,v2: double; a1,b1,a2,b2: integer);
- begin
- with WinSettings do begin
- if a1<0 then i1:= GetMaxX+a1 else i1:= a1;
- if b1<0 then j1:= GetMaxY+b1 else j1:= b1;
- if a2<0 then i2:= GetMaxX+a2 else i2:= a2;
- if b2<0 then j2:= GetMaxY+b2 else j2:= b2;
- x1:= u1;
- y1:= v1;
- if u2<=u1 then x2:= x1+(v2-v1)/(j2-j1)*(i2-i1) else x2:= u2;
- if v2<=v1 then y2:= y1+(u2-u1)/(i2-i1)*(j2-j1) else y2:= v2;
- SetViewPort(i1,j1,i2,j2,true) // клиппинг
- end
- end;
- procedure Axis(c: tAxis; t1,t2,t0,d: double; l,col: integer);
- var
- t: double; c0: integer;
- begin
- with WinSettings do begin
- c0:= GetColor;
- SetColor(col);
- if c=AxisX then WinLine(t1,t0,t2,t0) else WinLine(t0,t1,t0,t2);
- t:= int((t1+d*0.1)/d)*d;
- while t<t2-d*0.1 do begin
- if c=AxisY then begin
- Line(WinX(t0)-l,WinY(t),WinX(t0)+l,WinY(t));
- OutTextXY(WinX(t0)+l*4,WinY(t)-3,FloatToStrF(t,ffFixed,5,2))
- end
- else begin
- Line(WinX(t),WinY(t0)-l,WinX(t),WinY(t0)+l);
- OutTextXY(WinX(t)-3,WinY(t0)+l*4,FloatToStrF(t,ffFixed,5,2))
- end;
- t:= t+d
- end;
- SetColor(c0)
- end
- end;
- procedure PlotFunc(d,minX,maxX: double; c: integer; f: tFuncDoubleofDouble);
- var
- y: double; c0: integer;
- begin
- with WinSettings do begin
- c0:= GetColor;
- Setcolor(c);
- y:= f(minX);
- MoveTo(WinX(minX),WinY(y));
- while minX<=maxX do begin
- y:= f(minX);
- LineTo(WinX(minX),WinY(y));
- minX:= minX+d
- end;
- SetColor(c0)
- end
- end;
- procedure Frame(c: integer);
- var
- c0: integer;
- begin
- c0:= Getcolor;
- Setcolor(c);
- with WinSettings do Rectangle(0,0,i2-i1,j2-j1);
- Setcolor(c0);
- end;
- begin
- end.
test.pas:
- uses
- WinCrt, Graph, SysUtils, JGGraphExt;
- function YY(x: double): double;
- begin
- YY:= x*(x*(x-5)-100)-100
- end;
- function Y_sin(x: double): double;
- begin
- Y_sin:= 500*sin(x)*sin(x*x*x*x*x*x)
- end;
- begin
- GrOn;
- // Plot_1
- WinSet(-20,-1000,20,1000,50,50,500,700);
- with WinSettings do begin
- Frame(lightGray);
- Axis(AxisX,x1,x2,300,10,2,LightGray);
- Axis(AxisY,y1,y2,0,200,2,LightGray);
- PlotFunc(0.1,x1,x2,LightGreen,@YY);
- end;
- // Plot_2
- with WinSettings do begin
- WinSet(-20,-1000,20,1000,550,50,1300,700);
- Frame(lightGray);
- Axis(AxisX,x1,x2,300,10,2,LightGray);
- Axis(AxisY,y1,y2,0,200,2,LightGray);
- PlotFunc(0.1,x1,x2,blue,@Y_sin)
- end;
- ReadKey;
- GrOff
- end.
No comments:
Post a Comment