Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active August 29, 2015 14: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 ToJans/e796b2256ff948149a9f to your computer and use it in GitHub Desktop.
Save ToJans/e796b2256ff948149a9f to your computer and use it in GitHub Desktop.
Does something like "fzip" even exist? Or did I just invent a wacko name to do this stuff?
open System.Runtime.Serialization
[<DataContract>]
type Vector3D = {
[<DataMember>] x: decimal
[<DataMember>] y: decimal
[<DataMember>] z: decimal
} with
member this.width = this.x
member this.height = this.y
member this.depth = this.z
member this.fmap f = {x=f this.x;y = f this.y;z = f this.z}
member this.liftA2 f r = {x=f this.x r.x;y = f this.y r.y;z = f this.z r.z}
static member fromDecimal x y z = {x=x;y=y;z=z}
static member zero = Vector3D.fromDecimal 0M 0M 0M
static member unit = Vector3D.fromDecimal 1M 1M 1M
static member (~-) (v:Vector3D) = v.fmap (~-)
member this.half = this.fmap ((*) 0.5M )
member this.toDecimalList = [this.x;this.y;this.z]
member v.rotateY_CW_90degrees = Vector3D.fromDecimal (-v.z) v.y v.x
member v.rotateY_CCW_90degrees = Vector3D.fromDecimal v.z v.y (-v.x)
member this.abs =
let abs x = if x<0M then -x else x
{x = abs this.x;y=abs this.y;z = abs this.z}
override t.ToString() =
if t.x = t.y && t.x = t.z then
t.x.ToString()
else
sprintf "x=%M, y=%M, z=%M" t.x t.y t.z
type Delta =
| DV of Vector3D
| DX of decimal
| DY of decimal
| DZ of decimal
member this.toVector = match this with
| DV v -> v
| DX v -> Vector3D.fromDecimal v 0M 0M
| DY v -> Vector3D.fromDecimal 0M v 0M
| DZ v -> Vector3D.fromDecimal 0M 0M v
static member (+) (l:Vector3D,r:Delta) = r.toVector |> l.liftA2 (+)
static member (-) (l:Vector3D,r:Delta) = r.toVector |> l.liftA2 (-)
static member (~-) (d:Delta) = d.toVector.fmap (~-)
member this.half = this.toVector.fmap (fun x-> x/2M)
member this.x = this.toVector.x |> DX
member this.y = this.toVector.y |> DY
member this.z = this.toVector.z |> DZ
override t.ToString() = sprintf "D %O" t.toVector
type Scale =
| S of decimal
| SV of Vector3D
| SX of decimal
| SY of decimal
| SZ of decimal
member this.toVector = match this with
| S v -> Vector3D.fromDecimal v v v
| SV v -> v
| SX v -> Vector3D.fromDecimal v 1M 1M
| SY v -> Vector3D.fromDecimal 1M v 1M
| SZ v -> Vector3D.fromDecimal 1M 1M v
static member (*) (l:Vector3D,r:Scale) = r.toVector |> l.liftA2 (*)
static member (/) (l:Vector3D,r:Scale) = r.toVector |> l.liftA2 (/)
static member (~-) (s:Scale) = s.toVector.fmap (~-)
member this.half = this.toVector.half |> SV
member this.width = this.toVector.x
member this.height = this.toVector.y
member this.depth = this.toVector.z
static member bottom = SY -0.5M
static member left = SX -0.5M
static member back = SZ -0.5M
static member top = SY 0.5M
static member right = SX 0.5M
static member front = SZ 0.5M
override t.ToString() = sprintf "S %O" t.toVector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment