Cubes in our model do overlap. To overlap, they need to have some shared volume, just touching is not enough. A component is a set of cubes that are overlapping. In other words, if we want to add a cube x to a component, we need to find a cube from the component, that overlaps with the cube x.
Write a function components that takes a list of cubes and as a result divides these cubes into components. The result will be [[Cube]], where each of inner lists represents a component (as was defined above). You can use function printIt to print each of these components on a separated line.
Here is my code that I was able to write, but this is not enough to solve the problem.
import Data.List (groupBy)
data Point = Point Int Int Int deriving (Eq, Show)
data Cube = Cube {start :: Point, size :: Int} deriving (Eq, Show)
sampleInput :: [Cube]
sampleInput = [
Cube {start = Point 0 0 0, size = 5},
Cube {start = Point 4 4 4, size = 5},
Cube {start = Point 8 8 8, size = 4},
Cube {start = Point 12 12 12, size = 2},
Cube {start = Point 13 13 13, size = 2},
Cube {start = Point 10 10 0, size = 2},
Cube {start = Point 9 9 0, size = 4}
]
printIt :: [[Cube]] -> IO ()
printIt components = putStr (concat [show component ++ "\n" | component <- components])
function :: Cube -> Cube -> Bool
function (Cube (Point a00 b00 c00) size0) (Cube (Point a10 b10 c10) size1) = xyz
where
a01 = a00 + (size0 - 1)
b01 = b00 + (size0 - 1)
c01 = c00 + (size0 - 1)
a11 = a10 + (size1 - 1)
b11 = b10 + (size1 - 1)
c11 = c10 + (size1 - 1)
xyz = (a00 <= a11 && a01 >= a10) && (b00 <= b11 && b01 >= b10) && (c00 <= c11 && c01 >= c10)
What algorithm needs to be written to make it all work? For the result to be like this:
*Main> printIt (components sampleInput)
[Cube {start = Point 0 0 0, size = 5},Cube {start = Point 4 4 4, size = 5},Cube {start = Point 8 8 8, size = 4}]
[Cube {start = Point 12 12 12, size = 2},Cube {start = Point 13 13 13, size = 2}]
[Cube {start = Point 10 10 0, size = 2},Cube {start = Point 9 9 0, size = 4}]
