Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- def calc_bay_pos(
- bay_no,
- rows,
- cols,
- bay_start_no=0,
- row_start_no=0,
- col_start_no=0,
- horizontal=True,
- to_up=True,
- to_left=True,
- ):
- Row = namedtuple("Bay", "row col")
- bay_no -= bay_start_no
- if bay_no < 0:
- raise ValueError
- if bay_no >= rows * cols:
- raise ValueError
- if horizontal:
- col = bay_no % cols
- row = bay_no // cols
- else:
- row = bay_no % rows
- col = bay_no // rows
- if not to_up:
- row = rows - row - 1
- if not to_left:
- col = cols - col - 1
- return Row(row + row_start_no, col + col_start_no)
- def make_matrix(rows, cols):
- return [[0 for _ in range(cols)] for _ in range(rows)]
- def print_matrix(result):
- for row in result:
- print(" | ".join(map("{:^3d}".format, row)))
- rows = 3
- cols = 5
- mat = make_matrix(rows, cols)
- for horizontal in (True, False):
- for up in (True, False):
- for left in (True, False):
- print("=" * 25)
- print("Horizontal" if horizontal else "Vertical", end=" ")
- print("- Up" if up else "- Down", end=" ")
- print("- Left" if left else "- Right")
- for bay in range(rows * cols):
- pos = calc_bay_pos(bay, rows, cols, horizontal=horizontal, to_up=up, to_left=left)
- mat[pos.row][pos.col] = bay
- print_matrix(mat)
- print()
- def calculate_xy_position(row, col, row_shift, col_shift, row_offset, col_offset):
- Position = namedtuple("Position", "x y")
- return Position(row * row_shift + row_offset, col * col_shift + col_offset)
- # calculate_xy_position(1, 0, 100, 100, 50, 50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement