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 shapeDiamond struct {
12 *baseShape
13 }
14
15 func NewDiamond(box *geo.Box) Shape {
16 shape := shapeDiamond{
17 baseShape: &baseShape{
18 Type: DIAMOND_TYPE,
19 Box: box,
20 },
21 }
22 shape.FullShape = go2.Pointer(Shape(shape))
23 return shape
24 }
25
26 func (s shapeDiamond) GetInnerBox() *geo.Box {
27 width := s.Box.Width
28 height := s.Box.Height
29 tl := s.Box.TopLeft.Copy()
30 tl.X += width / 4.
31 tl.Y += height / 4.
32 width /= 2.
33 height /= 2.
34 return geo.NewBox(tl, width, height)
35 }
36
37 func diamondPath(box *geo.Box) *svg.SvgPathContext {
38 pc := svg.NewSVGPathContext(box.TopLeft, box.Width/77, box.Height/76.9)
39 pc.StartAt(pc.Absolute(38.5, 76.9))
40 pc.C(true, -0.3, 0, -0.5, -0.1, -0.7, -0.3)
41 pc.L(false, 0.3, 39.2)
42 pc.C(true, -0.4, -0.4, -0.4, -1, 0, -1.4)
43 pc.L(false, 37.8, 0.3)
44 pc.C(true, 0.4, -0.4, 1, -0.4, 1.4, 0)
45 pc.L(true, 37.5, 37.5)
46 pc.C(true, 0.4, 0.4, 0.4, 1, 0, 1.4)
47 pc.L(false, 39.2, 76.6)
48 pc.C(false, 39, 76.8, 38.8, 76.9, 38.5, 76.9)
49 pc.Z()
50 return pc
51 }
52
53 func (s shapeDiamond) Perimeter() []geo.Intersectable {
54 return diamondPath(s.Box).Path
55 }
56
57 func (s shapeDiamond) GetSVGPathData() []string {
58 return []string{
59 diamondPath(s.Box).PathData(),
60 }
61 }
62
63 func (s shapeDiamond) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
64 totalWidth := 2 * (width + paddingX)
65 totalHeight := 2 * (height + paddingY)
66 return math.Ceil(totalWidth), math.Ceil(totalHeight)
67 }
68
69 func (s shapeDiamond) GetDefaultPadding() (paddingX, paddingY float64) {
70 return defaultPadding / 4, defaultPadding / 2
71 }
72
View as plain text