Back
{TPlot2D and TPlotBox2D classes)
author: Nikolai Shokhirev  )
created: (June 06, 2002)
last modified: August 08, 2003)
ŠNikolai V. Shokhirev, 2002-2003  }

{ The unit for simple 2D graphics }
{                                                       }
{ Basic notations and expressions                       }
{                                                       }
{        0       LdW                iW-RdW    iW    iX  }
{                                                       }
{      0 +--------+--------------------+-------+-- YT   }
{        |                                     |        }
{        |        Y                            |        }
{        |        |                            |        }
{   TdH  +  Ymax  +--------------------+       +        }
{        |        |                    |       |        }
{        |        |                    |       |        }
{        |        |                    |       |        }
{        |        |                    |       |        }
{        |        |                    |       |        }
{        |        |                    |       |        }
{        |        |                    |       |        }
{ iH-BdH +  Ymin  +--------------------+-- X   +        }
{        |                                     |        }
{        |       Xmin                 Xmax     |        }
{        |                                     |        }
{   iH   +--------+--------------------+-------+ YB     }
{        |                                              }
{        |                                              }
{   iY                                                  }
{ TdH - Plot Top offset (pixels)                        }
{ BdH - Plot Bottom offset (pixels)                     }
{       Ymin    <=  Y <= Ymax                           }
{ iH > iH - BdH >= iY >= TdH >= 0                       }
{                                                       }
{ LdW - Plot Left offset (pixels)                       }
{ RdW - Plot Right offset (pixels)                      }
{      Xmin <=  X <= Xmax                               }
{ 0 <= rLdW <= iX <= rW-rRdW < rW                       }
{
(ix-LdW)/(iW-RdW-LdW)=(x-Xmin)/(Xmax-Xmin)
(iH-BdH-iy)/(iH-BdH-TdH)=(y-Ymin)/(Ymax-Ymin)

(ix-LdW)*(Xmax-Xmin)=(iW-RdW-LdW)*(x-Xmin)
(iH-BdH-iy)*(Ymax-Ymin)=(iH-BdH-TdH)*(y-Ymin)

ix-LdW = (iW-RdW-LdW)*(x-Xmin)/(Xmax-Xmin)
iH-BdH-iy = (iH-BdH-TdH)*(y-Ymin)/(Ymax-Ymin)

x-Xmin = (ix-LdW)*(Xmax-Xmin)/(iW-RdW-LdW)
y-Ymin = (iH-BdH-iy)*(Ymax-Ymin)/(iH-BdH-TdH)

ix = iW - RdW
Xmax = Xmin + (iW-RdW-LdW)*(Xmax-Xmin)/(iW-RdW-LdW)

iy = TdH
Ymax = Ymin + (iH-BdH-TdH)*(Ymax-Ymin)/(iH-BdH-TdH)

ix = 0
XL = Xmin - LdW*(Xmax-Xmin)/(iW-RdW-LdW)

ix = iW
XR = Xmin + (iW-LdW)*(Xmax-Xmin)/(iW-RdW-LdW)
   = Xmax - (iW-RdW-LdW)*(Xmax-Xmin)/(iW-RdW-LdW)
          + (iW-LdW)*(Xmax-Xmin)/(iW-RdW-LdW)
   = Xmax + RdW*(Xmax-Xmin)/(iW-RdW-LdW)

iy = 0
YT = Ymin + (iH-BdH)*(Ymax-Ymin)/(iH-BdH-TdH)
   = Ymax - (iH-BdH-TdH)*(Ymax-Ymin)/(iH-BdH-TdH)
          + (iH-BdH)*(Ymax-Ymin)/(iH-BdH-TdH)
   = Ymax + TdH*(Ymax-Ymin)/(iH-BdH-TdH)

iy = iH
YB = Ymin - BdH*(Ymax-Ymin)/(iH-BdH-TdH)
}
{                                                    }
{       0      LdW                 W-RdW   iW    iX  }
{                                                    }
{     0 +-------+--------------------+------+-- YT   }
{       |                                   |        }
{       |    YTitle                         |        }
{       |                                   |        }
{  TdH  +  5e-1 +--------------------+      +        }
{       |       |                    |      |        }
{       |       |                    |      |        }
{       |       |                    |      |        }
{       |  0e+0 +                    |      |        }
{       |       |                    |      |        }
{       |       |                    |      |        }
{       |       |                    |      |        }
{ H-BdH + -5e-1 +---------+----------+      +        }
{       |      1.1e1     1.2e2      1.3e3   |        }
{       |                                   |        }
{       |                         XTitle    |        }
{       |                                   |        }
{   iH  +-------+--------------------+------+ YB     }
{       |                                            }
{       |                                            }
{   iY                                               }
unit uGraph2D;

interface

uses
  uMatTypes, extctrls, graphics, classes, controls;

type

  TYofX = function(const x: TFloat): TFloat;

  TPlot2D = class
  private
    fXmin: TFloat;
    fXmax: TFloat;
    fYmin: TFloat;
    fYmax: TFloat;
    // Canvas Dimensions [pixels]
    iH, iW: TInt;
    // Plot Offsets [pixels]
    TdH, BdH, LdW, RdW: TInt;
    XDec: TInt; // X-Label decimals
    YDec: TInt; // Y-Label decimals
    fCanvas: TCanvas;
    rH, rTdH, rBdH: TFloat;
    rW, rLdW, rRdW: TFloat;
    function GetXmin: TFloat;
    procedure SetXmin(const Value: TFloat);
    function GetXmax: TFloat;
    procedure SetXmax(const Value: TFloat);
    function GetYmin: TFloat;
    procedure SetYmin(const Value: TFloat);
    function GetYmax: TFloat;
    procedure SetYmax(const Value: TFloat);
    function GetCanvas: TCanvas;
    procedure SetCanvas(const Value: TCanvas);
    function GetHeight: TInt;
    function GetWidth: TInt;
    procedure SetHeight(const Value: TInt);
    procedure SetWidth(const Value: TInt);
    function Yr2i(Y: TFloat): TInt;
    { Yr2i(Ymin) = iH - BdH,  Yr2i(Ymax) = TdH }
    function Yi2r(iY: TInt): TFloat;
    { Yi2r(iH - BdH) = Ymin,  Yi2r(TdH) = Ymax}
    function Xr2i(X: TFloat): TInt;
    { Xr2i(Xmin) = rLdW,  Xr2i(Xmax) = iW-rRdW }
    function Xi2r(iX: TInt): TFloat;
    { Xi2r(rLdW) = Xmin,  Xi2r(iW-rRdW) = Xmax}

//    function RealToStr(z: TFloat; decimals: TInt): string;
    { scientific notation           }
    { decimals = 0 example: 7e+12   }
    { decimals = 1 example: 1.3e+0  }
    { decimals = 2 example: 1.23e-1 }

//    function MinDec(Xmin, Xmax: TFloat): TInt;
    { minimal decimals }
  public
    constructor Create(Xmin, Xmax, Ymin, Ymax: TFloat);
    destructor Destroy; override;
    property Xmin: TFloat read GetXmin write SetXmin;
    property Xmax: TFloat read GetXmax write SetXmax;
    property Ymin: TFloat read GetYmin write SetYmin;
    property Ymax: TFloat read GetYmax write SetYmax;
    property Canvas: TCanvas read GetCanvas write SetCanvas;
    property Height: TInt read GetHeight write SetHeight;
    property Width: TInt read GetWidth write SetWidth;
    procedure MoveToF(x, y: TFloat);
    procedure LineToF(x, y: TFloat);
    procedure LineF(x1, y1, x2, y2: TFloat);
    procedure PlotFrame(XCaption, YCaption: boolean; NXTicks, NYTicks: TInt;
                        const XTitle, YTitle: string; XYRatio: TFloat = 1.0);
   end;

  TPlotBox2D = class(TPaintBox)
  private
    fXmin: TFloat;
    fXmax: TFloat;
    fYmin: TFloat;
    fYmax: TFloat;
    // Canvas Dimensions [pixels]
    // Plot Offsets [pixels]
    TdH, BdH, LdW, RdW: TInt;
    XDec: TInt; // X-Label decimals
    YDec: TInt; // Y-Label decimals
    rH, rTdH, rBdH: TFloat;
    rW, rLdW, rRdW: TFloat;
    fXCaption: boolean;
    fYCaption: boolean;
    fXTitle: string;
    fYTitle: string;
    fNYTicks: TInt;
    fNXTicks: TInt;
    fXYRatio: TFloat;
    fFrameColor: TColor;
    function GetXmin: TFloat;
    procedure SetXmin(const Value: TFloat);
    function GetXmax: TFloat;
    procedure SetXmax(const Value: TFloat);
    function GetYmin: TFloat;
    procedure SetYmin(const Value: TFloat);
    function GetYmax: TFloat;
    procedure SetYmax(const Value: TFloat);
    function Yr2i(Y: TFloat): TInt;
    { Yr2i(Ymin) = iH - BdH,  Yr2i(Ymax) = TdH }
    function Xr2i(X: TFloat): TInt;
    { Xr2i(Xmin) = rLdW,  Xr2i(Xmax) = iW-rRdW }
    function GetPenWidth: TInt;
    procedure SetPenWidth(const Value: TInt);
    function GetPenColor: TColor;
    procedure SetPenColor(const Value: TColor);
    function GetBrushColor: TColor;
    procedure SetBrushColor(const Value: TColor);
  public
    constructor Create(aOwner: TComponent; aParent: TWinControl);
    destructor Destroy; override;
    { Xi2r(rLdW) = Xmin,  Xi2r(iW-rRdW) = Xmax}
    function Xi2r(iX: TInt): TFloat;
    { Yi2r(iH - BdH) = Ymin,  Yi2r(TdH) = Ymax}
    function Yi2r(iY: TInt): TFloat;
    procedure SetLimits(Xmin, Xmax, Ymin, Ymax: TFloat);
    procedure Circle(x, y: TFloat; r: TInt);
    procedure MoveToF(x, y: TFloat);
    procedure LineToF(x, y: TFloat);
    procedure LineF(x1, y1, x2, y2: TFloat);
    procedure PlotFrame;
    property Xmin: TFloat read GetXmin write SetXmin;
    property Xmax: TFloat read GetXmax write SetXmax;
    property Ymin: TFloat read GetYmin write SetYmin;
    property Ymax: TFloat read GetYmax write SetYmax;
    property BrushColor: TColor read GetBrushColor write SetBrushColor;
    property PenColor: TColor read GetPenColor write SetPenColor;
    property PenWidth: TInt read GetPenWidth write SetPenWidth;
    property XCaption: boolean read fXCaption write fXCaption;
    property YCaption: boolean read fYCaption write fYCaption;
    property NXTicks: TInt read fNXTicks write fNXTicks;
    property NYTicks: TInt read fNYTicks write fNYTicks;
    property XTitle: string read fXTitle write fXTitle;
    property YTitle: string read fYTitle write fYTitle;
    property XYRatio: TFloat read fXYRatio write fXYRatio;
    property FrameColor: TColor read fFrameColor write fFrameColor;
   end;
{
  TFuncBox2D = class(TPlotBox2D)
  private

  end;
}
implementation

uses
  math, Sysutils, uDisplay;

end.

Back

Generated by Lore's Source to HTML Converter(http://www.newty.de/lsc/index.html)