package main import ( "fmt" "math" ) const ( numDimensions = 3 maxItems = 9 ) type Interval struct{ start, end int } type Cuboid struct { name string spaceOccupied [3]Interval } var cuboids [maxItems]Cuboid var nCuboids int func addCuboid(name string, vals ...int) { if nCuboids >= maxItems { return } cuboids[nCuboids] = newCuboid(name, vals) nCuboids++ } func main() { addCuboid("jumpyFlower", 0, 2, 1, 2, 0, 1) addCuboid("danceyFlower", 0, 2, 0, 1, -1, 7) addCuboid("wigglyFlower", 3, 4, -1, 0, 4, 6) addCuboid("shinyFlower", 0, 2, 3, 4, 0, 9) addCuboid("kitMon", 0, 1, 1, 2, 0, 1) addCuboid("pupMon", 2, 3, 1, 2, 0, 1) addCuboid("birdMon", 1, 2, 2, 3, 8, 9) addCuboid("fishMon", 3, 5, 2, 3, -1, -2) addCuboid("babyMon", 2, 4, 2, 4, 0, 1) fmt.Println(cuboids) for index1, cuboid1 := range cuboids { for index2, cuboid2 := range cuboids { if index1 < index2 && cuboid1.overlaps(cuboid2) { fmt.Printf("%12s overlaps %12s \n", cuboid1.name, cuboid2.name) } } } } func newCuboid(itemName string, intervalInfo []int) Cuboid { return Cuboid{itemName, convertToArrayOfStruct(intervalInfo)} } func convertToArrayOfStruct(intervals []int) [3]Interval { var ivArray [3]Interval for ii := 0; ii < numDimensions; ii++ { ivArray[ii] = Interval{intervals[ii*2], intervals[ii*2+1]} } return ivArray } func (c1 Cuboid) overlaps(c2 Cuboid) bool { for dimNum, dimLoHi := range c1.spaceOccupied { fmt.Printf("%12s %12s %d %d %d: returning true\n", c1.name, c2.name, dimNum, dimLoHi.start, dimLoHi.end) return true } return true } func (ivl Interval) ivlIntersects(iv2 Interval) bool { return math.Max(float64(ivl.start), float64(iv2.start)) <= math.Min(float64(ivl.end), float64(iv2.end)) }