if storing int and casting float64 each use

This commit is contained in:
Gavin Nishizawa 2023-04-11 11:20:21 -07:00
parent 42993b09f0
commit d835ef19a9
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 27 additions and 27 deletions

View file

@ -20,8 +20,8 @@ type gridDiagram struct {
width float64 width float64
height float64 height float64
gapRows float64 gapRows int
gapColumns float64 gapColumns int
} }
func newGridDiagram(root *d2graph.Object) *gridDiagram { func newGridDiagram(root *d2graph.Object) *gridDiagram {
@ -77,14 +77,14 @@ func newGridDiagram(root *d2graph.Object) *gridDiagram {
// grid gap sets both, but can be overridden // grid gap sets both, but can be overridden
if root.Attributes.GridGap != nil { if root.Attributes.GridGap != nil {
gd.gapRows, _ = strconv.ParseFloat(root.Attributes.GridGap.Value, 64) gd.gapRows, _ = strconv.Atoi(root.Attributes.GridGap.Value)
gd.gapColumns = gd.gapRows gd.gapColumns = gd.gapRows
} }
if root.Attributes.GridGapRows != nil { if root.Attributes.GridGapRows != nil {
gd.gapRows, _ = strconv.ParseFloat(root.Attributes.GridGapRows.Value, 64) gd.gapRows, _ = strconv.Atoi(root.Attributes.GridGapRows.Value)
} }
if root.Attributes.GridGapColumns != nil { if root.Attributes.GridGapColumns != nil {
gd.gapColumns, _ = strconv.ParseFloat(root.Attributes.GridGapColumns.Value, 64) gd.gapColumns, _ = strconv.Atoi(root.Attributes.GridGapColumns.Value)
} }
return &gd return &gd

View file

@ -13,7 +13,7 @@ import (
const ( const (
CONTAINER_PADDING = 60 CONTAINER_PADDING = 60
DEFAULT_GAP = 40. DEFAULT_GAP = 40
) )
// Layout runs the grid layout on containers with rows/columns // Layout runs the grid layout on containers with rows/columns
@ -188,10 +188,10 @@ func (gd *gridDiagram) layoutEvenly(g *d2graph.Graph, obj *d2graph.Object) {
o.Width = colWidths[j] o.Width = colWidths[j]
o.Height = rowHeights[i] o.Height = rowHeights[i]
o.TopLeft = cursor.Copy() o.TopLeft = cursor.Copy()
cursor.X += o.Width + gd.gapColumns cursor.X += o.Width + float64(gd.gapColumns)
} }
cursor.X = 0 cursor.X = 0
cursor.Y += rowHeights[i] + gd.gapRows cursor.Y += rowHeights[i] + float64(gd.gapRows)
} }
} else { } else {
for j := 0; j < gd.columns; j++ { for j := 0; j < gd.columns; j++ {
@ -203,22 +203,22 @@ func (gd *gridDiagram) layoutEvenly(g *d2graph.Graph, obj *d2graph.Object) {
o.Width = colWidths[j] o.Width = colWidths[j]
o.Height = rowHeights[i] o.Height = rowHeights[i]
o.TopLeft = cursor.Copy() o.TopLeft = cursor.Copy()
cursor.Y += o.Height + gd.gapRows cursor.Y += o.Height + float64(gd.gapRows)
} }
cursor.X += colWidths[j] + gd.gapColumns cursor.X += colWidths[j] + float64(gd.gapColumns)
cursor.Y = 0 cursor.Y = 0
} }
} }
var totalWidth, totalHeight float64 var totalWidth, totalHeight float64
for _, w := range colWidths { for _, w := range colWidths {
totalWidth += w + gd.gapColumns totalWidth += w + float64(gd.gapColumns)
} }
for _, h := range rowHeights { for _, h := range rowHeights {
totalHeight += h + gd.gapRows totalHeight += h + float64(gd.gapRows)
} }
totalWidth -= gd.gapColumns totalWidth -= float64(gd.gapColumns)
totalHeight -= gd.gapRows totalHeight -= float64(gd.gapRows)
gd.width = totalWidth gd.width = totalWidth
gd.height = totalHeight gd.height = totalHeight
} }
@ -245,8 +245,8 @@ func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
totalWidth += o.Width totalWidth += o.Width
totalHeight += o.Height totalHeight += o.Height
} }
totalWidth += gd.gapColumns * float64(len(gd.objects)-gd.rows) totalWidth += float64(gd.gapColumns * (len(gd.objects) - gd.rows))
totalHeight += gd.gapRows * float64(len(gd.objects)-gd.columns) totalHeight += float64(gd.gapRows * (len(gd.objects) - gd.columns))
var layout [][]*d2graph.Object var layout [][]*d2graph.Object
if gd.rowDirected { if gd.rowDirected {
@ -277,10 +277,10 @@ func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
rowHeight := 0. rowHeight := 0.
for _, o := range row { for _, o := range row {
o.TopLeft = cursor.Copy() o.TopLeft = cursor.Copy()
cursor.X += o.Width + gd.gapColumns cursor.X += o.Width + float64(gd.gapColumns)
rowHeight = math.Max(rowHeight, o.Height) rowHeight = math.Max(rowHeight, o.Height)
} }
rowWidth := cursor.X - gd.gapColumns rowWidth := cursor.X - float64(gd.gapColumns)
rowWidths = append(rowWidths, rowWidth) rowWidths = append(rowWidths, rowWidth)
maxX = math.Max(maxX, rowWidth) maxX = math.Max(maxX, rowWidth)
@ -291,9 +291,9 @@ func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
// new row // new row
cursor.X = 0 cursor.X = 0
cursor.Y += rowHeight + gd.gapRows cursor.Y += rowHeight + float64(gd.gapRows)
} }
maxY = cursor.Y - gd.gapRows maxY = cursor.Y - float64(gd.gapRows)
// then expand thinnest objects to make each row the same width // then expand thinnest objects to make each row the same width
// . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┬ maxHeight(A,B,C) // . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┬ maxHeight(A,B,C)
@ -371,10 +371,10 @@ func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
colWidth := 0. colWidth := 0.
for _, o := range column { for _, o := range column {
o.TopLeft = cursor.Copy() o.TopLeft = cursor.Copy()
cursor.Y += o.Height + gd.gapRows cursor.Y += o.Height + float64(gd.gapRows)
colWidth = math.Max(colWidth, o.Width) colWidth = math.Max(colWidth, o.Width)
} }
colHeight := cursor.Y - gd.gapRows colHeight := cursor.Y - float64(gd.gapRows)
colHeights = append(colHeights, colHeight) colHeights = append(colHeights, colHeight)
maxY = math.Max(maxY, colHeight) maxY = math.Max(maxY, colHeight)
// set all objects in column to the same width // set all objects in column to the same width
@ -384,9 +384,9 @@ func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
// new column // new column
cursor.Y = 0 cursor.Y = 0
cursor.X += colWidth + gd.gapColumns cursor.X += colWidth + float64(gd.gapColumns)
} }
maxX = cursor.X - gd.gapColumns maxX = cursor.X - float64(gd.gapColumns)
// then expand shortest objects to make each column the same height // then expand shortest objects to make each column the same height
// . ├maxWidth(A,B)─┤ ├maxW(C,D)─┤ ├maxWidth(E)──────┤ // . ├maxWidth(A,B)─┤ ├maxW(C,D)─┤ ├maxWidth(E)──────┤
// . ┌A─────────────┐ ┌C─────────┐ ┌E────────────────┐ // . ┌A─────────────┐ ┌C─────────┐ ┌E────────────────┐
@ -526,15 +526,15 @@ func genLayout(objects []*d2graph.Object, cutIndices []int) [][]*d2graph.Object
return layout return layout
} }
func getDistToTarget(layout [][]*d2graph.Object, targetSize, gapRows, gapColumns float64, columns bool) float64 { func getDistToTarget(layout [][]*d2graph.Object, targetSize float64, gapRows, gapColumns int, columns bool) float64 {
totalDelta := 0. totalDelta := 0.
for _, row := range layout { for _, row := range layout {
rowSize := 0. rowSize := 0.
for _, o := range row { for _, o := range row {
if columns { if columns {
rowSize += o.Height + gapRows rowSize += o.Height + float64(gapRows)
} else { } else {
rowSize += o.Width + gapColumns rowSize += o.Width + float64(gapColumns)
} }
} }
totalDelta += math.Abs(rowSize - targetSize) totalDelta += math.Abs(rowSize - targetSize)