def test_inside_triangle3(self): # Close call self.assertFalse( Point2D(-3.621, 7.222, False).inside_triangle(Point2D(0, 0, False), Point2D(-7.5, 8.5, False), Point2D(3, 5, False)))
def generate(sw_boundary=Point2D(-25, -25), ne_boundary=Point2D(25, 25)): """ :type sw_boundary: Point2D :type ne_boundary: Point2D """ while True: problem = { 'pnt1': Point2D(random.randint(sw_boundary.x, ne_boundary.x), random.randint(sw_boundary.y, ne_boundary.y)) } while True: pnt2 = Point2D(random.randint(sw_boundary.x, ne_boundary.x), random.randint(sw_boundary.y, ne_boundary.y)) if problem['pnt1'].x != pnt2.x and problem['pnt1'].y != pnt2.y: problem['pnt2'] = pnt2 break while True: pnt = Point2D(random.randint(sw_boundary.x, ne_boundary.x), random.randint(sw_boundary.y, ne_boundary.y)) if not on_line(pnt, problem['pnt1'], problem['pnt2']): problem['pnt'] = pnt break for _i in range(50): v = Vector2D(random.randint(-10, 10), random.randint(-10, 10)) if solve(problem['pnt'], problem['pnt1'], problem['pnt2'], v)['does_hit'] \ and v.x != 0 and v.y != 0: problem['vec'] = v return problem
def constructNotNormalRecsSample1(): start_pt1, start_pt2 = Point2D([-2.989484, 2.030411]), Point2D([-0.040854, -9.81334]) vec_lst1 = [ Vector(4.523321,-1.382918), Vector(3.008505,9.840376), Vector(-4.523321,1.382918), Vector(-3.008505,-9.840376) ] vec_lst2 = [Vector(8.19364, 4.667886), Vector(-2.098816, 3.684097), Vector(-8.19364, -4.667886), Vector(2.098816, -3.684097)] rec1 = Rectangle(vec_lst1, start_pt1) rec2 = Rectangle(vec_lst2, start_pt2) return rec1, rec2
def test_solve_1(self): s1 = solve((-200, 200, -100, 100), Point2D(-200, -150, False), Point2D(300, 100, False)) s2 = solve((-200, 200, -100, 100), Point2D(300, 100, False), Point2D(-200, -150, False)) self.assertEqual(s1['result'], 'trivial accept') self.assertEqual(s2['result'], 'trivial accept') self.assertEqual(s1['steps'], 3) self.assertEqual(s2['steps'], 3)
def test_solve_2(self): s1 = solve((200, 600, 100, 400), Point2D(0, 50, False), Point2D(500, 450, False)) s2 = solve((200, 600, 100, 400), Point2D(500, 450, False), Point2D(0, 50, False)) self.assertEqual(s1['result'], 'trivial accept') self.assertEqual(s2['result'], 'trivial accept') self.assertEqual(s1['steps'], 3) self.assertEqual(s2['steps'], 3)
def test_solve_3(self): s1 = solve((200, 600, 100, 400), Point2D(700, 150, False), Point2D(500, 0, False)) s2 = solve((200, 600, 100, 400), Point2D(500, 0, False), Point2D(700, 150, False)) self.assertEqual(s1['result'], 'trivial reject') self.assertEqual(s2['result'], 'trivial reject') self.assertEqual(s1['steps'], 2) self.assertEqual(s2['steps'], 2)
def test_solve_4(self): s1 = solve((200, 600, 100, 400), Point2D(240, 480, False), Point2D(140, 300, False)) s2 = solve((200, 600, 100, 400), Point2D(140, 300, False), Point2D(240, 480, False)) self.assertEqual(s1['result'], 'trivial reject') self.assertEqual(s2['result'], 'trivial reject') self.assertEqual(s1['steps'], 2) self.assertEqual(s2['steps'], 2)
def update(self): if self.world.PacMan.eat_mode == True: self.lethal = False else: self.lethal = True use_rand = 1 # ghosts check to see which direciton would get it closest to pacman and moves in that direction if use_rand == 1: current = (self.position - self.world.PacMan.position).magnitude() gx = self.position.x gy = self.position.y px = self.world.PacMan.position.x py = self.world.PacMan.position.y mutateLeft = (Point2D(gx - 1, gy) - self.world.PacMan.position).magnitude() mutateRight = (Point2D(gx + 1, gy) - self.world.PacMan.position).magnitude() mutateDown = (Point2D(gx, gy - 1) - self.world.PacMan.position).magnitude() mutateUp = (Point2D(gx, gy + 1) - self.world.PacMan.position).magnitude() target = min(mutateLeft, mutateRight, mutateDown, mutateUp) if mutateLeft == target: self.intention = 'left' if mutateRight == target: self.intention = 'right' if mutateDown == target: self.intention = 'down' if mutateUp == target: self.intention = 'up' MazeBoundAgent.update(self) if self.lethal and (self.position - self.world.PacMan.position).magnitude() < 1: y = -10 for g in self.world.ghosts: g.position.x = -8 g.position.y = y y += 4 g.intention = 'up' g.direction = 'up' self.world.PacMan.lives -= 1 self.world.PacMan.position.x = 0 self.world.PacMan.position.y = 0 game.paused = True if self.world.PacMan.eat_mode == True: self.velocity = -self.velocity if self.world.PacMan.eat_mode == True and ( self.position - self.world.PacMan.position).magnitude() < 1: self.world.addPoints(20) self.position.x = 0 self.position.y = 0
def test_solve3(self): s = solve(p=Point2D(8, 5, False), p1=Point2D(7, 3, False), v1=(4, 4, 4), p2=Point2D(5, 7, False), v2=(12, 12, 12), p3=Point2D(13, 9, False), v3=(7, 7, 7)) expected = (6.0, 6.0, 6.0) self.assertAlmostEqual(expected[0], s[0]) self.assertAlmostEqual(expected[1], s[1]) self.assertAlmostEqual(expected[2], s[2])
def drawBackground(self): # black background self.canvas.create_rectangle(0, 0, self.WINDOW_WIDTH, self.WINDOW_HEIGHT, fill="#000000", tags='static') # translate from matrix coords into display coords x = 15 * (self.WINDOW_WIDTH / self.WIDTH) y = 22 * (self.WINDOW_HEIGHT / self.HEIGHT) p1 = Point2D(.5, .5) p2 = Point2D(-.5, .5) p3 = Point2D(-.5, -.5) p4 = Point2D(.5, -.5) walls = self.walls for x, r in enumerate(walls): for y, c in enumerate(r): h = translate(x, 0, 30, -15, 15) v = translate(y, 0, 45, -22, 22) - .45 if c > 0: p1 = Point2D(.5 + h, .5 + v) p2 = Point2D(-.5 + h, .5 + v) p3 = Point2D(-.5 + h, -.5 + v) p4 = Point2D(.5 + h, -.5 + v) self.draw_shape([p1, p2, p3, p4], 'blue', 'static')
def test_solve1(self): s = solve( p=Point2D(8, 8, frac=False), p1=Point2D(3, 12, frac=False), v1=(14, 14, 14), p2=Point2D(11, 10, frac=False), v2=(19, 19, 19), p3=Point2D(9, 6, frac=False), v3=(11, 11, 11), ) expected = (13.0, 13.0, 13.0) self.assertAlmostEqual(expected[0], s[0]) self.assertAlmostEqual(expected[1], s[1]) self.assertAlmostEqual(expected[2], s[2])
def test_solve2(self): s = solve( p=Point2D(9, 5, frac=False), p1=Point2D(8, 3, frac=False), v1=(0.2, 0.2, 0.2), p2=Point2D(6, 7, frac=False), v2=(0.5, 0.5, 0.5), p3=Point2D(14, 9, frac=False), v3=(0.8, 0.8, 0.8), ) expected = (23 / 60, 23 / 60, 23 / 60) self.assertAlmostEqual(expected[0], s[0]) self.assertAlmostEqual(expected[1], s[1]) self.assertAlmostEqual(expected[2], s[2])
def startGame(self, event): #Have to reinitialize all of the important variables so the restart function will work. self.gameover = False self.character = None self.newHighscore = False self.bullet_speed = 0.4 self.cannons = [] self.bullets = [] self.walls = [] self.pickups = [] self.FIRE_RATE = 10 self.canvas.delete('all') #makes all of the background canvas objects self.makeBackdrop() self.makeScoreLabel() self.makeWalls() self.makeGuns() #binds the pause menu keys self.bind_all('<KeyPress-Escape>', self.pauseMenu) self.bind_all('<KeyPress- >', self.pauseMenu) #makes the controllable player character self.character = Controllable(Point2D(), 1.0, self) #initializes the keeping track of things variables self.counter = 0 self.score = 0 self.runGame()
def addghosts(self): self.ghostnumber = 0 x = random.randint(-20, 25) y = random.randint(-20, 25) x = 0 p = Ghostboy(Point2D(x, y), self) self.ghosts.append(p)
def set_property(self, pv): # pv is a property-value pair if pv[0] == "thrust": self.thrust = float(pv[1]) elif pv[0] == "spin": self.spin = float(pv[1]) elif pv[0] == "firing_photons": self.firing_photons = (pv[1] == "True") # print(pv[1], self.firing_photons) # if self.firing_photons: # print("tested as True") elif pv[0] == "firing_missiles": self.firing_missiles = (pv[1] == "True") elif pv[0] == "firing_at": if pv[1] == 'mouseoff': self.firing_at = self.position + self.get_heading() * 2.0 else: xy = [float(i) for i in pv[1].split(",")] self.firing_at = Point2D(xy[0], xy[1]) elif pv[0] == "braking": self.braking = float(pv[1])
def __init__(self, world): position0 = Point2D() velocity0 = Vector2D(0.0, 0.0) MovingBody.__init__(self, position0, velocity0, world) self.speed = 0.0 self.angle = 90.0 self.impulse = 0
def makeGuns(self): #should be pretty self explanatory I hope. for i in range(-20, 40, 20): self.cannons.append( Launcher(Point2D(float(i), self.wallbounds.ymin), Vector2D(0.0, 1.0), self)) self.cannons.append( Launcher(Point2D(float(i), self.wallbounds.ymax), Vector2D(0.0, -1.0), self)) for k in range(3): kr = (k - 1) * 15 self.cannons.append( Launcher(Point2D(self.wallbounds.xmin, float(kr)), Vector2D(1.0, 0.0), self)) self.cannons.append( Launcher(Point2D(self.wallbounds.xmax, float(kr)), Vector2D(-1.0, 0.0), self))
def addfood(self): y = self.bounds.ymin while y <= self.bounds.ymax: x = self.bounds.xmin while x <= self.bounds.xmax: f = Food(Point2D(x, y), self) self.foods.append(f) x += 2 y += 2
def constructNormalRec(start_pt_lst, length, width): start_pt = Point2D(start_pt_lst) vec_lst = [] length_vec = Vector(length, 0.0) width_vec = Vector(0.0, width) vec_lst = [length_vec, width_vec, ReverseVector(length_vec), ReverseVector(width_vec)] rec = Rectangle(vec_lst, start_pt) #print("print(rec.vec_lst): ") #for vec in rec.vec_lst: # print(vec) return rec
def reproject_point(self, point: Point3D) -> Point2D: """ Find the 2D point of the 3D point """ coord = np.array([[point.x], [point.y], [point.z]]) extrinsic = np.matmul(self.m, np.append(coord, 1).transpose()) extrinsic = np.matmul(np.eye(3, 4), extrinsic.transpose()) extrinsic = np.array([extrinsic[0] / extrinsic[2], extrinsic[1] / extrinsic[2], 1]) # adding translation, because color_sensor: extrinsic = np.add(extrinsic, np.array([.02, 0, 0])) intrinsic = np.matmul(self.k, extrinsic.transpose()) column, row = intrinsic[0], intrinsic[1] return Point2D(int(column), int(row))
def creatrec(rec1): #输入一个gh里的矩形,输出矩形类 rec1 = rs.PolylineVertices(rec1) origin_point1 = Point2D([rec1[0][0],rec1[0][1]]) point1 = rec1[1] point_end = rec1[-2] vecx1 = Vector(point1[0]-origin_point1.x,point1[1]-origin_point1.y) vecy1 = Vector(point_end[0]-origin_point1.x,point_end[1]-origin_point1.y) vecx1_na = vecx1.reverse() vecy1_na = vecy1.reverse() now_rec1 = Rectangle(origin_point1,[vecx1,vecy1,vecx1_na,vecy1_na]) return now_rec1
def create_tex(problems, title, sw_boundary=Point2D(-25, -25), ne_boundary=Point2D(25, 25)): lis = [] directory = os.path.dirname('tex/out/collision.tex') if not os.path.exists(directory): os.makedirs(directory) with open('tex/out/collision.tex', 'w+') as f: with open('tex/template/start_content.tex', 'r') as tmp_f: for line in enumerate(tmp_f): if line[0] == 23: f.write(line[1].replace('X', title)) else: f.write(line[1]) for i in range(problems): gen = generate(sw_boundary, ne_boundary) f.write('\\subsection*{{Problem {0}}}\n'.format(i)) f.write('\\label{{psec:{0}}}\n'.format(i)) f.write( '\\addcontentsline{{toc}}{{subsection}}{{\\nameref{{psec:{0}}}}}\n' .format(i)) p = gen['pnt'] p1 = gen['pnt1'] p2 = gen['pnt2'] v = gen['vec'] lis.append((p, p1, p2, v)) f.write(single_problem(p, p1, p2, v)) f.write('\\section{Solutions}\n') for prob in enumerate(lis): f.write('\\subsection*{{Solution {0}}}\n'.format(prob[0])) f.write('\\label{{ssec:{0}}}\n'.format(prob[0])) f.write( '\\addcontentsline{{toc}}{{subsection}}{{\\nameref{{ssec:{0}}}}}\n' .format(prob[0])) sol = solve(*prob[1]) f.write( "$t_\\text{{hit}} = {0}, p_\\text{{hit}} = {1}$ and $r = {2}$\n" .format(sol['t_hit'], sol['p_hit'], sol['reflection'])) f.write('\\end{document}\n')
def update(self): if not self.world.serving: old_position = self.position new_position = self.position + self.heading * self.SPEED self.position = new_position if self.world.paddle.hits_paddle(self): self.check_bounce_horizontal(self.world.paddle.position.y+self.world.paddle.width/2.0,from_above=False) for k in self.world.brick_list: if Ball.SPEED == 0.22: if self.world.power_count >= 3: Ball.SPEED = 0.55 elif Ball.SPEED == 0.85 and self.world.score > 180: if self.world.power_count >= 3: Ball.SPEED = 0.75 elif Ball.SPEED == 0.85 and self.world.score <= 180: if self.world.power_count >= 3: Ball.SPEED = 0.55 if self.world.paddle.length == 15.0: if self.world.power_count >= 5: self.world.paddle.length = 5.0 if k.hits_brick(self): if type(k) is Power: Ball.SPEED = 0.22 self.world.power_count = 0 if type(k) is Damage: Ball.SPEED = 0.85 self.world.power_count = 0 if type(k) is Shield: self.world.paddle.length = 15.0 self.world.power_count = 0 if type(k) is Unbreakable: self.check_bounce_horizontal(k.position.y-k.width/2.0,from_above=True) if type(k) is Prize: self.world.score += 25 if type(k) is Wormhole: self.position.x,self.position.y = 0 + random.uniform(-0.3,0.3), 0.5+ random.uniform(-0.3,0.3) if self.position.y <= k.position.y + k.width/2.0 and self.position.y>k.position.y: self.check_bounce_horizontal(k.position.y+k.width/2.0,from_above=False) elif self.position.y >= k.position.y - k.width/2.0 and self.position.y<k.position.y: self.check_bounce_horizontal(k.position.y-k.width/2.0,from_above=True) elif self.position.x == k.position.x + k.length/2.0: self.check_bounce_vertical(k.position.x + k.length/2.0,from_left=True) elif self.position.x == k.position.x - k.length/2.0: self.check_bounce_vertical(k.position.x - k.length/2.0,from_left=False) self.check_bounce_horizontal(self.world.bounds.ymax,from_above=True) self.check_bounce_vertical(self.world.bounds.xmin,from_left=False) self.check_bounce_vertical(self.world.bounds.xmax,from_left=True) else: paddle = self.world.paddle self.position = Point2D(paddle.position.x,paddle.position.y)
def generate_points(dic): """ :type dic: dict """ while True: p1 = Point2D(random.randint(0, 150), random.randint(0, 150), frac=False) p2 = Point2D(random.randint(0, 150), random.randint(0, 150), frac=False) p3 = Point2D(random.randint(0, 150), random.randint(0, 150), frac=False) if len({p1.x, p2.x, p3.x}) != 3: continue if len({p1.y, p2.y, p3.y}) != 3: continue if triangle_area(p1, p2, p3) < 10: continue min_x = min(p1.x, p2.x, p3.x) max_x = max(p1.x, p2.x, p3.x) if max_x - min_x <= 1: continue min_y = min(p1.y, p2.y, p3.y) max_y = max(p1.y, p2.y, p3.y) if max_y < min_y <= 1: continue for i in range(100): p = Point2D(random.randint(min_x + 1, max_x - 1), random.randint(min_y + 1, max_y - 1), frac=False) if p.inside_triangle(p1, p2, p3): dic['p'] = p dic['p1'] = p1 dic['p2'] = p2 dic['p3'] = p3 return
def generate(): while True: w = random_window() v = random_viewport() dvx = v[1] - v[0] dwx = w[1] - w[0] dvy = v[3] - v[2] dwy = w[3] - w[2] for _i in range(100): pnt = Point2D(random.randint(w[0], w[1]), random.randint(w[2], w[3]), False) if (dvx * pnt.x + v[0] * dwx - dvx * w[0]) % dwx == 0 and ( dvy * pnt.y + v[2] * dwy - dvy * w[2]) % dwy == 0: return {'w': w, 'v': v, 'p': pnt}
def solve(w, v, pnt): """ :param: w: window (left, right, bottom, top) :type w: tuple of int :param: v: viewport (left, right, bottom, top) :type v: tuple of int :type pnt: Point2D """ dvx = v[1] - v[0] dwx = w[1] - w[0] dvy = v[3] - v[2] dwy = w[3] - w[2] return Point2D(int((dvx * pnt.x + v[0] * dwx - dvx * w[0]) // dwx), int((dvy * pnt.y + v[2] * dwy - dvy * w[2]) // dwy), False)
def __init__(self, world): self.TURNS_IN_360 = Ship.TURNS_IN_360 * (60.0 / world.FPS) self.ACCELERATION = Ship.ACCELERATION * (60.0 / world.FPS) self.MAX_SPEED = Ship.MAX_SPEED * (60.0 / world.FPS) self.FRAMES_TO_FIRE_MISSILE = Ship.FRAMES_TO_FIRE_MISSILE * world.FPS / 60.0 position0 = Point2D() velocity0 = Vector2D(0.0, 0.0) self.dependants = [ ] #to keep track of what to get rid of when the player drops MovingBody.__init__(self, position0, velocity0, world) self.speed = 0.0 self.angle = 90.0 self.radius = Ship.RADIUS self.health = self.STARTING_HEALTH self.energy = self.MAX_ENERGY self.missiles = self.STARTING_MISSILES self.weapons_on = False self.firing_at = Point2D() self.braking = False self.thrust = 0 self.spin = 0 self.firing_photons = False self.firing_missiles = False self.frames_till_missile = 0 exhaust = ShipExhaust(self) self.dependants.append(exhaust) energy_indicator = EnergyIndicator(self) self.dependants.append(energy_indicator) missile_indicator = MissileIndicator(self) self.dependants.append(missile_indicator) #for when wrecked: self.wrecked = False self.spark_frames = 20 self.since_spark = 30 #so the first spark isn't coming out during the initial burst
def make_shape(self): angle = 0.0 dA = 2.0 * math.pi / 15.0 center = Point2D(0.0, 0.0) self.polygon = [] for i in range(15): if i % 3 == 0 and random.random() < 0.2: r = self.radius / 2.0 + random.random() * 0.25 else: r = self.radius - random.random() * 0.25 dx = math.cos(angle) dy = math.sin(angle) angle += dA offset = Vector2D(dx, dy) * r self.polygon.append(offset)
def line_a_b(img_rows: int, img_cols: int, point_a: Point2D, point_b: Point2D): """ Return the constrained line pixel locations for the line going from point_a to point_b in the image. """ rr, cc = draw.line(point_a.row, point_a.col, point_b.row, point_b.col) rr_s, rr_e = constrain_interval(rr, img_rows) cc_s, cc_e = constrain_interval(cc, img_cols) start = max(rr_s, cc_s) end = min(rr_e, cc_e) if end != -1: rr = rr[start:end] cc = cc[start:end] line_pixels = [Point2D(rr[i], cc[i]) for i in range(len(cc))] return line_pixels
def __init__(self, name, w, h, ww, wh, topology='wrapped', console_lines=0): # Register the world coordinate and graphics parameters. self.WINDOW_WIDTH = ww self.WINDOW_HEIGHT = wh self.bounds = Bounds(-w / 2, -h / 2, w / 2, h / 2) self.topology = topology # Populate the world with creatures self.agents = [] self.GAME_OVER = False self.PAUSE_GAME = False # Initialize the graphics window. self.root = Tk() self.root.title(name) Frame.__init__(self, self.root) self.canvas = Canvas(self.root, width=self.WINDOW_WIDTH, height=self.WINDOW_HEIGHT) # Handle mouse pointer motion and keypress events. self.mouse_position = Point2D(0.0, 0.0) self.mouse_down = False self.bind_all('<Motion>', self.handle_mouse_motion) self.canvas.bind('<Button-1>', self.handle_mouse_press) self.canvas.bind('<ButtonRelease-1>', self.handle_mouse_release) self.bind_all('<Key>', self.handle_keypress) self.canvas.pack() if console_lines > 0: self.text = Text(self.root, height=console_lines, bg="#000000", fg="#A0F090", width=115) self.text.pack() else: self.text = None self.pack()