From b96af5338fc9cfa68d0795a740dc7d05130514c1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 13 Aug 2015 20:35:57 +1000 Subject: [PATCH] math/fixed: add Point methods (Add, Sub, Mul, Div). These mirror the image.Point methods in the standard library. Change-Id: I7309af308a8182e325af20b47341f72703cbc95a Reviewed-on: https://go-review.googlesource.com/13603 Reviewed-by: David Crawshaw --- math/fixed/fixed.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/math/fixed/fixed.go b/math/fixed/fixed.go index f943419..8e78e6c 100644 --- a/math/fixed/fixed.go +++ b/math/fixed/fixed.go @@ -63,7 +63,47 @@ type Point26_6 struct { X, Y Int26_6 } +// Add returns the vector p+q. +func (p Point26_6) Add(q Point26_6) Point26_6 { + return Point26_6{p.X + q.X, p.Y + q.Y} +} + +// Sub returns the vector p-q. +func (p Point26_6) Sub(q Point26_6) Point26_6 { + return Point26_6{p.X - q.X, p.Y - q.Y} +} + +// Mul returns the vector p*k. +func (p Point26_6) Mul(k Int26_6) Point26_6 { + return Point26_6{p.X * k / 64, p.Y * k / 64} +} + +// Div returns the vector p/k. +func (p Point26_6) Div(k Int26_6) Point26_6 { + return Point26_6{p.X * 64 / k, p.Y * 64 / k} +} + // Point52_12 is a 52.12 fixed-point coordinate pair. type Point52_12 struct { X, Y Int52_12 } + +// Add returns the vector p+q. +func (p Point52_12) Add(q Point52_12) Point52_12 { + return Point52_12{p.X + q.X, p.Y + q.Y} +} + +// Sub returns the vector p-q. +func (p Point52_12) Sub(q Point52_12) Point52_12 { + return Point52_12{p.X - q.X, p.Y - q.Y} +} + +// Mul returns the vector p*k. +func (p Point52_12) Mul(k Int52_12) Point52_12 { + return Point52_12{p.X * k / 4096, p.Y * k / 4096} +} + +// Div returns the vector p/k. +func (p Point52_12) Div(k Int52_12) Point52_12 { + return Point52_12{p.X * 4096 / k, p.Y * 4096 / k} +}