home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / TEEBEZIE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  5.0 KB  |  185 lines

  1. {**********************************************}
  2. {   TBezierSeries Component                    }
  3. {   Copyright (c) 1996-98 by David Berneda     }
  4. {**********************************************}
  5. {$I teedefs.inc}
  6. unit TeeBezie;
  7.  
  8. { This Series component is derived from PointLine Series.
  9.   It draws PolyBezier curves using every 3 points in the Series.
  10.   The first point in the Series determines the origin.
  11.  
  12.   The LinePen property controls the Bezier curve color, width and style.
  13.   The inherited Pointer property is used to draw the control points. }
  14. interface
  15.  
  16. uses
  17.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, TeeProcs,
  18.   Teengine, Chart, Series, TeCanvas;
  19.  
  20. Const MaxBezierPoints=8000;  { <-- maximum points per Series }
  21.  
  22. type
  23.   TBezierSeries = class(TCustomSeries)
  24.   private
  25.     { Private declarations }
  26.     FNumBezierPoints:Integer;
  27.     Procedure SetBezierPoints(Value:Integer);
  28.   protected
  29.     { Protected declarations }
  30.     Procedure SetSeriesColor(AColor:TColor); override;
  31.     procedure DrawAllValues; override;
  32.   public
  33.     { Public declarations }
  34.     Constructor Create(AOwner:TComponent); override;
  35.     Procedure PrepareForGallery(IsEnabled:Boolean); override;
  36.   published
  37.     { Published declarations }
  38.     property LinePen;
  39.     property NumBezierPoints:Integer read FNumBezierPoints
  40.                                      write SetBezierPoints default 32;
  41.     property Pointer;
  42.     property XValues;
  43.     property YValues;
  44.    { events }
  45.     property OnClickPointer;
  46.   end;
  47.  
  48. implementation
  49.  
  50. Uses TeeConst, TeeProco;
  51.  
  52. Constructor TBezierSeries.Create(AOwner:TComponent);
  53. begin
  54.   inherited Create(AOwner);
  55.   FNumBezierPoints:=32;
  56. end;
  57.  
  58. Procedure TBezierSeries.SetBezierPoints(Value:Integer);
  59. begin
  60.   if Value<2 then Raise ChartException.Create(TeeMsg_LowBezierPoints);
  61.   SetIntegerProperty(FNumBezierPoints,Value);
  62. end;
  63.  
  64. procedure TBezierSeries.DrawAllValues;
  65. type PBezierPoints = ^TBezierPoints;
  66.      TBezierPoints = array[0..0] of TPoint;
  67.      TBezierMaxPoints=Array[0..MaxBezierPoints-1] of TPoint;
  68.      PBezierMaxPoints=^TBezierMaxPoints;
  69. var tmpPoints : PBezierMaxPoints;
  70.     t         : Integer;
  71.     tt        : Integer;
  72.     tmpCount  : Integer;
  73.     tmpColor  : TColor;
  74.     mu        : Double;
  75.     mum1      : Double;
  76.     mum12     : Double;
  77.     mu2       : Double;
  78.     P         : TPoint;
  79.     P1        : TPoint;
  80.     P2        : TPoint;
  81.     P3        : TPoint;
  82. Begin
  83.   New(tmpPoints);
  84.   { Calculate XY coordinates... }
  85.   tmpCount:=MinLong(MaxBezierPoints,Count);
  86.   for t:=0 to tmpCount-1 do
  87.   begin
  88.     tmpPoints^[t].X:=CalcXPos(t);
  89.     tmpPoints^[t].Y:=CalcYPos(t);
  90.   end;
  91.   { Draw bezier line... }
  92.   With ParentChart,Canvas do
  93.   begin
  94.     AssignVisiblePen(LinePen);
  95.     Brush.Style:=bsClear;
  96.     if View3D then
  97.     begin
  98.       MoveTo3D(tmpPoints^[0].X,tmpPoints^[0].Y,StartZ);
  99.       for t:=1 to tmpCount div 3 do
  100.       begin
  101.         P1:=tmpPoints^[3*t-3];
  102.         P2:=tmpPoints^[3*t-2];
  103.         P3:=tmpPoints^[3*t-1];
  104.         for tt:=1 to FNumBezierPoints do
  105.         begin
  106.           mu:=tt/FNumBezierPoints;
  107.           mu2:=Sqr(mu);
  108.           mum1:=1-mu;
  109.           mum12:=Sqr(mum1);
  110.           p.x:=Round(p1.x * mum12 + 2*p2.x*mum1*mu + p3.x*mu2);
  111.           p.y:=Round(p1.y * mum12 + 2*p2.y*mum1*mu + p3.y*mu2);
  112.           LineTo3D(P.X,P.Y,StartZ);
  113.         end;
  114.       end;
  115.     end
  116.     else
  117.     begin
  118.       MoveTo(tmpPoints^[0].X,tmpPoints^[0].Y);
  119.       {$IFNDEF D1}
  120.       PolyBezierTo(Handle,PBezierPoints(@tmpPoints)^,(3*(tmpCount div 3)));
  121.       {$ELSE}
  122.       for t:=1 to tmpCount div 3 do
  123.       begin
  124.         P1:=tmpPoints^[3*t-3];
  125.         P2:=tmpPoints^[3*t-2];
  126.         P3:=tmpPoints^[3*t-1];
  127.         for tt:=1 to FNumBezierPoints do
  128.         begin
  129.           mu:=tt/FNumBezierPoints;
  130.           mu2:=Sqr(mu);
  131.           mum1:=1-mu;
  132.           mum12:=Sqr(mum1);
  133.           p.x:=Round(p1.x * mum12 + 2*p2.x*mum1*mu + p3.x*mu2);
  134.           p.y:=Round(p1.y * mum12 + 2*p2.y*mum1*mu + p3.y*mu2);
  135.           LineTo(P.X,P.Y);
  136.         end;
  137.       end;
  138.       {$ENDIF}
  139.     end;
  140.   end;
  141.   { Draw pointers... }
  142.   if Pointer.Visible then
  143.   for t:=0 to tmpCount-1 do
  144.   begin
  145.     tmpColor:=ValueColor[t];
  146.     With Pointer do
  147.     begin
  148.       PrepareCanvas(tmpColor);
  149.       Draw(tmpPoints^[t].X,tmpPoints^[t].Y,tmpColor,Style);
  150.     end;
  151.   end;
  152.   Dispose(tmpPoints);
  153. End;
  154.  
  155. Procedure TBezierSeries.SetSeriesColor(AColor:TColor);
  156. begin
  157.   inherited SetSeriesColor(AColor);
  158.   LinePen.Color:=AColor;
  159. end;
  160.  
  161. Procedure TBezierSeries.PrepareForGallery(IsEnabled:Boolean);
  162. Begin
  163.   inherited PrepareForGallery(IsEnabled);
  164.   FillSampleValues(3);
  165.   ColorEachPoint:=IsEnabled;
  166.   Pointer.Draw3D:=False;
  167. end;
  168.  
  169. { Un-register the Series }
  170. Procedure TeeBezierExitProc; far;
  171. begin
  172.   UnRegisterTeeSeries([TBezierSeries]);
  173. end;
  174.  
  175. { Register the Series at Chart gallery }
  176. initialization
  177.   RegisterTeeSeries( TBezierSeries, TeeMsg_GalleryBezier, TeeMsg_GalleryExtended, 1 );
  178. {$IFDEF D1}
  179.   AddExitProc(TeeBezierExitProc);
  180. {$ELSE}
  181. finalization
  182.   TeeBezierExitProc;
  183. {$ENDIF}
  184. end.
  185.