Unit bezier;
{ Bezier curves ; curvas de Bezier }
{ Implemented by Joao Paulo Schwarz Schuler }
{ http://www.schulers.com/jpss }
{ Turbo Pascal Unit}
{ Delphi Unit }
INTERFACE
type TPOS = longint;
T3DPIX = object
X,Y,Z:TPOS;
procedure Def(PX,PY,PZ:TPOS); {define}
procedure Get(var PX,PY,PZ:TPOS);
end;
T4X4LI = array[1..4,1..4] of longint;
type TGEOMETRIC = object { matriz de pontos; points matrix }
P:array [1..4] of T3DPIX;
procedure Def(P1,P2,P3,P4:T3DPIX); {define}
procedure Get(var P1,P2,P3,P4:T3DPIX);
end;
type T4X4POS = object
P:T4X4LI;
procedure Def(P11,P12,P13,P14, {define}
P21,P22,P23,P24,
P31,P32,P33,P34,
P41,P42,P43,P44:TPOS);
procedure Get(var P11,P12,P13,P14,
P21,P22,P23,P24,
P31,P32,P33,P34,
P41,P42,P43,P44:TPOS);
procedure DefBezier; {define matriz de Bezier;
{define Bezier Matrix}
end;
type TCURVA = object
M:T4X4POS;
G:TGEOMETRIC;
CX,CY,CZ:array[1..4] of TPOS; {coeficientes;Coefficients}
procedure CalcCoef; { calcula coeficientes
Calculate Coeffitients }
{ Calcula linha ; calculate line }
function CalcX(t:extended):extended;
function CalcY(t:extended):extended;
function CalcZ(t:extended):extended;
end;
IMPLEMENTATION
procedure T3DPIX.Def(PX,PY,PZ:TPOS);
begin
X:=PX;
Y:=PY;
Z:=PZ;
end;
procedure T3DPIX.Get(var PX,PY,PZ:TPOS);
begin
PX:=X;
PY:=Y;
PZ:=Z;
end;
procedure TGEOMETRIC.Def(P1,P2,P3,P4:T3DPIX);
begin
P[1]:=P1;
P[2]:=P2;
P[3]:=P3;
P[4]:=P4;
end;
procedure TGEOMETRIC.Get(var P1,P2,P3,P4:T3DPIX);
begin
P1:=P[1];
P2:=P[2];
P3:=P[3];
P4:=P[4];
end;
procedure T4X4POS.Def(P11,P12,P13,P14,
P21,P22,P23,P24,
P31,P32,P33,P34,
P41,P42,P43,P44:TPOS);
begin
P[1,1]:=P11;P[1,2]:=P12;P[1,3]:=P13;P[1,4]:=P14;
P[2,1]:=P21;P[2,2]:=P22;P[2,3]:=P23;P[2,4]:=P24;
P[3,1]:=P31;P[3,2]:=P32;P[3,3]:=P33;P[3,4]:=P34;
P[4,1]:=P41;P[4,2]:=P42;P[4,3]:=P43;P[4,4]:=P44;
end;
procedure T4X4POS.Get(var P11,P12,P13,P14,
P21,P22,P23,P24,
P31,P32,P33,P34,
P41,P42,P43,P44:TPOS);
begin
P11:=P[1,1];P12:=P[1,2];P13:=P[1,3];P14:=P[1,4];
P21:=P[2,1];P22:=P[2,2];P23:=P[2,3];P24:=P[2,4];
P31:=P[3,1];P32:=P[3,2];P33:=P[3,3];P34:=P[3,4];
P41:=P[4,1];P42:=P[4,2];P43:=P[4,3];P44:=P[4,4];
end;
procedure T4X4POS.DefBezier;
{define matriz de Bezier;
{define Bezier Matrix}
begin
Def(-1 ,3 ,-3 ,1,
3 ,-6 ,3 ,0,
-3 ,3 ,0 ,0,
1 ,0 ,0 ,0);
end;
procedure TCURVA.CalcCoef;
{calculate coefficients; calcula coeficientes}
var I,J:word;
begin
for I:=1 to 4 do
begin CX:=0;CY:=0;CZ:=0;end;
for J:=1 to 4 do
for I:=1 to 4 do
begin
CX[J]:=CX[J]+M.P[J,I]*G.P.X;
CY[J]:=CY[J]+M.P[J,I]*G.P.Y;
CZ[J]:=CZ[J]+M.P[J,I]*G.P.Z;
end;
end;
function TCURVA.CalcX(t:extended):extended;
begin
CalcX:=((CX[1]*t+CX[2])*t+CX[3])*t+CX[4];
end;
function TCURVA.CalcY(t:extended):extended;
begin
CalcY:=((CY[1]*t+CY[2])*t+CY[3])*t+CY[4];
end;
function TCURVA.CalcZ(t:extended):extended;
begin
CalcZ:=((CZ[1]*t+CZ[2])*t+CZ[3])*t+CZ[4];
end;
end.
--------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses bezier;
procedure TForm1.Button1Click(Sender: TObject);
var L:TCURVA; {linha , Bezier curve}
P1,P2,P3,P4:T3DPIX;
I:integer;
C:byte;
begin
Randomize;
C:=Round(Random(256)); { cor ; color }
P1.Def(1,1,1); { ponto inicial ; initial point }
P2.Def(Round(Random(100)),Round(Random(100)),1);
P3.Def(Round(Random(200)),Round(Random(320)),1);
P4.Def(319,Round(Random(200)),1); { ponto final ; final point }
L.M.DefBezier;
L.G.Def(P1,P2,P3,P4);
L.CalcCoef;
for I:=0 to 1000 do { desenha ; draw}
Canvas.Pixels[Round(L.CalcY(I/1000)),Round(L.CalcX(I/1000))]:=C;
end;
end.