const type: vector3d is new struct
var float: x is 0.0;
var float: y is 0.0;
var float: z is 0.0;
end struct;
const func vector3d: vector3d (in float: x, in float: y, in float: z) is func
result
var vector3d: vect is vector3d.value;
begin
vect.x := x;
vect.y := y;
vect.z := z;
end func;
const func boolean: (in vector3d: a) = (in vector3d: b) is
return a.x = b.x and a.y = b.y and a.z = b.z;
const func boolean: (in vector3d: a) <> (in vector3d: b) is
return a.x <> b.x or a.y <> b.y or a.z <> b.z;
const func vector3d: - (in vector3d: a) is func
result
var vector3d: negated is vector3d.value;
begin
negated.x := -a.x;
negated.y := -a.y;
negated.z := -a.z;
end func;
const func vector3d: (in vector3d: a) + (in vector3d: b) is func
result
var vector3d: sum is vector3d.value;
begin
sum.x := a.x + b.x;
sum.y := a.y + b.y;
sum.z := a.z + b.z;
end func;
const func vector3d: (in vector3d: a) - (in vector3d: b) is func
result
var vector3d: difference is vector3d.value;
begin
difference.x := a.x - b.x;
difference.y := a.y - b.y;
difference.z := a.z - b.z;
end func;
const func vector3d: (in vector3d: v) * (in float: num) is func
result
var vector3d: product is vector3d.value;
begin
product.x := v.x * num;
product.y := v.y * num;
product.z := v.z * num;
end func;
const func vector3d: (in vector3d: v) / (in float: num) is func
result
var vector3d: quotient is vector3d.value;
begin
quotient.x := v.x / num;
quotient.y := v.y / num;
quotient.z := v.z / num;
end func;
const proc: (inout vector3d: vect) +:= (in float: delta) is func
begin
vect.x +:= delta;
vect.y +:= delta;
vect.z +:= delta;
end func;
const proc: (inout vector3d: vect) -:= (in float: delta) is func
begin
vect.x -:= delta;
vect.y -:= delta;
vect.z -:= delta;
end func;
const proc: (inout vector3d: vect) *:= (in float: number) is func
begin
vect.x *:= number;
vect.y *:= number;
vect.z *:= number;
end func;
const proc: (inout vector3d: vect) /:= (in float: number) is func
begin
vect.x /:= number;
vect.y /:= number;
vect.z /:= number;
end func;
const func float: abs (in vector3d: v) is
return sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);
const func float: sqrAbs (in vector3d: v) is
return v.x ** 2 + v.y ** 2 + v.z ** 2;
const func float: dot (in vector3d: a, in vector3d: b) is
return a.x * b.x + a.y * b.y + a.z * b.z;
const func vector3d: cross (in vector3d: a, in vector3d: b) is func
result
var vector3d: product is vector3d.value;
begin
product.x := a.y * b.z - a.z * b.y;
product.y := a.z * b.x - a.x * b.z;
product.z := a.x * b.y - a.y * b.x;
end func;
const func vector3d: reflect (in vector3d: vect, in vector3d: normal) is
return vect - normal * dot(normal, vect) * 2.0;
const func vector3d: unitVector (in vector3d: v) is func
result
var vector3d: unitVector is vector3d.value;
local
var float: length is 0.0;
begin
length := abs(v);
unitVector.x := v.x / length;
unitVector.y := v.y / length;
unitVector.z := v.z / length;
end func;
const func integer: compare (in vector3d: vect1, in vector3d: vect2) is func
result
var integer: signumValue is 0;
begin
signumValue := compare(vect1.x, vect2.x);
if signumValue = 0 then
signumValue := compare(vect1.y, vect2.y);
if signumValue = 0 then
signumValue := compare(vect1.z, vect2.z);
end if;
end if;
end func;
const func integer: hashCode (in vector3d: vect) is
return hashCode(vect.x) mod 16#40000000 +
hashCode(vect.y) mod 16#40000000 +
hashCode(vect.z) mod 16#40000000;
const func string: str (in vector3d: a) is
return "(" <& a.x digits 2 <& ", " <& a.y digits 2 <& ", " <& a.z digits 2 <& ")";
enable_output(vector3d);