Skip to content

Instantly share code, notes, and snippets.

@netguy204
Created September 16, 2012 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netguy204/3732276 to your computer and use it in GitHub Desktop.
Save netguy204/3732276 to your computer and use it in GitHub Desktop.
Vectors
deffun vector(x, y, z) { x: x, y: y, z: z } in
deffun sub(v1, v2)
vector( -(v1.x, v2.x), -(v1.y, v2.y), -(v1.z, v2.z))
in
deffun abs(x)
if <(x, 0) then -(0, x) else x
in
deffun mult(x, y)
defvar yi = 0 in
defvar r = 0 in {
for(yi = 0; <(yi, abs(y)); yi++) {
r = +(r, x);
};
abs(r);
}
in
deffun dotP(v1, v2)
+(mult(v1.x, v2.x), mult(v1.y, v2.y), mult(v1.z, v2.z));
in
deffun dist2(v1, v2)
defvar td = sub(v1, v2) in {
dotP(td, td)
}
in {
print(dotP(vector(1,2,3), vector(4,5,6)));
print(" ");
print(dist2(vector(1,2,3), vector(4,5,6)));
"";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment