Skip to content

Instantly share code, notes, and snippets.

@FreekPaans
Last active August 29, 2015 14:27
Show Gist options
  • Save FreekPaans/df58d5f80261ad6018fc to your computer and use it in GitHub Desktop.
Save FreekPaans/df58d5f80261ad6018fc to your computer and use it in GitHub Desktop.
module Vector =
type private PolarVector = {
r: double;
a: double;
}
type private 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