Created
September 16, 2012 12:41
-
-
Save netguy204/3732276 to your computer and use it in GitHub Desktop.
Vectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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