Last active
August 29, 2015 14:13
-
-
Save asarver/86bded06524f96175f39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pygame | |
| import sys50,50 | |
| class Coordinate: | |
| x = 0 | |
| y = 0 | |
| def __init__(self, x=0, y=0): | |
| self.x = x | |
| self.y = y | |
| class Canvas: | |
| width = 0 | |
| height = 0 | |
| window = [] | |
| def __init__(self, width=0, height=0): | |
| self.width = width | |
| self.height = height | |
| pygame.init() | |
| self.window = pygame.display.set_mode((height, width)) | |
| class Pen: | |
| currentPosition = Coordinate() | |
| cavas = Canvas() | |
| canWrite = False | |
| def __init__(self, width, height): | |
| self.canvas = Canvas(width, height) | |
| def draw(self, coords): | |
| for coord in coords: | |
| penIsOutside = self.IsSelfOutOfBounds() | |
| desiredIsOutside = self.IsDesiredOutOfBounds(coord) | |
| if penIsOutside and desiredIsOutside: | |
| self.raisePen() | |
| self.move(coord) | |
| elif penIsOutside: | |
| rise = self.currentPosition.y - coord.y | |
| run = self.currentPosition.x - coord.x | |
| if (run == 0): | |
| self.raisePen() | |
| self.move(Coordinate(coord.x, self.canvas.height)) | |
| elif (rise == 0): | |
| self.raisePen() | |
| self.move(Coordinate(self.canvas.width, self.currentPosition.y)) | |
| else: | |
| slope = rise / run | |
| possibleX = (self.canvas.height - self.currentPosition.y) / slope + self.currentPosition.x | |
| possibleY = (self.currentPosition.x - self.canvas.width) * slope + self.currentPosition.y | |
| self.raisePen() | |
| if (possibleX < self.canvas.width): | |
| self.move(Coordinate(possibleX, self.canvas.height)) | |
| else: | |
| self.move(Coordinate(self.canvas.width, possibleY)) | |
| self.lower() | |
| self.move(coord.x, coord.y) | |
| elif desiredIsOutside: | |
| rise = self.currentPosition.y - coord.y | |
| run = self.currentPosition.x - coord.x | |
| if (run == 0): | |
| self.lower() | |
| self.move(Coordinate(coord.x, self.canvas.height)) | |
| elif (rise == 0): | |
| self.lower() | |
| self.move(Coordinate(self.canvas.width, self.currentPosition.y)) | |
| else: | |
| slope = rise / run | |
| possibleX = (self.canvas.height - self.currentPosition.y) / slope + self.currentPosition.x | |
| possibleY = (self.currentPosition.x - self.canvas.width) * slope + self.currentPosition.y | |
| self.lower() | |
| if (possibleX < self.canvas.width): | |
| self.move(Coordinate(possibleX, self.canvas.height)) | |
| else: | |
| self.move(Coordinate(self.canvas.width, possibleY)) | |
| self.raisePen() | |
| self.move(coord.x, coord.y) | |
| else: | |
| self.lower() | |
| self.move(coord) | |
| pygame.display.flip() | |
| def position(self): | |
| return self.currentPosition | |
| def raisePen(self): | |
| self.canWrite = False | |
| def move(self, coord): | |
| if (self.canWrite): | |
| pygame.draw.line(self.canvas.window, (255,255,255), (self.currentPosition.x, self.currentPosition.y), (coord.x, coord.y)) | |
| self.currentPosition.x = coord.x | |
| self.currentPosition.y = coord.y | |
| def lower(self): | |
| self.canWrite = True | |
| def IsSelfOutOfBounds(self): | |
| return self.currentPosition.x > self.canvas.width or self.currentPosition.y > self.canvas.height | |
| def IsDesiredOutOfBounds(self, coord): | |
| return coord.x > self.canvas.width or coord.y > self.canvas.height | |
| if __name__ == "__main__": | |
| print 'enter coords' | |
| coords = raw_input() | |
| coords = coords.split(' ') | |
| points = [] | |
| for coord in coords: | |
| coordSplit = coord.split(',') | |
| points.append(Coordinate(int(coordSplit[0]), int(coordSplit[1]))) | |
| pen = Pen(100, 100) | |
| pen.draw(points) | |
| while True: | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| sys.exit(0) | |
| else: | |
| print event |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment