2022-11-03 13:54:49 +00:00
|
|
|
package shape
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-25 01:32:42 +00:00
|
|
|
"math"
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
2023-02-10 19:03:07 +00:00
|
|
|
"oss.terrastruct.com/util-go/go2"
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type shapeDiamond struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDiamond(box *geo.Box) Shape {
|
2023-02-10 19:03:07 +00:00
|
|
|
shape := shapeDiamond{
|
2022-11-03 13:54:49 +00:00
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: DIAMOND_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-02-10 19:03:07 +00:00
|
|
|
shape.FullShape = go2.Pointer(Shape(shape))
|
|
|
|
|
return shape
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
func (s shapeDiamond) GetInnerBox() *geo.Box {
|
|
|
|
|
width := s.Box.Width
|
|
|
|
|
height := s.Box.Height
|
|
|
|
|
tl := s.Box.TopLeft.Copy()
|
|
|
|
|
tl.X += width / 4.
|
|
|
|
|
tl.Y += height / 4.
|
|
|
|
|
width /= 2.
|
|
|
|
|
height /= 2.
|
|
|
|
|
return geo.NewBox(tl, width, height)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func diamondPath(box *geo.Box) *svg.SvgPathContext {
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, box.Width/77, box.Height/76.9)
|
|
|
|
|
pc.StartAt(pc.Absolute(38.5, 76.9))
|
|
|
|
|
pc.C(true, -0.3, 0, -0.5, -0.1, -0.7, -0.3)
|
|
|
|
|
pc.L(false, 0.3, 39.2)
|
|
|
|
|
pc.C(true, -0.4, -0.4, -0.4, -1, 0, -1.4)
|
|
|
|
|
pc.L(false, 37.8, 0.3)
|
|
|
|
|
pc.C(true, 0.4, -0.4, 1, -0.4, 1.4, 0)
|
|
|
|
|
pc.L(true, 37.5, 37.5)
|
|
|
|
|
pc.C(true, 0.4, 0.4, 0.4, 1, 0, 1.4)
|
|
|
|
|
pc.L(false, 39.2, 76.6)
|
|
|
|
|
pc.C(false, 39, 76.8, 38.8, 76.9, 38.5, 76.9)
|
|
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeDiamond) Perimeter() []geo.Intersectable {
|
|
|
|
|
return diamondPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeDiamond) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
diamondPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapeDiamond) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
|
|
|
|
totalWidth := 2 * (width + paddingX)
|
|
|
|
|
totalHeight := 2 * (height + paddingY)
|
2023-01-25 01:32:42 +00:00
|
|
|
return math.Ceil(totalWidth), math.Ceil(totalHeight)
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|
2023-01-24 03:10:31 +00:00
|
|
|
|
|
|
|
|
func (s shapeDiamond) GetDefaultPadding() (paddingX, paddingY float64) {
|
|
|
|
|
return defaultPadding / 4, defaultPadding / 2
|
|
|
|
|
}
|