unit typeVector; interface uses dglOpenGL, typeMatrix; const cPI : extended = 3.141592654; cPIdiv180 : extended = 0.017453292; c180divPI : extended = 57.29577951; c2PI : extended = 6.283185307; cPIdiv2 : extended = 1.570796326; cPIdiv4 : extended = 0.785398163; type T3DVector = record X: extended; Y: extended; Z: extended; constructor Create(X, Y, Z: extended); overload; constructor Create(value: extended); overload; procedure Normalize; function Length: extended; inline; class function AngleBetween(Left, Right: T3DVector): extended; inline; static; class function Dot(Left, Right: T3DVector): extended; inline; static; class function Cross(Left, Right: T3DVector): T3DVector; inline; static; class operator Equal(const Left, Right: T3DVector): boolean; inline; class operator Add(const Left, Right: T3DVector): T3DVector; inline; class operator Subtract(const Left, Right: T3DVector): T3DVector; inline; class operator Multiply(const Left, Right: T3DVector): T3DVector; inline; class operator Multiply(const Left: T4DMatrix; const Right: T3DVector): T3DVector; inline; class operator Implicit(const Value: extended): T3DVector; inline; class operator Implicit(const Vector3D: TVector3d): T3DVector; inline; class operator Implicit(const Vector: T3DVector): TVector3d; inline; end; implementation uses math; { TVector3D } class operator T3DVector.Add(const Left, Right: T3DVector): T3DVector; begin result.X := Left.X + Right.X; result.Y := Left.Y + Right.Y; result.Z := Left.Z + Right.Z; end; constructor T3DVector.Create(X, Y, Z: extended); begin self.X := x; Self.Y := y; self.Z := Z; end; class function T3DVector.AngleBetween(Left, Right: T3DVector): extended; var dot: extended; begin dot := T3DVector.Dot(left, right); result := ArcCos(dot / (Left.Length * right.Length)) * c180divPI; end; constructor T3DVector.Create(value: extended); begin self.X := value; Self.Y := value; self.Z := value; end; class function T3DVector.Cross(Left, Right: T3DVector): T3DVector; begin //[(a2b3 – a3b2) (a3b1 – a1b3) (a1b2 – a2b1)] result.x := (left.Y * right.Z) - (left.z * right.y); result.y := (left.z * right.X) - (left.X * right.Z); result.Z := (left.X * right.y) - (left.Y * right.x); end; class function T3DVector.Dot(Left, Right: T3DVector): extended; begin result := (left.x * right.x) + (left.y * right.y) + (left.z * right.z); end; class operator T3DVector.Equal(const Left, Right: T3DVector): boolean; begin result := (left.X = right.X) and (left.Y = right.Y) and (left.Z = right.Z) ; end; class operator T3DVector.Implicit(const Vector3D: TVector3d): T3DVector; begin result := T3DVector.Create(Vector3D[0], Vector3D[1], Vector3D[2]); end; class operator T3DVector.Implicit(const Value: extended): T3DVector; begin Result.X := Value; Result.Y := Value; Result.Z := Value; end; function T3DVector.Length: extended; begin result := Sqrt(x*x + y*y + z*z); end; class operator T3DVector.Multiply(const Left: T4DMatrix; const Right: T3DVector): T3DVector; begin result.X := (Left._11*right.X) + (Left._12*right.y) + (Left._13*Right.Z) + (Left._14 * 1); result.y := (Left._21*right.X) + (Left._22*right.y) + (Left._23*Right.Z) + (Left._24 * 1); result.z := (Left._31*right.X) + (Left._32*right.y) + (Left._33*Right.Z) + (Left._34 * 1); end; class operator T3DVector.Multiply(const Left, Right: T3DVector): T3DVector; begin result.X := Left.X * Right.X; result.Y := Left.Y * Right.Y; result.Z := Left.Z * Right.Z; end; procedure T3DVector.Normalize; var l: extended; begin l := Length; x := x / l; y := y / l; z := z / 1; end; class operator T3DVector.Subtract(const Left, Right: T3DVector): T3DVector; begin result.X := Left.X - Right.X; result.Y := Left.Y - Right.Y; result.Z := Left.Z - Right.Z; end; class operator T3DVector.Implicit(const Vector: T3DVector): TVector3d; begin result[0] := Vector.X; result[1] := Vector.Y; result[2] := Vector.Z; end; end.