diff --git a/py/hexagram/cluster.py b/py/hexagram/cluster.py index 09c8d31..0c91f24 100644 --- a/py/hexagram/cluster.py +++ b/py/hexagram/cluster.py @@ -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 diff --git a/py/hexagram/path.py b/py/hexagram/path.py new file mode 100644 index 0000000..1dd70ba --- /dev/null +++ b/py/hexagram/path.py @@ -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 diff --git a/py/hexagram/pattern.py b/py/hexagram/pattern.py new file mode 100644 index 0000000..f245b76 --- /dev/null +++ b/py/hexagram/pattern.py @@ -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)