home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Delphi1_And_Delphi2 / EXAMPLES / EXTENDED / SURFACE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  7.3 KB  |  251 lines

  1. {*********************************************}
  2. { TeeChart Delphi Component Library           }
  3. { Surface Series Demo                         }
  4. { Copyright (c) 1996 by David Berneda         }
  5. { All rights reserved                         }
  6. {*********************************************}
  7. unit Surface;
  8.  
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  13.   Forms, Dialogs, ExtCtrls, Teengine, Chart, StdCtrls, TeeSurfa,
  14.   Spin, Series, Buttons, teeprocs, TeeComma;
  15.  
  16. type
  17.   TSurfaceForm = class(TForm)
  18.     Chart1: TChart;
  19.     Panel1: TPanel;
  20.     CheckBox1: TCheckBox;
  21.     SurfaceSeries1: TSurfaceSeries;
  22.     SpinEdit1: TSpinEdit;
  23.     SpinEdit2: TSpinEdit;
  24.     Label2: TLabel;
  25.     Timer1: TTimer;
  26.     Animation1: TCheckBox;
  27.     Label1: TLabel;
  28.     RGColorMode: TRadioGroup;
  29.     BitBtn3: TBitBtn;
  30.     SpinEditRed: TSpinEdit;
  31.     SpinEditBlue: TSpinEdit;
  32.     SpinEditGreen: TSpinEdit;
  33.     SpinEdit3: TSpinEdit;
  34.     CheckBox3: TCheckBox;
  35.     Label3: TLabel;
  36.     TeeCommander1: TTeeCommander;
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure CheckBox1Click(Sender: TObject);
  39.     function SurfaceSeries1GetYValue(Sender: TChartSeries; X,
  40.       Z: Longint): Double;
  41.     procedure SpinEdit1Change(Sender: TObject);
  42.     procedure SpinEdit2Change(Sender: TObject);
  43.     procedure Animation1Click(Sender: TObject);
  44.     procedure Timer1Timer(Sender: TObject);
  45.     procedure RGColorModeClick(Sender: TObject);
  46.     procedure SpinEditBlueChange(Sender: TObject);
  47.     procedure CheckBox3Click(Sender: TObject);
  48.     procedure BitBtn1Click(Sender: TObject);
  49.     procedure BitBtn2Click(Sender: TObject);
  50.   private
  51.     { Private declarations }
  52.   public
  53.     { Public declarations }
  54.   end;
  55.  
  56. implementation
  57.  
  58. {$R *.DFM}
  59. uses EditChar,SurfEdit,TeCanvas;
  60.  
  61. procedure TSurfaceForm.FormCreate(Sender: TObject);
  62. begin
  63.   { warning if video don't supports true or high color }
  64.   if not Chart1.IsScreenHighColor then
  65.      ShowMessage('This demo looks much better with'+#13+
  66.                  '16k colors or greater.');
  67.  
  68.   TeeEraseBack:=False;  { <-- this will allow Win95+Plus! resize smoothly }
  69.  
  70.   { set the maximum allowed Surface grid dimensions for this demo }
  71.   SpinEdit2.MaxValue:=MaxAllowedCells;
  72.   SpinEdit3.MaxValue:=MaxAllowedCells;
  73.  
  74.   { default surface grid size for this demo }
  75.   SpinEdit2.Value:=20;
  76.   SpinEdit3.Value:=20;
  77.  
  78.   { initialize some values... }
  79.   With SurfaceSeries1 do
  80.   Begin
  81.     FillSampleValues(20);
  82.     Pen.Color:=clBlack;
  83.   end;
  84.   Chart1.Gradient.Visible:=Chart1.IsScreenHighColor;
  85. end;
  86.  
  87. procedure TSurfaceForm.CheckBox1Click(Sender: TObject);
  88. begin
  89.   SurfaceSeries1.WireFrame:=CheckBox1.Checked;
  90. end;
  91.  
  92. function TSurfaceForm.SurfaceSeries1GetYValue(Sender: TChartSeries; X,
  93.   Z: Longint): Double;
  94. var PiPortion,HalfPi,tmpx,tmpz:Double;
  95. begin
  96.   result:=0;
  97.   With SurfaceSeries1 do
  98.   begin
  99.     PiPortion:=Pi/(NumXValues);
  100.     HalfPi:=Pi*0.5;
  101.     tmpX:=x*PiPortion;
  102.     tmpZ:=z*PiPortion;
  103.     Case SpinEdit1.Value of { sample surfaces }
  104.       1: result:=0.5*sqr(Cos(x/(NumXValues*0.2)))+
  105.                      sqr(Cos(z/(NumXValues*0.2)))-
  106.                      cos(z/(NumXValues*0.5));
  107.       2: result:= sqr( Cos(tmpx) ) * sqr( Sin(tmpz) );
  108.       3: result:= cos(tmpx*tmpx)+sin(tmpz*tmpz);
  109.       4: result:= sqr(cos(tmpx))+sqr(sin(tmpz));
  110.       5: result:= -tmpx+sqr(tmpz)*sin(tmpx*tmpz);
  111.       6: result:= Sqrt(tmpx * tmpx + tmpz * tmpz);
  112.       7: result:= cos(abs(tmpx-HalfPi))*sin(tmpz);
  113.       8: result:= cos(abs(tmpx-HalfPi)*abs(tmpz-HalfPi));
  114.     end;
  115.   end;
  116. end;
  117.  
  118. procedure TSurfaceForm.SpinEdit1Change(Sender: TObject);
  119. begin
  120.   SurfaceSeries1.ReCreateValues;
  121. end;
  122.  
  123. procedure TSurfaceForm.SpinEdit2Change(Sender: TObject);
  124. begin
  125.   if Sender=SpinEdit2 then
  126.      SurfaceSeries1.NumXValues:=SpinEdit2.Value
  127.   else
  128.      SurfaceSeries1.NumZValues:=SpinEdit3.Value;
  129. end;
  130.  
  131. procedure TSurfaceForm.Animation1Click(Sender: TObject);
  132. begin
  133.   Randomize;
  134.   Timer1.Enabled:=Not Timer1.Enabled;  { start / stop timer }
  135. end;
  136.  
  137. procedure TSurfaceForm.Timer1Timer(Sender: TObject);
  138.  
  139.   Function RandomColor:TColor;
  140.   begin { get a random color }
  141.     result:=ColorPalette[1+Random(MaxDefaultColors)]
  142.   end;
  143.  
  144. begin
  145.   Timer1.Enabled:=False;  { stop animation timer }
  146.  
  147.   { Invert Left axis randomly }
  148.   if Random(100)<2 then
  149.      Chart1.LeftAxis.Inverted:=not Chart1.LeftAxis.Inverted;
  150.  
  151.   { Invert Bottom axis randomly }
  152.   if Random(100)<2 then
  153.      Chart1.BottomAxis.Inverted:=not Chart1.BottomAxis.Inverted;
  154.  
  155.   { Invert Depth axis randomly }
  156.   if Random(100)<2 then
  157.      Chart1.DepthAxis.Inverted:=not Chart1.DepthAxis.Inverted;
  158.  
  159.   { Change Color Mode (Single, Range or Palette) randomly }
  160.   if Random(100)<2 then
  161.      if RGColorMode.ItemIndex<2 then
  162.         RGColorMode.ItemIndex:=RGColorMode.ItemIndex+1
  163.      else
  164.         RGColorMode.ItemIndex:=0;
  165.  
  166.   { Change Chart Gradient Colors randomly (only at 16k colors or greater) }
  167.   if Random(100)<10 then Chart1.Gradient.StartColor:=RandomColor else
  168.   if Random(100)<10 then Chart1.Gradient.EndColor:=RandomColor;
  169.  
  170.   if Random(100)<5 then
  171.      Chart1.Gradient.Direction:=TGradientDirection(Random(Ord(High(TGradientDirection))));
  172.  
  173.   { Random change Surface and Chart colors }
  174.   With SurfaceSeries1 do
  175.   Begin
  176.     Case RGColorMode.ItemIndex of
  177.       0: if Random(100)<15 then SeriesColor:=RandomColor; { single color }
  178.       1: begin { color range }
  179.            if Random(100)<15 then
  180.               StartColor:=RandomColor
  181.            else
  182.            if Random(100)<15 then
  183.               EndColor:=RandomColor;
  184.          end;
  185.     end;
  186.     { random change pen color }
  187.     if Random(100)<15 then Pen.Color:=RandomColor;
  188.   end;
  189.  
  190.   { Change Surface Example: }
  191.   With SpinEdit1 do if Value<MaxValue then Value:=Value+1
  192.                                       else Value:=MinValue;
  193.   Timer1.Enabled:=True;  { re-start animation timer }
  194. end;
  195.  
  196. procedure TSurfaceForm.RGColorModeClick(Sender: TObject);
  197. begin
  198.   Case RGColorMode.ItemIndex of
  199.     0: Begin
  200.          SurfaceSeries1.UseColorRange:=False;
  201.          SurfaceSeries1.UsePalette:=False;
  202.        end;
  203.     1:  Begin
  204.          SurfaceSeries1.UseColorRange:=True;
  205.          SurfaceSeries1.UsePalette:=False;
  206.        end;
  207.     2: Begin
  208.          SurfaceSeries1.UseColorRange:=False;
  209.          SurfaceSeries1.UsePalette:=True;
  210.        end;
  211.   end;
  212.   SpinEditRed.Enabled:=RGColorMode.ItemIndex=2;
  213.   SpinEditGreen.Enabled:=RGColorMode.ItemIndex=2;
  214.   SpinEditBlue.Enabled:=RGColorMode.ItemIndex=2;
  215. end;
  216.  
  217. procedure TSurfaceForm.SpinEditBlueChange(Sender: TObject);
  218. begin
  219.   With SurfaceSeries1 do
  220.   Begin
  221.     RedFactor:=SpinEditRed.Value*(2.0/100.0);
  222.     GreenFactor:=SpinEditGreen.Value*0.01;
  223.     BlueFactor:=SpinEditBlue.Value*0.01;
  224.     CreateDefaultPalette(PaletteSteps);
  225.     Repaint;
  226.   end;
  227. end;
  228.  
  229. procedure TSurfaceForm.CheckBox3Click(Sender: TObject);
  230. begin
  231.   Chart1.Gradient.Visible:=Chart1.IsScreenHighColor and (not CheckBox3.Checked);
  232.   Chart1.View3DWalls:=not CheckBox3.Checked;
  233.   Chart1.AxisVisible:=not CheckBox3.Checked;
  234.   if CheckBox3.Checked then
  235.      Chart1.Color:=clBlack
  236.   else
  237.      Chart1.Color:=clScrollBar;
  238. end;
  239.  
  240. procedure TSurfaceForm.BitBtn1Click(Sender: TObject);
  241. begin
  242.   EditChart(Self,Chart1);
  243. end;
  244.  
  245. procedure TSurfaceForm.BitBtn2Click(Sender: TObject);
  246. begin
  247.   EditSeries(Self,SurfaceSeries1);
  248. end;
  249.  
  250. end.
  251.