def line(x1, y1, x2, y2): """Returns the coords of the line between (x1, y1), (x2, y2) :param x1: x coordinate of the startpoint :param y1: y coordinate of the startpoint :param x2: x coordinate of the endpoint :param y2: y coordinate of the endpoint """ x1 = normalize(x1) y1 = normalize(y1) x2 = normalize(x2) y2 = normalize(y2) xdiff = max(x1, x2) - min(x1, x2) ydiff = max(y1, y2) - min(y1, y2) xdir = 1 if x1 <= x2 else -1 ydir = 1 if y1 <= y2 else -1 r = max(xdiff, ydiff) for i in range(r+1): x = x1 y = y1 if ydiff: y += (float(i) * ydiff) / r * ydir if xdiff: x += (float(i) * xdiff) / r * xdir yield (x, y)
def set(self, x, y): """Set a pixel of the :class:`Canvas` object. :param x: x coordinate of the pixel :param y: y coordinate of the pixel """ x = normalize(x) y = normalize(y) col, row = get_pos(x, y) if type(self.chars[row][col]) != int: return self.chars[row][col] |= pixel_map[y % 4][x % 2]
def toggle(self, x, y): """Toggle a pixel of the :class:`Canvas` object. :param x: x coordinate of the pixel :param y: y coordinate of the pixel """ x = normalize(x) y = normalize(y) col, row = get_pos(x, y) if type(self.chars[row][col]) != int or self.chars[row][col] & pixel_map[y % 4][x % 2]: self.unset(x, y) else: self.set(x, y)
def unset(self, x, y): """Unset a pixel of the :class:`Canvas` object. :param x: x coordinate of the pixel :param y: y coordinate of the pixel """ x = normalize(x) y = normalize(y) col, row = get_pos(x, y) if type(self.chars[row][col]) == int: self.chars[row][col] &= ~pixel_map[y % 4][x % 2] if type(self.chars[row][col]) != int or self.chars[row][col] == 0: del(self.chars[row][col]) if not self.chars.get(row): del(self.chars[row])
def get(self, x, y): """Get the state of a pixel. Returns bool. :param x: x coordinate of the pixel :param y: y coordinate of the pixel """ x = normalize(x) y = normalize(y) dot_index = pixel_map[y % 4][x % 2] col, row = get_pos(x, y) char = self.chars.get(row, {}).get(col) if not char: return False if type(char) != int: return True return bool(char & dot_index)