Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lidded box #1

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# test -coverprofile output
*.out

# other files
*.plt
112 changes: 34 additions & 78 deletions box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,121 +8,77 @@ import (
u "github.com/happyRip/Box-Tailor/box/utility"
)

type Draft interface {
Draw() string
type Drafter interface {
Draw() []string
CalculateSize() (float64, float64)
}

type product struct {
name string
size u.Triad
boxVariant string
type Product struct {
Name string
Size u.Triad
BoxVariant string
}

func NewProduct() product {
return product{}
}

// TODO
func (p product) Draw() string {
return ""
}

// TODO
func (p product) CalculateSize() (float64, float64) {
return 0, 0
}

func (p *product) SetName(name string) error {
p.name = name
return nil
}

func (p *product) SetNameFromFilepath(filepath string) error {
func (p *Product) SetNameFromFilepath(filepath string) error {
filename := path.Base(filepath)
p.name = u.TrimExtension(filename)
p.Name = u.TrimExtension(filename)
return nil
}

func (p *product) SetSize(x, y, z float64) error {
func (p *Product) SetSize(x, y, z float64) error {
if u.AnyNotPositive(x, y, z) {
return errors.New("dimensions cannot be negative")
}
p.size.SetValues(x, y, z)
return nil
}

func (p *product) SetVariant(variant string) error {
p.boxVariant = variant
p.Size.SetValues(x, y, z)
return nil
}

func (p product) Name() string {
return p.name
}

func (p product) Size() (u.Triad, error) {
return p.size, nil
}

func (p product) SizeX() float64 {
x, _, _, _ := p.size.GetValues()
func (p Product) SizeX() float64 {
x := p.Size.X
return x
}

func (p product) SizeY() float64 {
_, y, _, _ := p.size.GetValues()
func (p Product) SizeY() float64 {
y := p.Size.Y
return y
}

func (p product) SizeZ() float64 {
_, _, z, _ := p.size.GetValues()
func (p Product) SizeZ() float64 {
z := p.Size.Z
return z
}

func (p product) BoxVariant() string {
return p.boxVariant
type Board struct {
Size u.Pair
Margin u.Pair
}

type board struct {
size u.Pair
margin u.Pair
}

func (b *board) SetSize(x, y float64) error {
func (b *Board) SetSize(x, y float64) error {
if u.AnyNotPositive(x, y) {
return errors.New("dimension not positive")
}
b.size.SetValues(x, y)
b.Size.SetValues(x, y)
return nil
}

func (b *board) SetMargin(x, y float64) error {
func (b *Board) SetMargin(x, y float64) error {
if u.AnyLessThanZero(x, y) {
return errors.New("dimension less than zero")
}
b.margin.SetValues(x, y)
b.Margin.SetValues(x, y)
return nil
}

func (b board) Size() (float64, float64, error) {
return b.size.GetValues()
}

func (b board) SizeX() float64 {
x, _, _ := b.size.GetValues()
func (b Board) SizeX() float64 {
x := b.Size.X
return x
}

func (b board) SizeY() float64 {
_, y, _ := b.size.GetValues()
func (b Board) SizeY() float64 {
y := b.Size.Y
return y
}

func (b board) Margin() (float64, float64, error) {
return b.margin.GetValues()
}

type shelfParams struct {
width, height float64
margin u.Pair
Expand Down Expand Up @@ -159,8 +115,8 @@ func (s shelfParams) Margin() (u.Pair, error) {
}

type rack struct {
productList []product
shelfList [][]product
productList []Product
shelfList [][]Product
shelfParameters shelfParams
}

Expand All @@ -178,15 +134,15 @@ func (r *rack) ShelfPack() error {

products := r.productList
var (
shelf []product
shelf []Product
currPos float64
)
for len(products) > 0 {
i := LessOrEqual('x', r.Width()-currPos, products...)

if i == -1 {
r.AppendShelf(shelf...)
shelf = []product{}
shelf = []Product{}
currPos = 0
i = 0
}
Expand All @@ -202,7 +158,7 @@ func (r *rack) ShelfPack() error {
return nil
}

func (r *rack) AppendShelf(products ...product) {
func (r *rack) AppendShelf(products ...Product) {
r.shelfList = append(r.shelfList, products)
}

Expand All @@ -214,14 +170,14 @@ func (r rack) Height() float64 {
return r.shelfParameters.Height()
}

func RemoveFromProductSlice(i int, products ...product) []product {
func RemoveFromProductSlice(i int, products ...Product) []Product {
if i >= len(products)-1 {
return products[:i]
}
return append(products[:i], products[i+1:]...)
}

// TODO
func LessOrEqual(axis rune, value float64, args ...product) int {
func LessOrEqual(axis rune, value float64, args ...Product) int {
return 0
}
51 changes: 0 additions & 51 deletions box/box_test.go

This file was deleted.

87 changes: 87 additions & 0 deletions box/lidded/lidded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package lidded

import (
"github.com/happyRip/Box-Tailor/box"
"github.com/happyRip/Box-Tailor/box/utility"
"github.com/happyRip/Box-Tailor/plotter"
)

type Box struct {
Content box.Product
Margin utility.Triad
Origin utility.Pair
BoardThickness float64
}

func (b Box) Draw() []string {
x, y, z := b.InternalSize()
thk := b.BoardThickness
var pen plotter.Pen

// draw cut lines
out := []string{plotter.SelectPen(1)}
if b.Origin.X != 0 || b.Origin.Y != 0 {
out = append(out, pen.MoveAbsolute(b.Origin.X, b.Origin.Y))
}
for i := 0; i < 2; i++ {
out = append(out,
pen.LineShape(
[][2]float64{
{0, -(2*(thk+z) + y)},
{z, 0},
{0, z + thk},
{0.5 * thk, 0},
{0, -(z + thk)},
{x + thk, 0},
{0, z + thk},
{0.5 * thk, 0},
{0, -(z + thk)},
{z, 0},
}...,
)...,
)
x, y, z = -x, -y, -z
thk = -thk
}

//draw fold lines
out = append(out,
plotter.SelectPen(2),
pen.MoveRelative(z+0.5*thk, -(z+0.5*thk)),
pen.DrawRectangle(x+thk, -(y+thk)),
pen.MoveAbsolute(b.Origin.X, -(z+thk)),
pen.Line(z, 0),
pen.MoveRelative(-z, -y),
pen.Line(z, 0),
pen.MoveRelative(2*thk+x, 0),
pen.Line(z, 0),
pen.MoveRelative(-z, y),
pen.Line(z, 0),
)

return out
}

func (b Box) CalculateSize() (float64, float64) {
x, y, z := b.InternalSize()
thk := b.BoardThickness
return 2*(z+thk) + x, 2*(z+thk) + y
}

func (b *Box) SetBuffer(x, y, z float64) {
b.Margin.SetValues(x, y, z)
}

func (b Box) ContentSize() (float64, float64, float64) {
return b.Content.Size.GetValues()
}

func (b Box) MarginSize() (float64, float64, float64) {
return b.Margin.GetValues()
}

func (b Box) InternalSize() (float64, float64, float64) {
x, y, z := b.ContentSize()
m, n, o := b.MarginSize()
return x + m, y + n, z + o
}
20 changes: 8 additions & 12 deletions box/utility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ func AnyLessThanZero(args ...float64) bool {
}

type Triad struct {
x, y, z float64
}

func NewTriad() Triad {
return Triad{}
X, Y, Z float64
}

func (t *Triad) SetValues(x, y, z float64) error {
t.x, t.y, t.z = x, y, 0
t.X, t.Y, t.Z = x, y, z
return nil
}

func (t Triad) GetValues() (float64, float64, float64, error) {
return t.x, t.y, t.x, nil
func (t Triad) GetValues() (float64, float64, float64) {
return t.X, t.Y, t.Z
}

func (t Triad) X() float64 {
Expand All @@ -58,14 +54,14 @@ func (t Triad) Z() float64 {
}

type Pair struct {
x, y float64
X, Y float64
}

func (p *Pair) SetValues(x, y float64) error {
p.x, p.y = x, y
p.X, p.Y = x, y
return nil
}

func (p Pair) GetValues() (float64, float64, error) {
return p.x, p.y, nil
func (p Pair) GetValues() (float64, float64) {
return p.X, p.Y
}
Loading