Split some functionality out

This commit is contained in:
XANTRONIX Development 2023-12-29 23:24:35 -05:00
parent f97ae0bdaf
commit 069a492180
3 changed files with 122 additions and 118 deletions

View file

@ -3,124 +3,6 @@ import cairo
from typing import Iterable
class Path():
__slots__ = 'commands',
def __init__(self, commands: Iterable):
self.commands = commands
def horiz_to(self, cr: cairo.Context, x2: float):
_, y = cr.get_current_point()
return cr.line_to(x2, y)
def rel_horiz_to(self, cr: cairo.Context, x2: float):
x, y = cr.get_current_point()
return cr.line_to(x + x2, y)
def vert_to(self, cr: cairo.Context, y2: float):
x, _ = cr.get_current_point()
return cr.line_to(x, y2)
def rel_vert_to(self, cr: cairo.Context, y2: float):
x, y = cr.get_current_point()
return cr.line_to(x, y + y2)
def draw(self, cr: cairo.Context):
last = None
for item in self.commands:
command, args = item
if command == 'M':
for i in range(0, len(args)):
if i == 0:
cr.move_to(*args[i])
else:
cr.line_to(*args[i])
elif command == 'm':
for i in range(0, len(args)):
if i == 0:
cr.rel_move_to(*args[i])
else:
cr.rel_line_to(*args[i])
elif command == 'L':
for arg in args:
cr.line_to(*arg)
elif command == 'l':
for arg in args:
cr.rel_line_to(*arg)
elif command == 'H':
for arg in args:
self.horiz_to(cr, arg)
elif command == 'h':
for arg in args:
self.rel_horiz_to(cr, arg)
elif command == 'V':
for arg in args:
self.vert_to(cr, arg)
elif command == 'v':
for arg in args:
self.rel_vert_to(cr, arg)
elif command == 'C':
for arg in args:
cr.curve_to(*arg)
elif command == 'c':
for arg in args:
cr.rel_curve_to(*arg)
elif command == 'Z' or command == 'z':
cr.close_path()
last = command
class HexagonPattern(Path):
COMMANDS = [
['M', [[23.246093, 0],
[ 0, 13.382812]]],
['V', [44.689453]],
['L', [[0.0839844, 44.640625],
[24.033203, 58.505859],
[24.001953, 86.332031],
[22.841796, 87]]],
['h', [4.478516]],
['L', [[26.068359, 86.275391],
[26.099609, 58.449219],
[50.083984, 44.640625],
[74.033203, 58.505859],
[74.001953, 86.332031],
[72.841796, 87]]],
['h', [4.478516]],
['L', [[ 76.068359, 86.275391],
[ 76.099609, 58.449219],
[100, 44.689453]]],
['V', [13.365234]],
['L', [[76.919921, 0]]],
['H', [73.246093]],
['L', [[50.015625, 13.373047],
[26.919921, 0]]],
['Z', []],
['M', [[25.083984, 1.25],
[49.033203, 15.115234],
[49.001953, 42.941406],
[25.017578, 56.75],
[ 1.0019531, 42.845703]]],
['l', [[ 0.033203, -27.75]]],
['z', []],
['m', [[50, 0],
[24.017576, 13.904297],
[-0.0352, 27.75]]],
['L', [[75.017578, 56.75],
[51.068359, 42.884766],
[51.099609, 15.058594]]],
['Z', []]
]
def __init__(self):
super().__init__(self.COMMANDS)
class Gauge():
pass

76
py/hexagram/path.py Normal file
View file

@ -0,0 +1,76 @@
from typing import Iterable
import cairo
class Path():
__slots__ = 'commands',
def __init__(self, commands: Iterable):
self.commands = commands
def horiz_to(self, cr: cairo.Context, x2: float):
_, y = cr.get_current_point()
return cr.line_to(x2, y)
def rel_horiz_to(self, cr: cairo.Context, x2: float):
x, y = cr.get_current_point()
return cr.line_to(x + x2, y)
def vert_to(self, cr: cairo.Context, y2: float):
x, _ = cr.get_current_point()
return cr.line_to(x, y2)
def rel_vert_to(self, cr: cairo.Context, y2: float):
x, y = cr.get_current_point()
return cr.line_to(x, y + y2)
def draw(self, cr: cairo.Context):
last = None
for item in self.commands:
command, args = item
if command == 'M':
for i in range(0, len(args)):
if i == 0:
cr.move_to(*args[i])
else:
cr.line_to(*args[i])
elif command == 'm':
for i in range(0, len(args)):
if i == 0:
cr.rel_move_to(*args[i])
else:
cr.rel_line_to(*args[i])
elif command == 'L':
for arg in args:
cr.line_to(*arg)
elif command == 'l':
for arg in args:
cr.rel_line_to(*arg)
elif command == 'H':
for arg in args:
self.horiz_to(cr, arg)
elif command == 'h':
for arg in args:
self.rel_horiz_to(cr, arg)
elif command == 'V':
for arg in args:
self.vert_to(cr, arg)
elif command == 'v':
for arg in args:
self.rel_vert_to(cr, arg)
elif command == 'C':
for arg in args:
cr.curve_to(*arg)
elif command == 'c':
for arg in args:
cr.rel_curve_to(*arg)
elif command == 'Z' or command == 'z':
cr.close_path()
last = command

46
py/hexagram/pattern.py Normal file
View file

@ -0,0 +1,46 @@
from hexagram.path import Path
class HexagonPattern(Path):
COMMANDS = [
['M', [[23.246093, 0],
[ 0, 13.382812]]],
['V', [44.689453]],
['L', [[0.0839844, 44.640625],
[24.033203, 58.505859],
[24.001953, 86.332031],
[22.841796, 87]]],
['h', [4.478516]],
['L', [[26.068359, 86.275391],
[26.099609, 58.449219],
[50.083984, 44.640625],
[74.033203, 58.505859],
[74.001953, 86.332031],
[72.841796, 87]]],
['h', [4.478516]],
['L', [[ 76.068359, 86.275391],
[ 76.099609, 58.449219],
[100, 44.689453]]],
['V', [13.365234]],
['L', [[76.919921, 0]]],
['H', [73.246093]],
['L', [[50.015625, 13.373047],
[26.919921, 0]]],
['Z', []],
['M', [[25.083984, 1.25],
[49.033203, 15.115234],
[49.001953, 42.941406],
[25.017578, 56.75],
[ 1.0019531, 42.845703]]],
['l', [[ 0.033203, -27.75]]],
['z', []],
['m', [[50, 0],
[24.017576, 13.904297],
[-0.0352, 27.75]]],
['L', [[75.017578, 56.75],
[51.068359, 42.884766],
[51.099609, 15.058594]]],
['Z', []]
]
def __init__(self):
super().__init__(self.COMMANDS)