Adjacency list to model relationships between relatively-positioned shapes?
I'm trying to model a composite object that consists of one or more shapes. For simplicity, I'll assume the shapes are all rectangles. A composite might look like this 开发者_运维百科(forgive my poor ASCII art):
+---+-------+---+ | | 2 | 5 | | +-------+---+ | 1 | 3 | | | +-------+ 6 | | | 4 | | +---+-------+---+
Wikipedia pointed me to graph theory, which I just barely remember from college, and it seems like an adjacency list would be a good way to model the relationships between all these shapes.
My question is, can I indicate left, right, top, and bottom relationships in an adjacency list? It isn't enough to say 1 is adjacent to 2; I need to say 1 is left of 2 (and 3 is above 4, etc.).
Sure. Where your normal adjacency list might look like:
vertex {
neighbours: // list of neighbours
}
To include relative positions, each vertex could have an adjacency list for each direction:
vertex {
left: ...
right: ...
up: ...
down: ...
}
So:
3 {
left: [1]
right: [6]
up: [2]
down: [4]
}
and
1 {
left: []
right: [2,3,4]
up: []
down: []
}
精彩评论