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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type shapeCylinder struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
const (
|
|
|
|
|
defaultArcDepth = 24.
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func NewCylinder(box *geo.Box) Shape {
|
|
|
|
|
return shapeCylinder{
|
|
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: CYLINDER_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
func getArcHeight(box *geo.Box) float64 {
|
|
|
|
|
arcHeight := defaultArcDepth
|
|
|
|
|
// Note: box height should always be larger than 3*default
|
|
|
|
|
// this just handles after collapsing into an oval
|
|
|
|
|
if box.Height < arcHeight*2 {
|
|
|
|
|
arcHeight = box.Height / 2.0
|
|
|
|
|
}
|
|
|
|
|
return arcHeight
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func (s shapeCylinder) GetInnerBox() *geo.Box {
|
|
|
|
|
height := s.Box.Height
|
|
|
|
|
tl := s.Box.TopLeft.Copy()
|
2023-01-21 04:04:59 +00:00
|
|
|
arc := getArcHeight(s.Box)
|
|
|
|
|
height -= 3 * arc
|
|
|
|
|
tl.Y += 2 * arc
|
2022-11-03 13:54:49 +00:00
|
|
|
return geo.NewBox(tl, s.Box.Width, height)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cylinderOuterPath(box *geo.Box) *svg.SvgPathContext {
|
2023-01-21 04:04:59 +00:00
|
|
|
arcHeight := getArcHeight(box)
|
2022-11-03 13:54:49 +00:00
|
|
|
multiplier := 0.45
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.StartAt(pc.Absolute(0, arcHeight))
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.C(false, 0, 0, box.Width*multiplier, 0, box.Width/2, 0)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.C(false, box.Width-box.Width*multiplier, 0, box.Width, 0, box.Width, arcHeight)
|
|
|
|
|
pc.V(true, box.Height-arcHeight*2)
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.C(false, box.Width, box.Height, box.Width-box.Width*multiplier, box.Height, box.Width/2, box.Height)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.C(false, box.Width*multiplier, box.Height, 0, box.Height, 0, box.Height-arcHeight)
|
|
|
|
|
pc.V(true, -(box.Height - arcHeight*2))
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cylinderInnerPath(box *geo.Box) *svg.SvgPathContext {
|
2023-01-21 04:04:59 +00:00
|
|
|
arcHeight := getArcHeight(box)
|
2022-11-03 13:54:49 +00:00
|
|
|
multiplier := 0.45
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.StartAt(pc.Absolute(0, arcHeight))
|
|
|
|
|
pc.C(false, 0, arcHeight*2, box.Width*multiplier, arcHeight*2, box.Width/2, arcHeight*2)
|
|
|
|
|
pc.C(false, box.Width-box.Width*multiplier, arcHeight*2, box.Width, arcHeight*2, box.Width, arcHeight)
|
2022-11-03 13:54:49 +00:00
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeCylinder) Perimeter() []geo.Intersectable {
|
|
|
|
|
return cylinderOuterPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeCylinder) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
cylinderOuterPath(s.Box).PathData(),
|
|
|
|
|
cylinderInnerPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapeCylinder) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
2023-01-21 04:04:59 +00:00
|
|
|
// 2 arcs top, height + padding, 1 arc bottom
|
2023-01-23 18:32:12 +00:00
|
|
|
totalHeight := height + paddingY + 3*defaultArcDepth
|
2023-01-25 01:32:42 +00:00
|
|
|
return math.Ceil(width + paddingX), math.Ceil(totalHeight)
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|
2023-01-24 03:10:31 +00:00
|
|
|
|
|
|
|
|
func (s shapeCylinder) GetDefaultPadding() (paddingX, paddingY float64) {
|
|
|
|
|
return defaultPadding, defaultPadding / 2
|
|
|
|
|
}
|