From: Maximilian Friedersdorff Date: Mon, 11 Mar 2019 10:17:54 +0000 (+0000) Subject: Initial Commit: Vec type, Cross, Dot and Sub methods X-Git-Url: https://git.friedersdorff.com/?p=max%2Fvector.go.git;a=commitdiff_plain;h=2aa7511dfa09910d809a85fd41023d42301af664 Initial Commit: Vec type, Cross, Dot and Sub methods --- 2aa7511dfa09910d809a85fd41023d42301af664 diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..e5f0b56 --- /dev/null +++ b/README.rst @@ -0,0 +1,3 @@ +vector.go +========= +Some utilities for dealing with 3D vectors. diff --git a/vector.go b/vector.go new file mode 100644 index 0000000..456b2e2 --- /dev/null +++ b/vector.go @@ -0,0 +1,22 @@ +// vector provides a Vec type and methods for performing vector arithmetic +package vector + +// 3 dimensional vector +type Vec struct { + X, Y, Z float64 +} + +// Cross returns the cross product between vectors v and o +func (v Vec) Cross(o Vec) Vec { + return Vec{v.Y*o.Z - v.Z*o.Y, v.Z*o.X - v.X*o.Z, v.X*o.Y - v.Y*o.X} +} + +// Doc returns the dot product between vectors v and o +func (v Vec) Dot(o Vec) float64 { + return v.X*o.X + v.Y*o.Y + v.Z*o.Z +} + +// sub subtracts o from v +func (v Vec) Sub(o Vec) Vec { + return Vec{v.X - o.X, v.Y - o.Y, v.Z - o.Z} +}