Skip to content

Instantly share code, notes, and snippets.

@FreekPaans
Created August 13, 2015 06:23
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 FreekPaans/48d8fc404fad7be1ca8a to your computer and use it in GitHub Desktop.
Save FreekPaans/48d8fc404fad7be1ca8a to your computer and use it in GitHub Desktop.
module Vector =
type PolarVector = {
r: double;
a: double;
}
type RectangularVector = {
x: double;
y: double;
}
type Vector = private Polar of PolarVector | Rectangle of RectangularVector
let createRectangular x y =
Rectangle { x = x; y = y;}
let createPolar r a =
Polar { r=r;a=a }
let private polar2Rect v =
Rectangle { x = (cos v.a) * v.r; y = (sin v.a) * v.r }
let rec Add v1 v2 =
match v1 with
| Rectangle v1xy ->
match v2 with
| Rectangle v2xy -> Rectangle { x = v1xy.x + v2xy.x; y = v1xy.y + v2xy.y }
| Polar polar -> Add v1 (polar2Rect polar)
| Polar polar ->
Add (polar2Rect polar) v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment