def line_equation(point1, point2, medium_point, coordinate): """Given ends of the line segment and a value of a given coordinate, find a value of second coordinate that lies at the segment using the line equation. point1, point2 - segment ends coordinates medium_point - point to take first value of missing point coordinate - known coordinate for a missing point (0 for x, 1 for y)""" if coordinate: comp_coordinate = 0 else: comp_coordinate = 1 value = ( (point2.get()[coordinate] - point1.get()[coordinate]) / (point2.get()[comp_coordinate] - point1.get()[comp_coordinate]) * (medium_point.get()[comp_coordinate] - point1.get()[comp_coordinate]) + point1.get()[coordinate]) comp_value = medium_point.get()[comp_coordinate] if coordinate: return classes.point(comp_value, value) else: return classes.point(value, comp_value)
def greedyRoute(): emptyT=[] a=classes.truck(id=1,capacity=10) b=classes.truck(id=2,capacity=20) c=classes.truck(id=3,capacity=10) d=classes.truck(id=4, capacity=300) emptyT.append(a) emptyT.append(b) emptyT.append(c) emptyT.append(d) point1=classes.point(id=1,xcoord=16, ycoord=16.2, requirement=1) point2=classes.point(id=2,xcoord=10, ycoord=14.2, requirement=15) point3=classes.point(id=3, xcoord=13, ycoord=19.2, requirement=15) point4=classes.point(id=4,xcoord=15,ycoord=10, requirement=3) points=[] points.append(point1) points.append(point2) points.append(point3) points.append(point4) # a.addPoint(point1) # a.addPoint(point2) # print a.getRoute() # remCap=a.getRemainingCap() # print remCap>0 # print point1.calcDist(point2) verbose=False for aTruck in emptyT: for aPoint in points: #print truck.getId(), '-', point.getId() if aPoint.getAssigned() is False and aTruck.getRemainingCap()>aPoint.getReq(): aTruck.addPoint(aPoint) aPoint.assigned=True #points.remove(point) if verbose==True: print 'ASSIGNED POINT', aPoint.getId(), 'to TRUCK', aTruck.getId() else: if verbose==True: print 'COULDNT ASSIGN POINT', aPoint.getId(), 'to TRUCK', aTruck.getId() print '**' print '****' print 'ROUTES:' for aTruck in emptyT: print print 'truck id',aTruck.getId() print 'ROUTE:',aTruck.getRoute() print 'remaining capacity:',aTruck.getRemainingCap() print '****' print '**' #greedyRoute()
def __init__(self, paper, type="normal", points=[], shape=(8, 10, 3), pin=1, spline=0, package=None, fill="#000"): meta_enabled.__init__(self, standard=paper.standard) drawable.__init__(self) with_line.__init__(self) line_colored.__init__(self) self.paper = paper self.type = type self.points = [] self.spline = spline self.paper = paper self.shape = shape self.items = [] self.pin = 1 if points: for p in points: pnt = point(self.paper, xy=p, arrow=self) self.points.append(pnt) self.reaction = reaction() self.reaction.arrows.append(self) if package: self.read_package(package)
def __init__( self, paper, coords=(), package=None, width=1): area_colored.__init__( self) vector_graphics_item.__init__( self, paper, coords=coords, package=package, width=width) del self.coords # polygon does use points for storage of coord information if not package: self.points = [] for i in range( 0, len( coords), 2): x = coords[i] y = coords[i+1] self.points.append( classes.point( self.paper, xy=( x, y), arrow=self))
def create_new_point( self, x, y, position=-1): "creates new point, position specifies relative position of point in points, usualy -1 or 0" pnt = point( self.paper, xy=(x,y), arrow=self) if position < 0: self.points.append( pnt) else: try: self.points.insert( position, pnt) except IndexError: self.points.append( pnt) warn( "bad position for adding point in arrow", UserWarning, 2) return pnt
def line_equation(point1, point2, medium_point, coordinate): """Given ends of the line segment and a value of a given coordinate, find a value of second coordinate that lies at the segment using the line equation. point1, point2 - segment ends coordinates medium_point - point to take first value of missing point coordinate - known coordinate for a missing point (0 for x, 1 for y)""" if coordinate: comp_coordinate = 0 else: comp_coordinate = 1 value = ((point2.get()[coordinate] - point1.get()[coordinate]) / (point2.get()[comp_coordinate] - point1.get()[comp_coordinate]) * (medium_point.get()[comp_coordinate] - point1.get()[comp_coordinate]) + point1.get()[coordinate]) comp_value = medium_point.get()[comp_coordinate] if coordinate: return classes.point(comp_value, value) else: return classes.point(value, comp_value)
def read_package( self, pack): """reads the dom element pack and sets internal state according to it""" self.points = [] for p in pack.getElementsByTagName( 'point'): self.points.append( classes.point( self.paper, arrow=self, package=p)) for attr in ("area_color", "line_color"): if pack.getAttributeNode( attr): setattr( self, attr, pack.getAttribute( attr)) w = pack.getAttribute( 'width') if w: self.line_width = float( w) else: self.line_width = 1.0
def read_file(file): """Read 3D-model from the given file and return it as a list of triangles Each triangle is treated as a tuple of three points.""" triangles = [] with open(file, 'r') as a_file: for a_line in a_file: str = a_line.split(",") raw_data = [] for word in str: number = word.strip("{}()\n\r\"") raw_data.append(float(number)) data = tuple([classes.point(raw_data[index], raw_data[index+1]) for index, _ in enumerate(raw_data) if index % 2 == 0]) triangles.append(data) return triangles
def read_file(file): """Read 3D-model from the given file and return it as a list of triangles Each triangle is treated as a tuple of three points.""" triangles = [] with open(file, 'r') as a_file: for a_line in a_file: str = a_line.split(",") raw_data = [] for word in str: number = word.strip("{}()\n\r\"") raw_data.append(float(number)) data = tuple([ classes.point(raw_data[index], raw_data[index + 1]) for index, _ in enumerate(raw_data) if index % 2 == 0 ]) triangles.append(data) return triangles
def read_package( self, package): """reads the dom element package and sets internal state according to it""" if package.getAttribute( 'id'): self.id = package.getAttribute( 'id') a = ['no', 'yes'] self.type = package.getAttribute( 'type') or 'normal' start = a.index( package.getAttribute( 'start')) end = a.index( package.getAttribute( 'end')) if start and end: self.pin = 3 elif start: self.pin = 2 elif end: self.pin = 1 else: self.pin = 0 self.spline = a.index( package.getAttribute( 'spline')) self.line_width = float( package.getAttribute( 'width')) #self.shape = package.getAttribute( 'shape') self.line_color = package.getAttribute( 'color') for p in package.getElementsByTagName( 'point'): self.points.append( point( self.paper, arrow=self, package=p))
def __init__( self, paper, type="normal", points=[], shape=(8,10,3), pin=1, spline=0, package=None, fill="#000"): meta_enabled.__init__( self, standard=paper.standard) drawable.__init__( self) with_line.__init__( self) line_colored.__init__( self) self.paper = paper self.type = type self.points = [] self.spline = spline self.paper = paper self.shape = shape self.items = [] self.pin = 1 if points: for p in points: pnt = point( self.paper, xy=p, arrow=self) self.points.append( pnt) self.reaction = reaction() self.reaction.arrows.append( self) if package: self.read_package( package)
if __name__ == '__main__': #read model triangles = read_file(TEST_FILE) #init pygame and compute basic model parameters pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) window_center_x = WINDOW_WIDTH / 2 window_center_y = WINDOW_HEIGHT / 2 model_boundaries = model_boundaries() scale = model_boundaries.scale_factor() shift_x = model_boundaries.shift_x() shift_y = model_boundaries.shift_y() #scale and render each triangle for triangle in triangles: new_triangle = [] for point in triangle: new_triangle.append(classes.point( window_center_x + point.get_x() * scale - shift_x, window_center_y - point.get_y() * scale + shift_y)) render_triangle(new_triangle) #execution loop handle_events()
if __name__ == '__main__': #read model triangles = read_file(TEST_FILE) #init pygame and compute basic model parameters pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) window_center_x = WINDOW_WIDTH / 2 window_center_y = WINDOW_HEIGHT / 2 model_boundaries = model_boundaries() scale = model_boundaries.scale_factor() shift_x = model_boundaries.shift_x() shift_y = model_boundaries.shift_y() #scale and render each triangle for triangle in triangles: new_triangle = [] for point in triangle: new_triangle.append( classes.point( window_center_x + point.get_x() * scale - shift_x, window_center_y - point.get_y() * scale + shift_y)) render_triangle(new_triangle) #execution loop handle_events()
from classes import point p = point(input("entr the value"), input("entr the y value")) print(p.x) print(p.y)