From 2aa7511dfa09910d809a85fd41023d42301af664 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Mon, 11 Mar 2019 10:17:54 +0000 Subject: [PATCH] Initial Commit: Vec type, Cross, Dot and Sub methods --- README.rst | 3 +++ vector.go | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 README.rst create mode 100644 vector.go 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} +} -- 2.47.1