...
1 package shape
2
3 import (
4 "math"
5
6 "oss.terrastruct.com/d2/lib/geo"
7 "oss.terrastruct.com/util-go/go2"
8 )
9
10 type shapeCircle struct {
11 *baseShape
12 }
13
14 func NewCircle(box *geo.Box) Shape {
15 shape := shapeCircle{
16 baseShape: &baseShape{
17 Type: CIRCLE_TYPE,
18 Box: box,
19 },
20 }
21 shape.FullShape = go2.Pointer(Shape(shape))
22 return shape
23 }
24
25 func (s shapeCircle) GetInnerBox() *geo.Box {
26 width := s.Box.Width
27 height := s.Box.Height
28 insideTL := s.GetInsidePlacement(width, height, 0, 0)
29 tl := s.Box.TopLeft.Copy()
30 width -= 2 * (insideTL.X - tl.X)
31 height -= 2 * (insideTL.Y - tl.Y)
32 return geo.NewBox(&insideTL, width, height)
33 }
34
35 func (s shapeCircle) AspectRatio1() bool {
36 return true
37 }
38
39 func (s shapeCircle) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
40 length := math.Max(width+paddingX, height+paddingY)
41 diameter := math.Ceil(math.Sqrt2 * length)
42 return diameter, diameter
43 }
44
45 func (s shapeCircle) GetInsidePlacement(width, height, paddingX, paddingY float64) geo.Point {
46 project45 := 1 / math.Sqrt2
47 r := s.Box.Width / 2
48
49 return geo.Point{
50 X: s.Box.TopLeft.X + math.Ceil(r-project45*(r-paddingX/2)),
51 Y: s.Box.TopLeft.Y + math.Ceil(r-project45*(r-paddingY/2)),
52 }
53 }
54
55 func (s shapeCircle) Perimeter() []geo.Intersectable {
56 return []geo.Intersectable{geo.NewEllipse(s.Box.Center(), s.Box.Width/2, s.Box.Height/2)}
57 }
58
59 func (s shapeCircle) GetDefaultPadding() (paddingX, paddingY float64) {
60 return defaultPadding / math.Sqrt2, defaultPadding / math.Sqrt2
61 }
62
View as plain text