~
BEGIN OPEN JaM;
VEC: TYPE ~ Vector2.VEC;
PopVec:
PROC [self: State]
RETURNS [
VEC] ~ {
y: REAL ~ PopReal[self]; x: REAL ~ PopReal[self];
RETURN[[x, y]];
};
PushVec:
PROC [self: State, v:
VEC] ~ {
PushReal[self, v.x]; PushReal[self, v.y];
};
ApplyV2Add:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushVec[self, Vector2.Add[v1, v2]]; -- Sum, v1+v2.
};
ApplyV2Sub:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushVec[self, Vector2.Sub[v1, v2]]; -- Difference, v1-v2.
};
ApplyV2Neg:
PUBLIC
PROC [self: State] ~ {
v1: VEC = PopVec[self];
PushVec[self, Vector2.Neg[v1]]; -- Negative, -v.
};
ApplyV2Mul:
PUBLIC
PROC [self: State] ~ {
v1: VEC = PopVec[self]; s1: REAL = PopReal[self];
PushVec[self, Vector2.Mul[v1, s1]]; -- Multiplication by a scalar, sv.
};
ApplyV2Div:
PUBLIC
PROC [self: State] ~ {
v1: VEC = PopVec[self]; s1: REAL = PopReal[self];
PushVec[self, Vector2.Div[v1, s1]]; -- Division by a scalar, v/s.
};
ApplyV2MulC:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushVec[self, Vector2.MulC[v1, v2]]; -- Componentwise multiplication.
};
ApplyV2DivC:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushVec[self, Vector2.DivC[v1, v2]]; -- Componentwise division.
};
ApplyV2Dot:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushReal[self, Vector2.Dot[v1, v2]]; -- Dot (inner) product, v1"v2 = |v1| |v2| cos(theta).
};
ApplyV2Cross:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushReal[self, Vector2.Cross[v1, v2]]; -- Magnitude of cross (outer) product, |v1 v2| = |v1| |v2| sin(theta).
};
ApplyV2Square:
PUBLIC
PROC [self: State] ~ {
v1: VEC = PopVec[self];
PushReal[self, Vector2.Square[v1]]; -- Square, v"v = |v||v|.
};
ApplyV2Length:
PUBLIC
PROC [self: State] ~ {
v1: VEC = PopVec[self];
PushReal[self, Vector2.Length[v1]]; -- Length (magnitude), |v| = SqRt[v"v].
};
ApplyV2Unit:
PUBLIC
PROC [self: State] ~ {
v1: VEC = PopVec[self];
PushVec[self, Vector2.Unit[v1]]; -- Unit vector, v/|v|.
};
ApplyV2Exchange:
PUBLIC
PROC [self: State] ~ {
v2: VEC = PopVec[self]; v1: VEC = PopVec[self];
PushVec[self, v2]; PushVec[self, v1];
};
RegisterVector2:
PUBLIC
PROC [self: State] ~ {
Register[self, ".v2add", ApplyV2Add];
Register[self, ".v2sub", ApplyV2Sub];
Register[self, ".v2neg", ApplyV2Neg];
Register[self, ".v2mul", ApplyV2Mul];
Register[self, ".v2div", ApplyV2Div];
Register[self, ".v2mulc", ApplyV2MulC];
Register[self, ".v2divc", ApplyV2DivC];
Register[self, ".v2dot", ApplyV2Dot];
Register[self, ".v2cross", ApplyV2Cross];
Register[self, ".v2square", ApplyV2Square];
Register[self, ".v2length", ApplyV2Length];
Register[self, ".v2unit", ApplyV2Unit];
Register[self, ".v2exch", ApplyV2Exchange];
};