Passed
Push — issue_147 ( 80ed33 )
by Doug
04:13
created

OrientatedItem::isStable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use function min;
12
13
/**
14
 * An item to be packed.
15
 *
16
 * @author Doug Wright
17
 */
18
class OrientatedItem
19
{
20
    /**
21
     * @var Item
22
     */
23
    protected $item;
24
25
    /**
26
     * @var int
27
     */
28
    protected $width;
29
30
    /**
31
     * @var int
32
     */
33
    protected $length;
34
35
    /**
36
     * @var int
37
     */
38
    protected $depth;
39
40
    /** @var float */
41
    protected $tippingPoint;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Item $item
47
     * @param int  $width
48
     * @param int  $length
49
     * @param int  $depth
50
     */
51 21
    public function __construct(Item $item, int $width, int $length, int $depth)
52
    {
53 21
        $this->item = $item;
54 21
        $this->width = $width;
55 21
        $this->length = $length;
56 21
        $this->depth = $depth;
57 21
        $this->tippingPoint = atan(min($this->length, $this->width) / $this->depth);
58 21
    }
59
60
    /**
61
     * Item.
62
     *
63
     * @return Item
64
     */
65 21
    public function getItem(): Item
66
    {
67 21
        return $this->item;
68
    }
69
70
    /**
71
     * Item width in mm in it's packed orientation.
72
     *
73
     * @return int
74
     */
75 21
    public function getWidth(): int
76
    {
77 21
        return $this->width;
78
    }
79
80
    /**
81
     * Item length in mm in it's packed orientation.
82
     *
83
     * @return int
84
     */
85 21
    public function getLength(): int
86
    {
87 21
        return $this->length;
88
    }
89
90
    /**
91
     * Item depth in mm in it's packed orientation.
92
     *
93
     * @return int
94
     */
95 21
    public function getDepth(): int
96
    {
97 21
        return $this->depth;
98
    }
99
100
    /**
101
     * Calculate the surface footprint of the current orientation.
102
     *
103
     * @return int
104
     */
105 13
    public function getSurfaceFootprint(): int
106
    {
107 13
        return $this->width * $this->length;
108
    }
109
110
    /**
111
     * @return float
112
     */
113
    public function getTippingPoint(): float
114
    {
115
        return $this->tippingPoint;
116
    }
117
118
    /**
119
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
120
     *
121
     * N.B. Assumes equal weight distribution.
122
     *
123
     * @return bool
124
     */
125 21
    public function isStable(): bool
126
    {
127 21
        return $this->tippingPoint > 0.261;
128
    }
129
}
130