CC calculation: compile - arnthor89/pygame GitHub Wiki
CC calculation: compile
Function
def compile(strings, black='X', white='.',xor='o'):
"""pygame.cursors.compile(strings, black, white,xor) -> data, mask
compile cursor strings into cursor data
This takes a set of strings with equal length and computes
the binary data for that cursor. The string widths must be
divisible by 8.
The black and white arguments are single letter strings that
tells which characters will represent black pixels, and which
characters represent white pixels. All other characters are
considered clear.
This returns a tuple containing the cursor data and cursor mask
data. Both these arguments are used when setting a cursor with
pygame.mouse.set_cursor().
"""
#first check for consistent lengths
size = len(strings[0]), len(strings)
if size[0] % 8 or size[1] % 8:
raise ValueError("cursor string sizes must be divisible by 8 %s" %
size)
for s in strings[1:]:
if len(s) != size[0]:
raise ValueError("Cursor strings are inconsistent lengths")
#create the data arrays.
#this could stand a little optimizing
maskdata = []
filldata = []
maskitem = fillitem = 0
step = 8
for s in strings:
for c in s:
maskitem = maskitem << 1
fillitem = fillitem << 1
step = step - 1
if c == black:
maskitem = maskitem | 1
fillitem = fillitem | 1
elif c == white:
maskitem = maskitem | 1
elif c == xor:
fillitem = fillitem | 1
if not step:
maskdata.append(maskitem)
filldata.append(fillitem)
maskitem = fillitem = 0
step = 8
return tuple(filldata), tuple(maskdata)
Control flow graph
def compile(strings, black='X', white='.',xor='o'):
#1
size = len(strings[0]), len(strings)
if size[0] % 8 or size[1] % 8:
#3
raise ValueError("cursor string sizes must be divisible by 8 %s" %
size)
#2
for s in strings[1:]:
#5
if len(s) != size[0]:
#7
raise ValueError("Cursor strings are inconsistent lengths")
#6
#4
maskdata = []
filldata = []
maskitem = fillitem = 0
step = 8
for s in strings:
#9
for c in s:
#11
maskitem = maskitem << 1
fillitem = fillitem << 1
step = step - 1
if c == black:
#13
maskitem = maskitem | 1
fillitem = fillitem | 1
elif c == white:
#14
maskitem = maskitem | 1
elif c == xor:
#15
fillitem = fillitem | 1
#12
if not step:
#17
maskdata.append(maskitem)
filldata.append(fillitem)
maskitem = fillitem = 0
step = 8
#16
#10
#8
return tuple(filldata), tuple(maskdata)
https://i.imgur.com/tt5c1Pv.png
Calculation
- Edges = 26
- Nodes = 17
- Connected components = 1
26 - 17 + 2*1 = 11