def draw_line(win, colour, point1, point2, current_tile): """Helper function for drawing lines.""" current_line = Line(Point(*point1), Point(*point2)) current_line.setWidth(2) current_line.setFill(colour) current_line.draw(win) current_tile.append(current_line)
def draw(self): if self.pulsing: color = 'red' else: color = 'black' target = self.target source = self.source line = Line(source.location, target.location) line.setWidth(1) line.setFill(color) line.setOutline(color) line.draw(self.brain.win) dx = target.location.x - source.location.x dy = target.location.y - source.location.y k = dy / dx if dx != 0 else dy k = abs(k) dd = 20 sign_dx = -1 if dx < 0 else 1 sign_dy = -1 if dy < 0 else 1 dx = -sign_dx * dd / sqrt(k**2 + 1) dy = -sign_dy * k * dd / sqrt(k**2 + 1) # sleep(1) dp = Point(target.location.x + dx, target.location.y + dy) line = Line(dp, target.location) line.setWidth(3) line.setFill(color) line.draw(self.brain.win)
def draw_arrow(win, arrow_x, arrow_y): """Draw an arrow onto the given graphics window.""" arrow_shaft = Circle(Point(arrow_x, arrow_y), 0.008).draw(win) arrow_shaft.setFill("brown") for x in (1, -1): fletching = Line(Point(arrow_x + 0.02, arrow_y + 0.02 * x), Point(arrow_x - 0.02, arrow_y - 0.02 * x)).draw(win) fletching.setWidth(2) fletching.setFill("gray")
def get_right(self): x = self.position.x + (self.looking.x - self.position.x) * math.cos( -math.pi / 3) - (self.looking.y - self.position.y) * math.sin( -math.pi / 3) y = self.position.y + (self.looking.x - self.position.x) * math.sin( -math.pi / 3) + (self.looking.y - self.position.y) * math.cos( -math.pi / 3) l = Line(self.position, Point(x, y)) l.setFill(self.color) return l
def draw_patch_window(): """ 9. Write a function draw_patch_window() (without parameters) which displays, in a graphics window of size 100 × 100 pixels, the patch design which is labelled with the final digit of your student number. The patch design should be displayed in red, as in the table. It's important that your program draws the patch design accurately, but don't worry if one pixel is chopped off at the edge of the window. Note that you don't need to draw a border around the patch design. """ win = GraphWin("Patch design", 100, 100) for distance in (20, 40, 60, 80, 100): line = Line(Point(0, distance), Point(distance, 0)) line.setFill("red") line.setWidth(2) line.draw(win) line = Line(Point(100 - distance, 0), Point(100, distance)) line.setFill("red") line.setWidth(2) line.draw(win) for distance in (20, 40, 60, 80): line = Line(Point(distance, 100), Point(100, distance)) line.setFill("red") line.setWidth(2) line.draw(win) line = Line(Point(0, distance), Point(100 - distance, 100)) line.setFill("red") line.setWidth(2) line.draw(win) win.getMouse() win.close()
def __init__(self): self.lines = frame() steps = range(99, 499, 100) for y in steps: line = Line(Point(0, y), Point(499, y)) line.setFill("blue") line.setWidth(5) self.lines.append(line) for x in steps: line = Line(Point(x, 0), Point(x, 499)) line.setFill("blue") line.setWidth(5) self.lines.append(line)
class Goal: def __init__(self, vel, pos, win): self.vel_x, self.vel_y = vel[0], vel[1] self.vel = np.array([self.vel_x, self.vel_y]) self.pos_x = pos[0] self.pos_y = pos[1] self.win = win def set_graphicals(self): # draw player self.body = Circle(Point(scale(self.pos_x), scale(self.pos_y)), 7) self.body.setFill('red') # Note: downwards in Y is the positive direction for this graphics lib self.arrow = Line( Point(scale(self.pos_x), scale(self.pos_y)), Point(scale(self.pos_x + self.vel_x), scale(self.pos_y + self.vel_y))) self.arrow.setFill('black') self.arrow.setArrow('last') self.body.draw(self.win) self.arrow.draw(self.win)
def drawAxis(win, new_view): L1 = Line(Point(0, 0), Point(new_view.xVmax, 0)) L2 = Line(Point(0, 0), Point(0, new_view.yVmax)) L3 = Line(Point(0, 0), Point(new_view.xVmin, new_view.yVmin)) L1.setFill('blue') L2.setFill('blue') L3.setFill('blue') L1.setArrow("last") L2.setArrow("last") L3.setArrow("last") L1.draw(win) L2.draw(win) L3.draw(win) return win
def draw_patch(win, x, y, colour): """Helper function for drawing a patch design.""" for distance in (20, 40, 60, 80, 100): line = Line(Point(x, distance + y), Point(x + distance, y)) line.setFill(colour) line.setWidth(2) line.draw(win) line = Line(Point(x + 100 - distance, y), Point(x + 100, y + distance)) line.setFill(colour) line.setWidth(2) line.draw(win) for distance in (20, 40, 60, 80): line = Line(Point(x + distance, y + 100), Point(x + 100, y + distance)) line.setFill(colour) line.setWidth(2) line.draw(win) line = Line(Point(x, y + distance), Point(x + 100 - distance, y + 100)) line.setFill(colour) line.setWidth(2) line.draw(win)
tongue_tip = Circle(Point(299, 405), 30) tongue_tip.setFill(color_rgb(90, 255, 84)) tongue_tip.setWidth(3) tongue_tip.draw(win) tongue = Rectangle(Point(269, 403), Point(329, 348)) tongue.setFill(color_rgb(90, 255, 84)) tongue.setWidth(0) tongue.draw(win) tongue_ctr = Line(Point(299, 348), Point(299, 385)) tongue_ctr.setWidth(4) tongue_ctr.draw(win) tongue_edge_L = Line(Point(269, 348), Point(269, 410)) tongue_edge_L.setFill('black') tongue_edge_L.setWidth(3) tongue_edge_L.draw(win) tongue_edge_R = tongue_edge_L.clone() tongue_edge_R.move(60, 0) tongue_edge_R.draw(win) # Black Border border = Circle(Point(299, 299), 350) border.setFill('') border.setWidth(200) border.draw(win) win.getMouse() win.close()
class DynamicPoint: def __init__(self, vel_start, pos_start, dt, vel_max, acc_max, win): # dynamics related self.vel_start = np.array(vel_start) self.pos_x = pos_start[0] self.pos_y = pos_start[1] self.dt = dt self.vel_max = vel_max self.acc_max = acc_max self.dist_max = vel_max * dt # path planning related self.finished = False self.path = None self.sling_path = None self.sling_path_calculated = None self.sling_vel = None self.sling_acc = None self.node_count = 0 self.node_count_sling = 0 self.at_node = True self.total_time = 0 # graphics related self.body = None self.body_radius = 10 self.vel_arrow = None self.acc_arrow = None self.win = win self.sling_path_drawables = [] self.get_trace = False # Change this to see the car in action or just to see the trace if self.get_trace is True: self.body_radius /= 10 # for actions self.current_action = None self.current_acc = np.array([0, 0], dtype=float) self.current_vel = np.array(vel_start, dtype=float) self.new_action = True #Flag if new action is received self.arc_length = 0 self.precision = 0.4 self.position_error = 0 # for circle/arc parameters self.theta = 0 self.T = 0 self.n = 0 self.beta = 0 self.circle = None def set_velocity(self, goal): self.total_time += self.dt # check if we have reached the next node self.position_error = math.sqrt( (self.pos_x - self.sling_path[-1][0].x)**2 + (self.pos_y - self.sling_path[-1][0].y)**2) #print(self.position_error, self.n) if self.position_error < self.precision or self.n < 0: # we have reached the next one, but is it # the goal node? if (len(self.sling_path) == 1): self.finished = True return else: self.current_action = self.sling_path.pop() self.pos_x = self.current_action[0].x self.pos_y = self.current_action[0].y vel_dummy = self.sling_vel.pop() #TODO Setting velocity to dirct to the next point if self.current_action[1] == 0: direc = 0 if self.sling_path[-1][0].x - self.pos_x < 0: direc = math.pi + math.atan( (self.sling_path[-1][0].y - self.pos_y) / (self.sling_path[-1][0].x - self.pos_x)) else: direc = math.atan( (self.sling_path[-1][0].y - self.pos_y) / (self.sling_path[-1][0].x - self.pos_x)) vel_mag = math.sqrt( np.dot(self.current_vel, self.current_vel)) self.current_vel[0] = vel_mag * math.cos(direc) self.current_vel[1] = vel_mag * math.sin(direc) self.current_acc = self.sling_acc.pop() self.new_action = True # are we going into a circle? if (self.current_action[1] != 0): if self.new_action: #self.set_circle_params() #self.circle = DubinCircle.fromArc(self.current_action[0], self.sling_path[-1][0], self.current_action[1]) #print("Status: ",self.current_action[0], self.current_action[1], self.current_vel) self.circle = DubinCircle.fromVel(self.current_action[0], self.current_action[1], self.current_vel) self.T = self.circle.arclength(self.current_action[0], self.sling_path[-1][0]) / \ math.sqrt(np.dot(self.current_vel, self.current_vel)) self.theta = self.circle.arcangle(self.current_action[0], self.sling_path[-1][0]) #if self.theta<0: # self.theta = 2*math.pi + self.theta self.n = self.T / self.dt self.beta = self.theta / self.n self.new_action = False self.move_circular() else: # we are moving straight if self.new_action: angle = atan(self.current_vel[1] / self.current_vel[0]) acc_mag = sqrt(np.dot(self.current_acc, self.current_acc)) vel_mag1 = sqrt(np.dot(self.current_vel, self.current_vel)) vel_mag2 = sqrt(np.dot(self.sling_vel[-1], self.sling_vel[-1])) #print("vel1", vel_mag1, "vel2", vel_mag2) acc_sign = 1 if (vel_mag2 < vel_mag1): acc_sign = -1 self.current_acc[0] = acc_sign * acc_mag * cos(angle) self.current_acc[1] = acc_sign * acc_mag * sin(angle) #self.current_acc = find_acc(self.current_vel, self.sling_vel[-1], # self.current_action[0].dist_to(self.sling_path[-1][0])) self.new_action = False self.move() #print("Velocity:", math.sqrt(np.dot(self.current_vel,self.current_vel)), # "Accel:", self.current_acc) #self.total_time += self.dt def move_circular(self): # TODO: sign fix rotation_mat = np.array([[cos(self.beta), sin(self.beta)], [-sin(self.beta), cos(self.beta)]]) position_vect = np.array( [self.pos_x - self.circle.c.x, self.pos_y - self.circle.c.y]) position_vect = position_vect @ rotation_mat self.pos_x = position_vect[0] + self.circle.c.x self.pos_y = position_vect[1] + self.circle.c.y #self.pos_x = (self.pos_x - self.circle.c.x) * cos(self.beta) - \ # (self.pos_y - self.circle.c.y) * sin(self.beta) + self.circle.c.x #self.pos_y = (self.pos_x - self.circle.c.x) * sin(self.beta) + \ # (self.pos_y - self.circle.c.y) * cos(self.beta) + self.circle.c.y #print("beta: ",self.beta,"radius: ",(self.pos_x-self.circle.c.x)**2 + (self.pos_y-self.circle.c.y)**2) self.current_vel = self.current_vel @ rotation_mat angle = math.atan2(-self.current_vel[0], self.current_vel[1]) self.current_acc[0] = self.acc_max * cos(angle) self.current_acc[1] = self.acc_max * sin(angle) self.n -= 1 #self.set_graphicals() def move(self): #print(self.current_vel) self.current_vel[ 0] = self.current_vel[0] + self.current_acc[0] * self.dt self.current_vel[ 1] = self.current_vel[1] + self.current_acc[1] * self.dt self.pos_x += self.dt * self.current_vel[0] self.pos_y += self.dt * self.current_vel[1] #self.set_graphicals() def add_path(self, path): # A path is a list of nodes self.path = path self.node_count = len(path) def add_sling_path(self, goal, obstacles=None): # A path is a list of nodes vel_series = get_velocity_series(self.path, self.vel_start, goal.vel, self.vel_max) acc_series = get_acceleration_series(vel_series, self.acc_max) self.sling_path, self.sling_vel, self.sling_acc = create_sling_path( self.path, vel_series, acc_series, obstacles=None) #TODO: set it back to obstacles if not self.sling_path: return False self.sling_path_calculated = self.sling_path #print("Path Generated : ", self.sling_path_calculated) self.node_count_sling = len(self.sling_path) self.sling_path = [el for el in reversed(self.sling_path)] self.sling_vel = [el for el in reversed(self.sling_vel)] self.sling_acc = [el for el in reversed(self.sling_acc)] ''' if self.sling_path_calculated is not None: self.sling_path_drawables = [Circle(action[0].get_scaled_point(), 3) for action in self.sling_path_calculated] for el in self.sling_path_drawables: el.draw(self.win) for el in self.sling_path: cir = Circle(el[0].get_scaled_point(), 3) cir.draw(self.win) ''' #for action in self.sling_path_calculated: # self.body = Circle(action[0].get_scaled_point(), self.body_radius) # self.body.setFill('yellow') # self.body.draw(self.win) def set_graphicals(self): draw_x = scale(self.pos_x) draw_y = scale(self.pos_y) if self.circle is not None and self.get_trace is False: dubinc = Circle(self.circle.c.get_scaled_point(), scale_vectors(self.circle.r)) dubinc.setOutline('Green') dubinc.draw(self.win) if self.body is not None and self.get_trace is False: self.body.undraw() self.body = Circle(Point(draw_x, draw_y), self.body_radius) self.body.setFill('yellow') self.body.draw(self.win) if self.vel_arrow: self.vel_arrow.undraw() self.vel_arrow = Line( Point(draw_x, draw_y), Point(scale(self.pos_x + self.current_vel[0]), scale(self.pos_y + self.current_vel[1]))) self.vel_arrow.setFill('black') self.vel_arrow.setArrow("last") self.vel_arrow.draw(self.win) if self.acc_arrow: self.acc_arrow.undraw() self.acc_arrow = Line( Point(draw_x, draw_y), Point(scale(self.pos_x + self.current_acc[0] * 5), scale(self.pos_y + self.current_acc[1] * 5))) self.acc_arrow.setFill('blue') self.acc_arrow.setArrow('last') self.acc_arrow.draw(self.win)
c = int(c) if is_vert == '': rec = Rectangle(points[r][c], points[r+1][c+1]) rec.setFill(colors[player]) rec.draw(win) scores[player] += 1 score_text[player].undraw() score_text[player].setText('{}:{}'.format(wins[player], scores[player])) score_text[player].draw(win) else: if is_vert: line = Line(points[r][c], points[r+1][c]) else: line = Line(points[r][c], points[r][c+1]) line.draw(win) line.setFill(colors[player]) printer(win, results_dir, f, i, j) (n1, s1), (n2, s2) = scores.items() if s1 > s2: winner = n1 elif s2 > s1: winner = n2 else: winner = None if winner: wins[winner] += 1 score_text[winner].undraw() score_text[winner].setText('{}:{}'.format(wins[winner], scores[winner])) score_text[winner].draw(win)
class DynamicPointTest: def __init__(self, vel_start, pos_start, dt, vel_max, acc_max, win): # dynamics related self.vel_start = np.array(vel_start) self.pos_x = pos_start[0] self.pos_y = pos_start[1] self.dt = 0.05 self.vel_max = vel_max self.acc_max = acc_max self.dist_max = vel_max * dt # path planning related self.finished = False self.path = None self.sling_path = None self.sling_path_calculated = None self.sling_vel = None self.sling_acc = None self.node_count = 0 self.node_count_sling = 0 self.at_node = True self.total_time = 0 # graphics related self.body = None self.body_radius = 10 self.vel_arrow = None self.acc_arrow = None self.win = win # for actions self.current_action = None self.current_acc = None self.current_vel = None self.new_action = True #Flag if new action is received self.arc_length = 0 # for circle/arc parameters self.theta = 0 self.T = 0 self.n = 0 self.beta = 0 self.circle = None def set_velocity(self, goal): # check if we have reached the next node if self.pos_x == self.sling_path[-1][ 0].x and self.pos_y == self.sling_path[-1][0].y or self.n == 0: # we have reached the next one, but is it # the goal node? if (len(self.sling_path) == 1): self.finished = True return else: self.current_action = self.sling_path.pop() self.current_vel = self.sling_vel.pop() self.current_acc = self.sling_acc.pop() self.new_action = True # are we going into a circle? if (self.current_action[1] != 0): if self.new_action: self.set_circle_params() self.T = self.arc_length / math.sqrt(self.current_vel[0]**2 + self.current_vel[1]**2) self.n = math.floor(self.T / self.dt) self.beta = self.theta / self.n self.new_action = False self.move_circular() else: # we are moving straight self.move() self.total_time += self.dt def move_circular(self): angle = atan(-self.current_vel[0] / self.current_vel[1]) self.current_acc[0] = self.current_acc * cos(angle) self.current_acc[1] = self.current_acc * sin(angle) rotation_mat = np.array([[cos(self.beta), sin(self.beta)], [-sin(self.beta), cos(self.beta)]]) self.current_vel = self.current_vel * rotation_mat self.pos_x = (self.pos_x - self.circle.c.x) * cos(self.beta) - ( self.pos_y - self.circle.c.y) * sin(self.beta) + self.circle.c.x self.pos_y = (self.pos_x - self.circle.c.x) * sin(self.beta) + ( self.pos_y - self.circle.c.y) * cos(self.beta) + self.circle.c.y self.n -= 1 self.set_graphicals() def move(self): angle = atan(self.current_vel[1] / self.current_vel[0]) self.current_acc[0] = self.current_acc * cos(angle) self.current_acc[1] = self.current_acc * sin(angle) self.current_vel[ 0] = self.current_vel[0] + self.current_acc[0] * self.dt self.current_vel[ 1] = self.current_vel[1] + self.current_acc[1] * self.dt self.pos_x += self.dt * self.current_vel[0] self.pos_y += self.dt * self.current_vel[1] self.set_graphicals() def add_path(self, path): # A path is a list of nodes self.path = path self.node_count = len(path) def add_sling_path(self, goal): # A path is a list of nodes vel_series = get_velocity_series(self.path, self.vel_start, goal.vel, self.vel_max) acc_series = get_acceleration_series(self.path, self.acc_max) self.sling_path, self.sling_vel, self.sling_acc = create_sling_path( self.path, vel_series, acc_series) self.sling_path_calculated = self.sling_path print("Path Generated : ", self.sling_path_calculated) self.node_count_sling = len(self.sling_path) self.sling_path = [el for el in reversed(self.sling_path)] self.sling_vel = [el for el in reversed(self.sling_vel)] self.sling_acc = [el for el in reversed(self.sling_acc)] def set_circle_params(self): p1, p2 = self.current_action[0], self.sling_path[-1][0] vel_1 = self.current_vel dir_1 = self.current_action[1] slope = -vel_1[0] / vel_1[1] radius = abs(1 / dir_1) k = radius / math.sqrt(1 + slope**2) centre = Node(p1.x + k, p1.y + k * slope) circle = DubinCircle(centre, radius, dir_1) theta = circle.arcangle(p1, p2) self.arc_length = circle.arclength(p1, p2) self.theta = theta self.circle = circle def set_graphicals(self): draw_x = scale(self.pos_x) draw_y = scale(self.pos_y) # Draw the new path if self.sling_path_calculated is not None: for action in self.sling_path_calculated: cir = Circle(action[0].get_scaled_point(), self.body_radius) cir.setFill('yellow') cir.draw(self.win) if self.circle is not None: dubinc = Circle(self.circle.c.get_scaled_point(), scale_vectors(self.circle.r)) dubinc.setOutline('Green') dubinc.draw(self.win) if self.body: self.body.undraw() self.body = Circle(Point(draw_x, draw_y), self.body_radius) self.body.setFill('yellow') self.body.draw(self.win) if self.vel_arrow: self.vel_arrow.undraw() self.vel_arrow = Line( Point(draw_x, draw_y), Point(scale(self.pos_x + self.current_vel[0] * 5), scale(self.pos_y + self.current_vel[1] * 5))) self.vel_arrow.setFill('black') self.vel_arrow.setArrow("last") self.vel_arrow.draw(self.win) if self.acc_arrow: self.acc_arrow.undraw() self.acc_arrow = Line( Point(draw_x, draw_y), Point(scale(self.pos_x + self.current_acc[0] * 5), scale(self.pos_y + self.current_acc[1] * 5))) self.acc_arrow.setFill('blue') self.acc_arrow.setArrow('last') self.acc_arrow.draw(self.win) '''
if is_vert == '': rec = Rectangle(points[r][c], points[r + 1][c + 1]) rec.setFill(colors[player]) rec.draw(win) scores[player] += 1 score_text[player].undraw() score_text[player].setText('{}:{}'.format( wins[player], scores[player])) score_text[player].draw(win) else: if is_vert: line = Line(points[r][c], points[r + 1][c]) else: line = Line(points[r][c], points[r][c + 1]) line.draw(win) line.setFill(colors[player]) printer(win, results_dir, f, i, j) (n1, s1), (n2, s2) = scores.items() if s1 > s2: winner = n1 elif s2 > s1: winner = n2 else: winner = None if winner: wins[winner] += 1 score_text[winner].undraw() score_text[winner].setText('{}:{}'.format( wins[winner], scores[winner]))
def draw_walk(win, start, current_pos, colour): line = Line(start, current_pos) line.setFill(colour) line.draw(win)
def get_fire(self): l = Line(self.position, self.looking) l.setFill("orange") return l