Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active April 18, 2020 16:02
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 amatiasq/728842c8274ab0f28b3203b87260d087 to your computer and use it in GitHub Desktop.
Save amatiasq/728842c8274ab0f28b3203b87260d087 to your computer and use it in GitHub Desktop.
[<Struct>]
type Vector =
{ X: float; Y: float; Z: float }
static member (*) (k, v: Vector) = { X = k * v.X; Y = k * v.Y; Z = k * v.Z }
static member (-) (v1: Vector, v2: Vector) = { X = v1.X - v2.X; Y = v1.Y - v2.Y; Z = v1.Z - v2.Z }
static member (+) (v1: Vector, v2: Vector) = { X = v1.X + v2.X; Y = v1.Y + v2.Y; Z = v1.Z + v2.Z }
static member Dot (v1: Vector, v2: Vector) = v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z
static member Mag (v: Vector) = sqrt (v.X * v.X + v.Y * v.Y + v.Z * v.Z)
static member Norm (v: Vector) =
let mag = Vector.Mag v
let div = if mag = 0.0 then infinity else 1.0/mag
div * v
static member Cross (v1: Vector, v2: Vector) =
{ X = v1.Y * v2.Z - v1.Z * v2.Y
; Y = v1.Z * v2.X - v1.X * v2.Z
; Z = v1.X * v2.Y - v1.Y * v2.X }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment