...
1 package shape
2
3 import (
4 "math"
5
6 "oss.terrastruct.com/d2/lib/geo"
7 "oss.terrastruct.com/d2/lib/svg"
8 "oss.terrastruct.com/util-go/go2"
9 )
10
11 type shapeHexagon struct {
12 *baseShape
13 }
14
15 func NewHexagon(box *geo.Box) Shape {
16 shape := shapeHexagon{
17 baseShape: &baseShape{
18 Type: HEXAGON_TYPE,
19 Box: box,
20 },
21 }
22 shape.FullShape = go2.Pointer(Shape(shape))
23 return shape
24 }
25
26 func (s shapeHexagon) GetInnerBox() *geo.Box {
27 width := s.Box.Width
28 height := s.Box.Height
29 tl := s.Box.TopLeft.Copy()
30 tl.X += width / 6.
31 width /= 1.5
32 tl.Y += height / 6.
33 height /= 1.5
34 return geo.NewBox(tl, width, height)
35 }
36
37 func hexagonPath(box *geo.Box) *svg.SvgPathContext {
38 halfYFactor := 43.6 / 87.3
39 pc := svg.NewSVGPathContext(box.TopLeft, box.Width, box.Height)
40 pc.StartAt(pc.Absolute(0.25, 0))
41 pc.L(false, 0, halfYFactor)
42 pc.L(false, 0.25, 1)
43 pc.L(false, 0.75, 1)
44 pc.L(false, 1, halfYFactor)
45 pc.L(false, 0.75, 0)
46 pc.Z()
47 return pc
48 }
49
50 func (s shapeHexagon) Perimeter() []geo.Intersectable {
51 return hexagonPath(s.Box).Path
52 }
53
54 func (s shapeHexagon) GetSVGPathData() []string {
55 return []string{
56 hexagonPath(s.Box).PathData(),
57 }
58 }
59
60 func (s shapeHexagon) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
61 totalWidth := 1.5 * (width + paddingX)
62 totalHeight := 1.5 * (height + paddingY)
63 return math.Ceil(totalWidth), math.Ceil(totalHeight)
64 }
65
66 func (s shapeHexagon) GetDefaultPadding() (paddingX, paddingY float64) {
67 return defaultPadding / 2, defaultPadding / 2
68 }
69
View as plain text