The correct data structure for storing forms and spatial relationships between them

I'm a BEGINNER of Python and I really need help.

I have a (working) script that retrieves simple shapes in hand-made images. The next step is to determine the spatial relationships between the shapes and the data structure that contain all this information. I know how to determine the spatial relationships between shapes, for example, inside, side by side, above and below. However, I need a data structure to contain all this information: the coordinates of the edges (x, y) and the relation to other shapes. I was thinking about a hierarchical view, something like this picture:

enter image description here

Each form contains information.

Shape = (x0, y0, ..., xn, yn), position(Shape)

Where

  • (x0, y0, ..., xn, yn) - coordinates of the edges of the form
  • position (Shape) - the ratio of the shape to another.

For instance:

S1: (x0, y0, x1, y1, x2, y2, x3, y3), below(T1)
T1: (x0, y0, x1, y1, x2, y2), above(S1)
C1: (radius), inside(S1), above(C2)
C2: (radius), inside(S1), below(C1)

python? (), , . ? . , ? , .

+3
2

, , Python Natworkx, @Cilyan. ( ), , , . , , , :

enter image description here

:

:

[('3-gon', {'points': array([[250, 181],
                             [133,  43],
                             [ 21, 188]]), 
            'center': (139, 135)}), 
 ('4-gon', {'points': array([[251, 184],
                             [ 22, 192],
                             [ 41, 350],
                             [244, 346]]), 
            'center': (139, 265)}), 
 ('5-gon', {'points': array([[131,  37],
                             [ 11, 192],
                             [ 37, 354],
                             [247, 350],
                             [256, 182]]), 
            'center': (138, 223)})]

:

[('3-gon', '5-gon', {'relation': 'inside'}), 
 ('4-gon', '3-gon', {'relation': 'below'}), 
 ('4-gon', '5-gon', {'relation': 'inside'})]

, ! .

0

Python.

. , :

class Point(object):
    """ Define a point at X, Y """
    def __init__(self, x=0, y=0):
        self.x=x
        self.y=y

:

class Circle(object):
    def __init__(self, x=0, y=0, radius=1):
        """ Define a circle centered at X, Y with radius """
        self.x=x
        self.y=y
        self.radius=radius

, :

>>> c1=Circle()      # default of x=0, y=0, radius=1
>>> p1=Point(5,6)    # a point at x=5, y=6

? :

>>> ((p1.x-c1.x)**2 + (p1.y - c1.y)**2)**0.5<c1.radius
False

, . , Circle:

class Circle(object):
    def __init__(self, x=0, y=0, radius=1):
        self.x=x
        self.y=y
        self.radius=radius

    def point_in(self, p):
        return ((p.x-self.x)**2 + (p.y - self.y)**2)**0.5<self.radius

:

>>> c1.point_in(Point(5,5))
False
>>> c1.point_in(Point(.5,.5))
True

"" ""? , "" , Point.y > Circle.y. Circle:

def above(self, p):
    return ((p.x-self.x)**2 + (p.y - self.y)**2)**0.5 > self.radius and p.y>self.y 

:

>>> c1.above(Point(5,5)) 
True

, , , . , . ( , ? ! . OpenGL, ...)

, "" "" , , . , , .

+3

All Articles