Vector: TYPE = SV3d.Vector;
Vector2d: TYPE = SVVector2d.Vector2d;
Add:
PUBLIC
PROC [v1: Vector, v2: Vector]
RETURNS [sumV: Vector] = {
sumV[1] ← v1[1] + v2[1];
sumV[2] ← v1[2] + v2[2];
sumV[3] ← v1[3] + v2[3];
};
Difference:
PUBLIC
PROC [v1: Vector, v2: Vector]
RETURNS [v1Minusv2: Vector] = {
v1Minusv2[1] ← v1[1] - v2[1];
v1Minusv2[2] ← v1[2] - v2[2];
v1Minusv2[3] ← v1[3] - v2[3];
};
Scale:
PUBLIC
PROC [v: Vector, scalar:
REAL]
RETURNS [scaledV: Vector] = {
scaledV[1] ← v[1]*scalar; scaledV[2] ← v[2]*scalar; scaledV[3] ← v[3]*scalar;
};
Normalize:
PUBLIC
PROC [v: Vector]
RETURNS [normV: Vector] = {
Returns the unit vector with the same direction as v.
mag: REAL ← Magnitude[v];
normV[1] ← v[1]/mag;
normV[2] ← v[2]/mag;
normV[3] ← v[3]/mag;
};
Negate:
PUBLIC
PROC [v: Vector]
RETURNS [negV: Vector] = {
negV[1] ← -v[1]; negV[2] ← -v[2]; negV[3] ← -v[3];
};
DotProduct:
PUBLIC
PROC [v1: Vector, v2: Vector]
RETURNS [scalar:
REAL] = {
scalar ← v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3];
};
CrossProduct:
PUBLIC
PROC [v1: Vector, v2: Vector]
RETURNS [prodV: Vector] = {
| i j k |
| v1x v1y v1z |=(v1y*v2z - v1z*v2y) i + (v1z*v2x - v1x*v2z) j
| v2x v2y v2z |(v1x*v2y - v1y*v2x) k
prodV[1] ← v1[2]*v2[3] - v1[3]*v2[2];
prodV[2] ← v1[3]*v2[1] - v1[1]*v2[3];
prodV[3] ← v1[1]*v2[2] - v1[2]*v2[1];
};
Magnitude:
PUBLIC
PROC [v: Vector]
RETURNS [magnitude:
REAL] = {
magnitude ← RealFns.SqRt[(v[1]*v[1]+v[2]*v[2]+v[3]*v[3])];
};
Parallel:
PUBLIC
PROC [v1, v2: Vector]
RETURNS [
BOOL] = {
a,b,c: REAL;
IF v2[1] = 0 THEN { IF v1[1] # 0 THEN RETURN[FALSE] ELSE a ← 0}
ELSE {IF v1[1] = 0 THEN IF v2[1] # 0 THEN RETURN[FALSE] ELSE a ← v1[1]/v2[1]};
IF v2[2] = 0 THEN { IF v1[2] # 0 THEN RETURN[FALSE] ELSE b ← 0}
ELSE {IF v1[2] = 0 THEN IF v2[2] # 0 THEN RETURN[FALSE] ELSE b ← v1[2]/v2[2]};
IF v2[3] = 0 THEN { IF v1[3] # 0 THEN RETURN[FALSE] ELSE c ← 0}
ELSE {IF v1[3] = 0 THEN IF v2[3] # 0 THEN RETURN[FALSE] ELSE c ← v1[3]/v2[3]};
RETURN[AllEqual[a, b, c]];
};
AllEqual:
PRIVATE
PROC [a, b, c:
REAL]
RETURNS [
BOOL] = {
IF a = 0
THEN {
IF b = 0 THEN RETURN[TRUE]-- both vectors have only a z component
ELSE IF c = 0 THEN RETURN[TRUE] -- both vectors have only a y component
ELSE RETURN[b=c];
}
ELSE {
IF b = 0
THEN {
IF c = 0 THEN RETURN[TRUE]-- both vectors have only an x component
ELSE RETURN[a=c];
}
ELSE {
IF c = 0 THEN RETURN[a=b]
ELSE RETURN[a=b AND b = c];
};
};
}; -- end of AllEqual
Perpendicular:
PUBLIC
PROC [v1, v2: Vector]
RETURNS [
BOOL] = {
RETURN[DotProduct[v1, v2] = 0];
};
Vector2DAsXYVector:
PUBLIC
PROC [vXY: Vector2d]
RETURNS [vZeroZ: Vector] = {
vZeroZ[1] ← vXY[1];
vZeroZ[2] ← vXY[2];
vZeroZ[3] ← 0;
};
Vector2DAsYZVector:
PUBLIC
PROC [vYZ: Vector2d]
RETURNS [vZeroX: Vector] = {
vZeroX[1] ← 0;
vZeroX[2] ← vYZ[1];
vZeroX[3] ← vYZ[2];
};
Vector2DAsZXVector:
PUBLIC
PROC [vZX: Vector2d]
RETURNS [vZeroY: Vector] = {
vZeroY[1] ← vZX[2];
vZeroY[2] ← 0;
vZeroY[3] ← vZX[1];
};
ProjectOntoXYPlane:
PUBLIC
PROC [v: Vector]
RETURNS [v2d: Vector2d] = {
v2d[1] ← v[1];
v2d[2] ← v[2];
};
ProjectOntoYZPlane:
PUBLIC
PROC [v: Vector]
RETURNS [v2d: Vector2d] = {
v2d[1] ← v[2];
v2d[2] ← v[3];
};
ProjectOntoZXPlane:
PUBLIC
PROC [v: Vector]
RETURNS [v2d: Vector2d] = {
v2d[1] ← v[3];
v2d[2] ← v[1];
};