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 shapeHexagon struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHexagon(box *geo.Box) Shape {
|
2023-02-10 19:03:07 +00:00
|
|
|
shape := shapeHexagon{
|
2022-11-03 13:54:49 +00:00
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: HEXAGON_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 shapeHexagon) GetInnerBox() *geo.Box {
|
|
|
|
|
width := s.Box.Width
|
2023-01-25 20:24:56 +00:00
|
|
|
height := s.Box.Height
|
2023-01-21 04:04:59 +00:00
|
|
|
tl := s.Box.TopLeft.Copy()
|
2023-01-25 20:24:56 +00:00
|
|
|
tl.X += width / 6.
|
|
|
|
|
width /= 1.5
|
|
|
|
|
tl.Y += height / 6.
|
|
|
|
|
height /= 1.5
|
|
|
|
|
return geo.NewBox(tl, width, height)
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func hexagonPath(box *geo.Box) *svg.SvgPathContext {
|
|
|
|
|
halfYFactor := 43.6 / 87.3
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, box.Width, box.Height)
|
|
|
|
|
pc.StartAt(pc.Absolute(0.25, 0))
|
|
|
|
|
pc.L(false, 0, halfYFactor)
|
|
|
|
|
pc.L(false, 0.25, 1)
|
|
|
|
|
pc.L(false, 0.75, 1)
|
|
|
|
|
pc.L(false, 1, halfYFactor)
|
|
|
|
|
pc.L(false, 0.75, 0)
|
|
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeHexagon) Perimeter() []geo.Intersectable {
|
|
|
|
|
return hexagonPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeHexagon) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
hexagonPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapeHexagon) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
2023-01-25 20:24:56 +00:00
|
|
|
totalWidth := 1.5 * (width + paddingX)
|
|
|
|
|
totalHeight := 1.5 * (height + paddingY)
|
|
|
|
|
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 shapeHexagon) GetDefaultPadding() (paddingX, paddingY float64) {
|
2023-01-25 20:24:56 +00:00
|
|
|
return defaultPadding / 2, defaultPadding / 2
|
2023-01-24 03:10:31 +00:00
|
|
|
}
|