Source

vector3-io / vector3.io

# Make a 3d-vector class
Vector3 := Object clone;
Vector3 make := method(x, y, z,
                       result := Vector3 clone;
                       result x := x;
                       result y := y;
                       result z := z;
                       result
                      )

# Add dot product function
Vector3  := method(other,
                    other x * x +  
                    other y * y +  
                    other z * z
                   )

# Add cross product function
Vector3  := method(other,
                    Vector3 make (
                            y * other z - z * other y,
                            z * other x - x * other z,
                            x * other y - y * other x
                           )
                   )

# Make the functions infix operators in the language
# (2 is the same precedence level as *, / and %)
OperatorTable addOperator("⋅", 2)
OperatorTable addOperator("⨯", 2)

# Test it
a := Vector3 make (1, -2,  3);
b := Vector3 make (4,  5, -6);

dotproduct   := a  b
crossproduct := a  b

dotproduct   println
crossproduct println