From 69668aa61de642ea275e6e2ace85f7b921480603 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 6 May 2015 23:35:58 +0200 Subject: Separate Units --- units.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'units.go') diff --git a/units.go b/units.go index 4ee5e2a..c3da609 100644 --- a/units.go +++ b/units.go @@ -1,9 +1,55 @@ package main +import "fmt" + const ( - MM = 20.0 + MM = Unit(20.0) CM = 10 * MM DM = 10 * CM IN = 25.4 * MM PT = IN / 72 ) + +type Unit float64 + +func (u Unit) String() string { + return fmt.Sprintf("%.2f", u) +} + +type Point struct { + X, Y Unit +} + +func (p Point) String() string { + return fmt.Sprintf("%v,%v", p.X, p.Y) +} + +type Triple struct { + U, V, W Unit +} + +func (t Triple) String() string { + return fmt.Sprintf("%v,%v,%v", t.U, t.V, t.W) +} + +type Polar struct { + R, Theta Unit +} + +type Path []Point + +func (p Point) Add(u Point) Point { + return Point{p.X + u.X, p.Y + u.Y} +} + +func (p Point) Sub(u Point) Point { + return Point{p.X - u.X, p.Y - u.Y} +} + +func (p Point) AddX(u Unit) Point { + return Point{p.X + u, p.Y} +} + +func (p Point) AddY(u Unit) Point { + return Point{p.X, p.Y + u} +} -- cgit v1.2.3