Update
[dormin.git] / vec.ml
blobddf2b585d85552d0250f0132438d066d5dbe8798
1 module IntOp =
2 struct
3 let (+) = (+)
4 let (-) = (-)
5 let ( * ) = ( * )
6 let ( / ) = ( / )
7 end
9 module FltOp =
10 struct
11 let (+) = (+.)
12 let (-) = (-.)
13 let ( * ) = ( *. )
14 let ( / ) = ( /. )
15 end
17 type t = { x : float; y : float; z : float }
19 let vx v = v.x
20 let vy v = v.y
21 let vz v = v.z
23 let make x y z = { x = x; y = y; z = z }
24 let elts v = v.x, v.y, v.z
26 let origin = { x = 0.; y = 0.; z = 0. }
27 let x_axis = { origin with x = 1. }
28 let y_axis = { origin with y = 1. }
29 let z_axis = { origin with z = 1. }
31 open FltOp
32 let neg v = { x = -.v.x; y = -.v.y; z = -.v.z }
33 let add v u = { x = v.x + u.x; y = v.y + u.y; z = v.z + u.z }
34 let sub v u = { x = v.x - u.x; y = v.y - u.y; z = v.z - u.z }
35 let scale v s = { x = v.x * s; y = v.y * s; z = v.z * s }
36 let divide v s = { x = v.x / s; y = v.y / s; z = v.z / s }
37 let cross v u = { x = v.y * u.z - v.z * u.y;
38 y = v.z * u.x - v.x * u.z;
39 z = v.x * u.y - v.y * u.x }
40 let dot v u = v.x * u.x + v.y * u.y + v.z * u.z
41 let magnitude_squared v = dot v v
42 let magnitude v = sqrt (magnitude_squared v)
43 let normalize v = scale v (1. / (magnitude v))
45 let print ppf v =
46 Format.fprintf ppf "[@[<1>x % 11f;@ y % 11f;@ z % 11f]@]" v.x v.y v.z