def pens_updown_to_papr(lst): '''Converts all PU((x1, y1, x2, y2) and PD(x1, y1, x2, y2) found in `lst` into (PU( ), PA(x1, y1, x2, y2)) pair sequences. The function removes the coordinates from PU and PD and places them in PR or PA, whatever was last found in lst prior to a PU or PD.''' if not isinstance(lst, (list, tuple)): raise TypeError('`lst` argument must be a list or tuple.') result = [ ] last_penplot = None for e in lst: if isinstance(e, (PU, PD)): if len(e.xy) > 0: if last_penplot is None: msg = "*** WARNING: %s with coordinates found without prior PA or PR. PA assumed." % e print(msg) last_penplot = PA( ) last_penplot.xy = e.xy ec = copy.copy(e) ec.xy = None result.append(ec) result.append(copy.copy(last_penplot)) else: result.append(e) else: if isinstance(e, PR): last_penplot = PR( ) elif isinstance(e, PA): last_penplot = PA( ) result.append(e) return result
def convert_coordinates_to_hpgl_absolute_path(coords): """Converts an iterator of lists of coordinates e.g., [<x1, y1>, <x2, y2>, <x3, y3>, ...] into a list of PA, PD and PU HPGL commands. """ if not isinstance(coords, (list, tuple, CoordinateArray)): raise TypeError("`coords` must be a list of coordinates or CoordinateArray.") coords = [list(c) for c in coords] result = [] result.append(PU()) result.append(PA([coords[0]])) result.append(PD()) ## this denies the possibility of paths with one coord. # result.append(PA(coords[1:])) result.append(PA(coords)) result.append(PU()) return result
def pr_to_pa(arg, starting_position=None): """Converts a given PR into PA starting at `starting_position`. - `arg` is a PR object. - `starting_position` is a coordinate pair. Default is (0, 0). """ if not isinstance(arg, PR): raise TypeError("`arg` must be of type PR") if len(arg.xy) == 0: return PA() last_abs = starting_position or Coordinate(0, 0) last_abs = Coordinate(*last_abs) # abs_coords = [last_abs] abs_coords = [] for p in arg.xy: last_abs = last_abs + p abs_coords.append(last_abs) return PA(abs_coords)
def pens_updown_to_papr(lst): """Converts all PU((x1, y1, x2, y2) and PD(x1, y1, x2, y2) found in `lst` into (PU( ), PA(x1, y1, x2, y2)) pair sequences. The function removes the coordinates from PU and PD and places them in PR or PA, whatever was last found in lst prior to a PU or PD.""" if not isinstance(lst, (list, tuple)): raise TypeError("`lst` argument must be a list or tuple.") result = [] last_penplot = None for e in lst: if isinstance(e, (PU, PD)): if len(e.xy) > 0: if last_penplot is None: msg = ( "*** WARNING: %s with coordinates found without prior PA or PR. PA assumed." % e) print(msg) last_penplot = PA() last_penplot.xy = e.xy ec = copy.copy(e) ec.xy = None result.append(ec) result.append(copy.copy(last_penplot)) else: result.append(e) else: if isinstance(e, PR): last_penplot = PR() elif isinstance(e, PA): last_penplot = PA() result.append(e) return result
def test_get_bounding_box_01(): '''The function can take a list of _HPGLPrimitives.''' t = hpgltools.get_bounding_box([PA([(1, 2), (3, 4)])]) assert t == (Coordinate(1, 2), Coordinate(3, 4))
def test_get_bounding_box_02(): '''The function can take a list of _HPGLPrimitives.''' a = [PA([(1, 2), (3, 4)]), EA((10, -2))] t = hpgltools.get_bounding_box(a) assert t == (Coordinate(1, -2), Coordinate(10, 4))
def test_get_centroid_01(): g = [PA([(0, 0), (10, 10), (0, 10), (10, 0)])] t = hpgltools.get_centroid(g) assert t == Coordinate(5, 5)
def test_PA__eq__01(): assert PA([(1, 2), (3, 4)]) == PA([(1, 2), (3, 4)])
def test_PA_01(): t = PA() assert t.xy == CoordinateArray([]) assert isinstance(t.xy, CoordinateArray) assert t.format == b"PA;"
def pens_updown_to_papr(lst): '''Converts all PU((x1, y1, x2, y2) and PD(x1, y1, x2, y2) found in `lst` into (PU( ), PA(x1, y1, x2, y2)) pair sequences. The function removes the coordinates from PU and PD and places them in PR or PA, whatever was last found in lst prior to a PU or PD.''' if not isinstance(lst, (list, tuple)): raise TypeError('`lst` argument must be a list or tuple.') result = [] last_move = None pen_down = False pen_up = True for e in lst: if isinstance(e, (PU, PD)): if len(e.xy) > 0: if last_move is None: msg = "*** WARNING: %s with coordinates found without prior PA or PR. PA assumed." % e print(msg) last_move = PA() new_move = None if isinstance(last_move, PA): new_move = PA() elif isinstance(last_move, PR): new_move = PR() new_move.xy = e.xy up_down_command = None if isinstance(e, PU): if pen_down: up_down_command = PU() result.append(up_down_command) pen_up = True pen_down = False elif isinstance(e, PD): if pen_up: up_down_command = PD() result.append(up_down_command) pen_down = True pen_up = False result.append(new_move) last_move = new_move else: if isinstance(e, PU): if pen_down: result.append(e) pen_up = True elif isinstance(e, PD): if pen_up: result.append(e) pen_down = True else: if isinstance(e, PR): last_move = PR() elif isinstance(e, PA): last_move = PA() result.append(e) return result
def pens_updown_to_papr(lst): '''Converts all PU((x1, y1, x2, y2) and PD(x1, y1, x2, y2) found in `lst` into (PU( ), PA(x1, y1, x2, y2)) pair sequences. The function removes the coordinates from PU and PD and places them in PR or PA, whatever was last found in lst prior to a PU or PD.''' if not isinstance(lst, (list, tuple)): raise TypeError('`lst` argument must be a list or tuple.') result = [ ] last_move = None pen_down = False pen_up = True for e in lst: if isinstance(e, (PU, PD)): if len(e.xy) > 0: if last_move is None: msg = "*** WARNING: %s with coordinates found without prior PA or PR. PA assumed." % e print(msg) last_move = PA( ) new_move = None if isinstance(last_move, PA): new_move = PA() elif isinstance(last_move, PR): new_move = PR() new_move.xy = e.xy up_down_command = None if isinstance(e, PU): if pen_down: up_down_command = PU( ) result.append(up_down_command) pen_up = True pen_down = False elif isinstance(e, PD): if pen_up: up_down_command = PD( ) result.append(up_down_command) pen_down = True pen_up = False result.append(new_move) last_move = new_move else: if isinstance(e, PU): if pen_down: result.append(e) pen_up = True elif isinstance(e, PD): if pen_up: result.append(e) pen_down = True else: if isinstance(e, PR): last_move = PR( ) elif isinstance(e, PA): last_move = PA( ) result.append(e) return result