2022-11-03 13:54:49 +00:00
|
|
|
package shape
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type shapeParallelogram struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
const parallelWedgeWidth = 26.
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func NewParallelogram(box *geo.Box) Shape {
|
|
|
|
|
return shapeParallelogram{
|
|
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: PARALLELOGRAM_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
func (s shapeParallelogram) GetInnerBox() *geo.Box {
|
|
|
|
|
tl := s.Box.TopLeft.Copy()
|
|
|
|
|
width := s.Box.Width - 2*parallelWedgeWidth
|
|
|
|
|
tl.X += parallelWedgeWidth
|
|
|
|
|
return geo.NewBox(tl, width, s.Box.Height)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func parallelogramPath(box *geo.Box) *svg.SvgPathContext {
|
2023-01-21 04:04:59 +00:00
|
|
|
wedgeWidth := parallelWedgeWidth
|
|
|
|
|
// Note: box width should always be larger than parallelWedgeWidth
|
|
|
|
|
// this just handles after collapsing into a line
|
2022-11-03 13:54:49 +00:00
|
|
|
if box.Width <= wedgeWidth {
|
|
|
|
|
wedgeWidth = box.Width / 2.0
|
|
|
|
|
}
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
|
|
|
|
pc.StartAt(pc.Absolute(wedgeWidth, 0))
|
|
|
|
|
pc.L(false, box.Width, 0)
|
|
|
|
|
pc.L(false, box.Width-wedgeWidth, box.Height)
|
|
|
|
|
pc.L(false, 0, box.Height)
|
|
|
|
|
pc.L(false, 0, box.Height)
|
|
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeParallelogram) Perimeter() []geo.Intersectable {
|
|
|
|
|
return parallelogramPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeParallelogram) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
parallelogramPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapeParallelogram) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
|
|
|
|
totalWidth := width + paddingX + parallelWedgeWidth*2
|
|
|
|
|
return totalWidth, height + paddingY
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|