-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_block_string.pyx
66 lines (47 loc) · 1.52 KB
/
class_block_string.pyx
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
65
66
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 3 17:33:17 2019
@author: arwen
"""
cdef class CBlock(object):
cdef readonly double left
cdef readonly double right
cdef readonly double top
cdef readonly double bottom
cdef readonly str text
def __init__(self,
double left,
double right,
double top,
double bottom,
str text
):
self.left = left
self.right = right
self.top = top
self.bottom = bottom
self.text = text
@property
def width(self):
return self.right - self.left
@property
def height(self):
return self.bottom - self.top
cpdef double cwidth(self): # callable from python AND c
return self.right - self.left
cdef double area(self):
return (self.right - self.left) * (self.bottom - self.top)
def print_area(self):
print( self.area() )
cpdef bint is_left(CBlock block1, CBlock block2):
return block1.left <= block2.left
cpdef bint is_above(CBlock block1, CBlock block2):
return block1.top <= block2.top
def sorted_extract_above_and_left(CBlock block, all_blocks):
cdef CBlock b
result = []
for b in all_blocks:
if is_above(b, block) and is_left(b, block):
result.append(b)
result = sorted(result, key = lambda b:b.left)
return result