PDA

View Full Version : Type TRGBVector and mathemathic for him



CynicRus
07-03-2012, 02:19 PM
type T2DVector = record
X: double;
Y: double;
end;
type T2DVectorArray = array of T2DVector;

function Lenght2DVector(V: T2DVector):double;
begin
result:=sqrt(V.X*V.X+V.Y*V.Y);
end;

function Add2DVector(v1,v2:T2DVector):T2DVector;
begin
result.X:=v1.X+v2.X;
result.Y:=v1.Y+v2.Y;
end;

function Sub2DVector(v1,v2:T2DVector):T2DVector;
begin
result.X:=v1.X-v2.X;
result.Y:=v1.Y-v2.Y;
end;

function Mul2DVector(v:T2DVector;d:double):T2DVector;
begin
result.X:=V.X*d;
result.Y:=V.Y*d;
end;

function Mul2DVectorEx(v1,v2: T2DVector):T2DVector;
begin
result.X:=v1.X*v2.X;
result.Y:=v1.Y*v2.Y;
end;

function Div2DVector(v:T2DVector;d: double): T2DVector;
begin
result.X:=v.X/d;
result.Y:=v.Y/d;
end;

function Div2DVectorEx(v,v1: T2DVector):T2DVector;
begin
result.X:=v.X/v1.X;
result.Y:=v.Y/v1.Y;
end;

function Zero:T2DVector;
begin
result.X:=0;
result.Y:=0;
end;

function Angle2DVector(V:T2DVector):double;
var
d:double;
begin
result:=0;
d:=Lenght2DVector(V);
if d=0 then result:=0 else begin
if (V.X>=0) and (V.Y>=0) then result:=arcCos(V.X/d);
if (V.X<0) and (V.Y>=0) then result:=arcCos(V.Y/d)+pi/2;
if (V.X<0) and (V.Y<0) then result:=arcCos(abs(V.X)/d)+pi;
if (V.X>=0) and (V.Y<0) then result:=arcCos(abs(V.Y)/d)+pi/2*3;
end;
end;

function Dist2DVector(const v1,v2:T2DVector):double;
var dx : double;
dy : double;
begin
dx := v1.X-v2.X;
dy := v1.Y-v2.Y;
Result := Sqrt(dx*dx+dy*dy);
end;

function Rotate2DVector(vec:T2DVector; angle:double):T2DVector;
var d,d1:double;
begin
d:=sin(angle);
d1:=cos(angle);
result.x:=+vec.x*d1+vec.y*d;
result.y:=-vec.x*d+vec.y*d1;
end;

function Magnitude(v:T2DVector):double;
begin
result:=Sqr((v.X*v.X)+(v.Y*v.Y));
end;

function Normalize(v:T2DVector):T2DVector;
var
m : double;
begin
m:=Magnitude(v);
if m = 0 then
begin
v.X:=1;
v.Y:=0;
end
else
begin
v.X:=v.X/m;
v.Y :=v.Y/m;
end;
result.X:=v.X;
result.Y:=v.Y;
end;

function EqualVector2D(v,v1: T2DVector):boolean;
begin
result:=(v.X=v1.X) and (v.Y=v1.Y);
end;

function Perpendicular(v: T2DVector): T2DVector;
begin
result.X:=-v.Y;
result.Y:=v.X;
end;

function Reflect(v: T2DVector): T2DVector;
begin
result.X:=-v.X;
result.Y:=-v.Y;
end;

function Create2DVector(X,Y:double):T2DVector;
begin
result.X:=X;
result.Y:=Y;
end;

function Create2DVectorEx(P: TPoint):T2DVector;
begin
result.X:=p.x;
result.Y:=p.y;
end;

function TPAToT2DA(TPA: TPointArray):T2DVectorArray;
var
va: T2DVectorArray;
v: T2DVector;
i: integer;
begin
for i:=0 to length(TPA)-1 do begin
v:=Create2DVectorEx(TPA[i]);
va[i]:=v;
end;
result:=va;
end;

function Direction(From,tTo: double): T2DVector;
begin
result:=Normalize(Create2DVector(From, tTo));
end;

Ezio Auditore da Firenze
07-03-2012, 02:44 PM
Can I ask what this does exactly?

Brandon
07-03-2012, 02:46 PM
It's a class for math. Basically vector projection and shifting along an axis or point. More functions in it would be cool.
This just calculates magnitude, cross, dot and normalization of a vector. Can prob make Gauss Jordan matrices from these and some other useful applications I guess.

Ezio Auditore da Firenze
07-03-2012, 02:48 PM
It's a class for math. Basically vector projection and shifting along an axis or point. More functions in it would be cool.
This just calculates magnitude, cross, dot and normalization of a vector. Can prob make Gauss Jordan matrices from these and some other useful applications I guess.

Ah okay, that's pretty neat! Thanks for letting me know :)

CynicRus
07-03-2012, 06:50 PM
Added new features-) Sorry, no description of that - will be added later. Description and examples of work will be added later.

CynicRus
07-05-2012, 08:04 AM
I decided to convert a vector in 2D, this class is to work with him.

masterBB
07-05-2012, 08:57 AM
Created a LAPE version of your code using type functions:
type
T2DVector = record
X: double;
Y: double;
end;

type
T2DVectorArray = array of T2DVector;

function T2DVector.Length(): double; //th dude, not ht
begin
result := sqrt(X * X + Y * Y);
end;

procedure T2DVector.Add(vector: T2DVector);
begin
X := X + vector.X;
Y := Y + vector.Y;
end;

procedure T2DVector.Sub(vector: T2DVector);
begin
X := X - vector.X;
Y := Y - vector.Y;
end;

procedure T2DVector.Multi(multiplier: Double); //don't name parameters to their types
begin
X := X * multiplier;
Y := Y * multiplier;
end;

procedure T2DVector.Multi(vector:T2DVector); overload;
begin
X := X * vector.X;
Y := Y * vector.Y;
end;

procedure T2DVector.Divide(d: Double);
begin
X := X / d;
Y := Y / d;
end;

procedure T2DVector.Divide(vector:T2DVector); overload;
begin
X := X / vector.X;
Y := Y / vector.Y;
end;

procedure T2DVector.Zero;
begin
X := 0;
Y := 0;
end;

function T2DVector.Angle: double;
var
l: double;
begin
result := 0;
l := Length;
if l = 0 then
result := 0
else
begin
if (X >= 0) and (Y >= 0) then
result := arcCos(X / l);
if (X < 0) and (Y >= 0) then
result := arcCos(Y / l) + pi / 2;
if (X < 0) and (Y < 0) then
result := arcCos(abs(X) / l) + pi;
if (X >= 0) and (Y < 0) then
result := arcCos(abs(Y) / l) + pi / 2 * 3;
end;
end;

function T2DVector.Dist(vector:T2DVector): double;
var
dx: double;
dy: double;
begin
dx := X - vector.X;
dy := Y - vector.Y;
Result := Sqrt(dx * dx + dy * dy);
end;

procedure T2DVector.Rotate(angle: double);
var
d, d1: double;
oldVector:T2DVector;
begin
d := sin(angle);
d1 := cos(angle);
oldVector := Self;
x := + oldVector.x * d1 + oldVector.y * d;
y := - oldVector.x * d + oldVector.y * d1;
end;

function T2DVector.Magnitude: double;
begin
result := Sqr((X * X) + (Y * Y));
end;

procedure T2DVector.Normalize;
var
m: double;
begin
m := Self.Magnitude;
if m = 0 then
Self := [1,0] //you can set a type in one line like this
else
Self.Divide(m);
end;

function T2DVector.Equals(v, vector: T2DVector): boolean; //example: if vectorA.Equals(vectorB) then
begin
result := (X = vector.X) and (Y = vector.Y);
end;

procedure T2DVector.Perpendicular;
var
oX: Double;
begin
oX := X;
X := - Y;
Y := oX;
end;

procedure T2DVector.Reflect;
begin
X := - X;
Y := - Y;
end;

function Create2DVector(X, Y: double): T2DVector;
begin
result.X := X;
result.Y := Y;
end;

function Create2DVector(P: TPoint): T2DVector; overload;
begin
result.X := p.x;
result.Y := p.y;
end;

function TPAToT2DA(TPA: TPointArray): T2DVectorArray;
var
va: T2DVectorArray;
v: T2DVector;
i: integer;
begin
for i := 0 to length(TPA) - 1 do
begin
v := Create2DVector(TPA[i]);
va[i] := v;
end;
result := va;
end;

procedure T2DVector.Direction(From, tTo: double);
begin
Self := Create2DVector(From, tTo);
Self.Normalize;
end;

I formated it for you with nielsie his formatter:
type
T2DVector = record
X: double;
Y: double;
end;

type
T2DVectorArray = array of T2DVector;

function Lenght2DVector(V: T2DVector): double;
begin
result := sqrt(V.X * V.X + V.Y * V.Y);
end;

function Add2DVector(v1, v2: T2DVector): T2DVector;
begin
result.X := v1.X + v2.X;
result.Y := v1.Y + v2.Y;
end;

function Sub2DVector(v1, v2: T2DVector): T2DVector;
begin
result.X := v1.X - v2.X;
result.Y := v1.Y - v2.Y;
end;

function Mul2DVector(v: T2DVector; d: double): T2DVector;
begin
result.X := V.X * d;
result.Y := V.Y * d;
end;

function Mul2DVectorEx(v1, v2: T2DVector): T2DVector;
begin
result.X := v1.X * v2.X;
result.Y := v1.Y * v2.Y;
end;

function Div2DVector(v: T2DVector; d: double): T2DVector;
begin
result.X := v.X / d;
result.Y := v.Y / d;
end;

function Div2DVectorEx(v, v1: T2DVector): T2DVector;
begin
result.X := v.X / v1.X;
result.Y := v.Y / v1.Y;
end;

function Zero: T2DVector;
begin
result.X := 0;
result.Y := 0;
end;

function Angle2DVector(V: T2DVector): double;
var
d: double;
begin
result := 0;
d := Lenght2DVector(V);
if d = 0 then
result := 0
else
begin
if (V.X >= 0) and (V.Y >= 0) then
result := arcCos(V.X / d);
if (V.X < 0) and (V.Y >= 0) then
result := arcCos(V.Y / d) + pi / 2;
if (V.X < 0) and (V.Y < 0) then
result := arcCos(abs(V.X) / d) + pi;
if (V.X >= 0) and (V.Y < 0) then
result := arcCos(abs(V.Y) / d) + pi / 2 * 3;
end;
end;

function Dist2DVector(const v1, v2: T2DVector): double;
var
dx: double;
dy: double;
begin
dx := v1.X - v2.X;
dy := v1.Y - v2.Y;
Result := Sqrt(dx * dx + dy * dy);
end;

function Rotate2DVector(vec: T2DVector; angle: double): T2DVector;
var
d, d1: double;
begin
d := sin(angle);
d1 := cos(angle);
result.x := + vec.x * d1 + vec.y * d;
result.y := - vec.x * d + vec.y * d1;
end;

function Magnitude(v: T2DVector): double;
begin
result := Sqr((v.X * v.X) + (v.Y * v.Y));
end;

function Normalize(v: T2DVector): T2DVector;
var
m: double;
begin
m := Magnitude(v);
if m = 0 then
begin
v.X := 1;
v.Y := 0;
end
else
begin
v.X := v.X / m;
v.Y := v.Y / m;
end;
result.X := v.X;
result.Y := v.Y;
end;

function EqualVector2D(v, v1: T2DVector): boolean;
begin
result := (v.X = v1.X) and (v.Y = v1.Y);
end;

function Perpendicular(v: T2DVector): T2DVector;
begin
result.X := - v.Y;
result.Y := v.X;
end;

function Reflect(v: T2DVector): T2DVector;
begin
result.X := - v.X;
result.Y := - v.Y;
end;

function Create2DVector(X, Y: double): T2DVector;
begin
result.X := X;
result.Y := Y;
end;

function Create2DVectorEx(P: TPoint): T2DVector;
begin
result.X := p.x;
result.Y := p.y;
end;

function TPAToT2DA(TPA: TPointArray): T2DVectorArray;
var
va: T2DVectorArray;
v: T2DVector;
i: integer;
begin
for i := 0 to length(TPA) - 1 do
begin
v := Create2DVectorEx(TPA[i]);
va[i] := v;
end;
result := va;
end;

function Direction(From, tTo: double): T2DVector;
begin
result := Normalize(Create2DVector(From, tTo));
end;