forked from rodrigo-brito/gocity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.go
64 lines (50 loc) · 1.21 KB
/
position.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package model
import (
"math"
)
type Position struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}
const defaultMargin = 1
type generator struct {
numberNodes int
margin int
dimension int
xReference float64
yReference float64
currentIndex int
maxWidth float64
maxHeight float64
bounds Position
}
func NewGenerator(numberNodes int) *generator {
generator := &generator{
numberNodes: numberNodes,
dimension: int(math.Ceil(math.Sqrt(float64(numberNodes)))),
}
return generator
}
func (g *generator) GetBounds() Position {
return Position{
X: g.maxWidth + defaultMargin,
Y: g.maxHeight + defaultMargin,
}
}
func (g *generator) NextPosition(width, height float64) Position {
g.currentIndex++
if g.currentIndex > g.dimension && g.yReference+height >= g.maxWidth {
g.currentIndex = 0
g.yReference = 0
g.xReference = g.maxWidth + defaultMargin
}
position := Position{X: g.xReference + (width+defaultMargin)/2, Y: g.yReference + (height+defaultMargin)/2}
if g.xReference+width > g.maxWidth {
g.maxWidth = g.xReference + width
}
if g.yReference+height > g.maxHeight {
g.maxHeight = g.yReference + height
}
g.yReference += height + defaultMargin
return position
}