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;
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;