...

Source file src/oss.terrastruct.com/d2/lib/shape/shape_parallelogram.go

Documentation: oss.terrastruct.com/d2/lib/shape

     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 shapeParallelogram struct {
    12  	*baseShape
    13  }
    14  
    15  const parallelWedgeWidth = 26.
    16  
    17  func NewParallelogram(box *geo.Box) Shape {
    18  	shape := shapeParallelogram{
    19  		baseShape: &baseShape{
    20  			Type: PARALLELOGRAM_TYPE,
    21  			Box:  box,
    22  		},
    23  	}
    24  	shape.FullShape = go2.Pointer(Shape(shape))
    25  	return shape
    26  }
    27  
    28  func (s shapeParallelogram) GetInnerBox() *geo.Box {
    29  	tl := s.Box.TopLeft.Copy()
    30  	width := s.Box.Width - 2*parallelWedgeWidth
    31  	tl.X += parallelWedgeWidth
    32  	return geo.NewBox(tl, width, s.Box.Height)
    33  }
    34  
    35  func parallelogramPath(box *geo.Box) *svg.SvgPathContext {
    36  	wedgeWidth := parallelWedgeWidth
    37  	// Note: box width should always be larger than parallelWedgeWidth
    38  	// this just handles after collapsing into a line
    39  	if box.Width <= wedgeWidth {
    40  		wedgeWidth = box.Width / 2.0
    41  	}
    42  	pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
    43  	pc.StartAt(pc.Absolute(wedgeWidth, 0))
    44  	pc.L(false, box.Width, 0)
    45  	pc.L(false, box.Width-wedgeWidth, box.Height)
    46  	pc.L(false, 0, box.Height)
    47  	pc.L(false, 0, box.Height)
    48  	pc.Z()
    49  	return pc
    50  }
    51  
    52  func (s shapeParallelogram) Perimeter() []geo.Intersectable {
    53  	return parallelogramPath(s.Box).Path
    54  }
    55  
    56  func (s shapeParallelogram) GetSVGPathData() []string {
    57  	return []string{
    58  		parallelogramPath(s.Box).PathData(),
    59  	}
    60  }
    61  
    62  func (s shapeParallelogram) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
    63  	totalWidth := width + paddingX + parallelWedgeWidth*2
    64  	return math.Ceil(totalWidth), math.Ceil(height + paddingY)
    65  }
    66  

View as plain text