def shoot_em(self, tank): my_position = Point(tank.x, tank.y) for enemy in self.enemies: enemy_position = Point(enemy.x, enemy.y) if my_position.distance(enemy_position) <= 50: theta = self.fields.angle(tank, enemy) if theta < 1.5 and theta > -1.5: line_to_enemy = Line(my_position, enemy_position) safe = True for teamMate in self.mytanks: if teamMate.index == tank.index: continue teamMate_position = Point(teamMate.x, teamMate.y) cp2 = teamMate_position.closestPointOnLine( line_to_enemy) teamMate_enemy_line = Line(teamMate_position, cp2) if teamMate_position.distance(cp2) < 10: safe = False break if safe == True: return True return False
def __init__(self, bzrc,tank,job): self.updatecounts = 0 self.tank = tank self.bzrc = bzrc self.fields = fields.Calculations() self.throttle = .15 self.commands = [] self.constants = self.bzrc.get_constants() self.job = job self.base = bzrc.get_bases()[self.getFlagIndex()] c1 = Point(self.base.corner1_x,self.base.corner1_y) c3 = Point(self.base.corner3_x,self.base.corner3_y) line = Line(c1,c3) self.base = line.getMidpoint() flags = bzrc.get_flags() for flag in flags: if flag.color == self.constants['team']: self.flag = flag self.obstacles = [] #the .07125 comes from the 4 Ls world where 7.125% of the world was occupied self.time = time.time() self.locationList = [] self.oldlocation = [] self.startTime = time.time() self.mytanks = self.bzrc.get_mytanks()
def __init__(self, bzrc, tank, job): self.updatecounts = 0 self.tank = tank self.bzrc = bzrc self.fields = fields.Calculations() self.throttle = .15 self.commands = [] self.constants = self.bzrc.get_constants() self.job = job self.base = bzrc.get_bases()[self.getFlagIndex()] c1 = Point(self.base.corner1_x, self.base.corner1_y) c3 = Point(self.base.corner3_x, self.base.corner3_y) line = Line(c1, c3) self.base = line.getMidpoint() flags = bzrc.get_flags() for flag in flags: if flag.color == self.constants['team']: self.flag = flag self.obstacles = [] #the .07125 comes from the 4 Ls world where 7.125% of the world was occupied self.time = time.time() self.locationList = [] self.oldlocation = [] self.startTime = time.time() self.mytanks = self.bzrc.get_mytanks()
def question_22_17(query): VertexB = Point(Fraction(1), Fraction(-2), "B") VertexC = Point(Fraction(-2), Fraction(-1), "C") EdgeA = Line.get_line_contains_points(VertexB, VertexC) p1 = Point(Fraction(2), Fraction(0), "p1") p2 = Point(Fraction(2), Fraction(1), "p2") EdgeC = Line.get_line_contains_points(VertexB, p1) EdgeB = Line.get_line_contains_points(VertexC, p2) del p1, p2 BisectorAngleA = Line.get_bisectors_for_2lines(EdgeB, EdgeC)[0] VertexOfTargetRhombusInsideEdgeA = EdgeA.get_cross_point(BisectorAngleA) EageOfTargetRhombusParallelsToEdgeC = Line.get_line_parallel_to(EdgeC, VertexOfTargetRhombusInsideEdgeA) EageOfTargetRhombusParallelsToEdgeB = Line.get_line_parallel_to(EdgeB, VertexOfTargetRhombusInsideEdgeA) VertexOfTargetRhombusInsideEdgeB = Line.get_cross_point(EageOfTargetRhombusParallelsToEdgeC, EdgeB) VertexOfTargetRhombusInsideEdgeC = Line.get_cross_point(EageOfTargetRhombusParallelsToEdgeB, EdgeC) TriangleEdges = {(EdgeA.a, EdgeA.b, EdgeA.c), (EdgeB.a, EdgeB.b, EdgeB.c), (EdgeC.a, EdgeC.b, EdgeC.c)} init_figure, grid_size = common.INIT_FIGURE(), common.GRID_SIZE() init_figure.extend(TriangleEdges) resolution_vertex_in_edge_a = query.query_point_by_symmetry(VertexOfTargetRhombusInsideEdgeA, init_figure) resolution_vertex_in_edge_b = query.query_point_by_symmetry(VertexOfTargetRhombusInsideEdgeB, init_figure) resolution_vertex_in_edge_c = query.query_point_by_symmetry(VertexOfTargetRhombusInsideEdgeC, init_figure) resolution_product = itertools.product(resolution_vertex_in_edge_a, resolution_vertex_in_edge_b, resolution_vertex_in_edge_c) solution_for_all = [frozenset((set(r[0]) | set(r[1]) | set(r[2]))) for r in resolution_product] solution_for_all.sort() targets = (VertexOfTargetRhombusInsideEdgeA, VertexOfTargetRhombusInsideEdgeB, VertexOfTargetRhombusInsideEdgeC) for r in solution_for_all: draw_resolution(r, targets, init_figure, grid_size)
def question_16_15(query): line1 = Line.get_line_contains_points(Point(-2, 0), Point(0, 1)) line2 = Line.get_line_contains_points(Point(0, -1), Point(1, 1)) line = Line.get_bisectors_for_2lines(line1, line2)[0] A = line1.get_cross_point(line2) # query and show it(them) init_figure, grid_size = common.INIT_FIGURE(), common.GRID_SIZE() init_figure.extend(((line1.a, line1.b, line1.c), (line2.a, line2.b, line2.c))) solution = query.query_line_by_symmetry(line, (A,), init_figure) points = set([s[0] for s in solution]) for r in solution[:4]: p, fig = r draw_resolution(fig, points, init_figure, grid_size)
def __init__(self, bzrc, enemy): self.bzrc = bzrc self.constants = self.bzrc.get_constants() self.commands = [] self.throttle = .15 self.obstacles = [] self.fields = Calculations() self.flagIndex = self.getIndex() self.base = bzrc.get_bases()[self.flagIndex] self.enemyFlag = int(enemy) c1 = Point(self.base.corner1_x, self.base.corner1_y) c3 = Point(self.base.corner3_x, self.base.corner3_y) line = Line(c1, c3) self.base = line.getMidpoint() for obs in bzrc.get_obstacles(): self.obstacles.append(Obstacle(obs))
def question_14_9(query): # get Point B line1 = Line.get_line_contains_points(Point(-3, 1), Point(3, 3)) line2 = Line(1, 0, -2) B = line1.get_cross_point(line2) # get Point A line1 = Line.get_line_contains_points(Point(2, -3), Point(3, 0)) line2 = Line(0, 1, -1) A = line1.get_cross_point(line2) del line1, line2 # get the midpoint target_point_x = (A.x + B.x) / 2 target_point_y = (A.y + B.y) / 2 point_target = Point(target_point_x, target_point_y, "Target") # query and show it(them) solution = query.query_point_by_symmetry(point_target) init_figure, grid_size = common.INIT_FIGURE(), common.GRID_SIZE() for r in solution: draw_resolution(r, [point_target], init_figure, grid_size)
def question_24_6(query): A = Point(1, -2) B = Point(0, 1) line1 = Line.get_line_contains_points(A, B) line2 = Line.get_line_by_point_slope(A, Fraction(4)) tan_alpha = Fraction(1, 4) tan_beta = Fraction(1, 3) tan_theta = tan_of_add(tan_of_double(tan_beta), tan_alpha) slope = tan_of_neg(tan_of_complementary(tan_theta)) line = Line.get_line_by_point_slope(A, slope) # query and show it(them) init_figure, grid_size = common.INIT_FIGURE(), common.GRID_SIZE() init_figure.extend(((line1.a, line1.b, line1.c), (line2.a, line2.b, line2.c))) solution = query.query_line_by_symmetry(line, (A,), init_figure) points = set([s[0] for s in solution]) for r in solution[:4]: p, fig = r draw_resolution(fig, points, init_figure, grid_size)
def question_14_8(query): # get Point B line1 = Line.get_line_contains_points(Point(-3, -1), Point(3, -3)) line_tmp = Line(1, 0, -2) B = line1.get_cross_point(line_tmp) # get Point A line2 = Line.get_line_contains_points(Point(-3, 1), Point(2, 2)) line_tmp = Line(1, 0, 1) A = line2.get_cross_point(line_tmp) line = Line.get_line_contains_points(A, B) point_target = A.middle(B) # query and show it(them) init_figure, grid_size = common.INIT_FIGURE(), common.GRID_SIZE() for ll in (line, line1, line2): init_figure.append((ll.a, ll.b, ll.c)) solution = query.query_point_by_symmetry(point_target, init_figure) for r in solution[:4]: draw_resolution(r, [point_target], init_figure, grid_size)
def question_24_7(query): A = Point(2, -1) B = Point(-2, 0) line1 = Line.get_line_contains_points(A, B) line2 = Line.get_line_by_point_slope(A, Fraction(-1)) tan_alpha = Fraction(1, 4) tan_beta = tan_of_sub(Fraction(1), tan_alpha) tan_theta = tan_of_sub(tan_beta, tan_alpha) slope = tan_theta line = Line.get_line_by_point_slope(A, slope) # query and show it(them) init_figure, grid_size = common.INIT_FIGURE(), common.GRID_SIZE() init_figure.extend(((line1.a, line1.b, line1.c), (line2.a, line2.b, line2.c))) solution = query.query_line_by_symmetry(line, (A,), init_figure) points = set([s[0] for s in solution]) for r in solution[:4]: p, fig = r draw_resolution(fig, points, init_figure, grid_size)
def getTangentialObstacleField(self, tank, target, oldTank, elapsedTime): deltaX = 0.0 deltaY = 0.0 tankPosition = Point(tank.x, tank.y) closestPoint = tankPosition.closestPointOnLine(target.p1, target.p2) if self.distance(tankPosition, closestPoint) < target.spread: lineToTarget = Line(tankPosition, closestPoint) if target.isPerpendicular(lineToTarget): midpoint = target.getMidpoint() if closestPoint.x < midpoint.x: deltaX = -1.0 elif closestPoint.x > midpoint.x: deltaX = 1.0 else: deltaX = 0.0 if closestPoint.y < midpoint.y: if target.getSlope() == 0.0: deltaY = 0.0 elif target.getSlope() == None: deltaY = -1.0 else: deltaY = -1.0 * target.getSlope() elif closestPoint.y > midpoint.y: if target.getSlope() == 0: deltaY = 0.0 elif target.getSlope() == None: deltaY = 1.0 else: deltaY = 1.0 * target.getSlope() else: deltaY = 0.0 return deltaX, deltaY
def getCenter(self): coolLine = Line(self.corners[0], self.corners[2]) midpoint = coolLine.getMidpoint() return midpoint
def draw_resolution(fig, target_points, init_figure, grid_size): grid_pixels = 100 line_width = 1 margin = 10 total_size = margin * 2 + grid_size * 2 * grid_pixels + line_width color_background = ImageColor.colormap["whitesmoke"] color_frame = ImageColor.colormap["darkgrey"] color_pre_line = ImageColor.colormap["darkred"] color_ans_line = ImageColor.colormap["darkblue"] color_key_point = ImageColor.colormap["yellow"] color_target_point = ImageColor.colormap["yellowgreen"] radius_key_point = 5 img = Image.new('RGB', (total_size, total_size), color_background) draw = ImageDraw.Draw(img) line_top = Line(Fraction(0), Fraction(1), Fraction(-grid_size)) line_bottom = Line(Fraction(0), Fraction(1), Fraction(+grid_size)) line_left = Line(Fraction(1), Fraction(0), Fraction(-grid_size)) line_right = Line(Fraction(1), Fraction(0), Fraction(+grid_size)) frame = (line_top, line_bottom, line_left, line_right) def coord_in_img(p: Point): return margin + (p.x + 3) * grid_pixels, margin + ( grid_size * 2 - p.y - 3) * grid_pixels point_checker: Callable[[Fraction, Fraction], bool] = common.POINT_CHECKER(grid_size) def draw_point(p): pos = coord_in_img(p) r = radius_key_point draw.ellipse( (pos[0] - r, pos[1] - r, pos[0] + r, pos[1] + r), color_target_point if p in target_points else color_key_point) def draw_line(line, color, width): points = set( line.get_cross_point(frame_line, point_checker) for frame_line in frame) - {None} points = tuple(points) draw.line([coord_in_img(points[0]), coord_in_img(points[1])], color, width) # draw frame and non-frame init lines init_lines = [Line(ll[0], ll[1], ll[2]) for ll in init_figure] for line in init_lines: if (line.a, line.b) in ((0, 1), ( 1, 0 )): # integer vertical line and horizontal line, it's in the grid _color, _width = color_frame, 1 else: _color, _width = color_pre_line, 2 draw_line(line, _color, _width) # draw answer lines all_lines = set(init_lines) answer_lines = set([Line(ll[0], ll[1], ll[2]) for ll in fig]) while answer_lines: all_points = get_points_in_one_lines_set(all_lines) # try to find an answer line which contains 2 known points. for line in tuple(answer_lines): point_list = [] for point in all_points: if line.contain_point(point): point_list.append(point) if len(point_list) == 2: break # found 2 points, they can decide line else: continue # this line doesn't contain 2 found points, should continue to try line loop # 'found 2 point' will break here assert len(point_list) >= 2 draw_line(line, color_ans_line, 3) draw_point(point_list[0]) draw_point(point_list[1]) answer_lines.remove(line) all_lines.add(line) break # break line loop, continue to loop answer_lines else: # no line be found that can be generated by 2 exist points. assert False for p in target_points: draw_point(p) img.show()
#!/bin/env python from pierre import Pierre from geo import Line, Segment from math import pi from matplotlib import pyplot p = Pierre(Line(2.4485734704807562, [1, 0]), 0.3188, [ Line(1.012532492680377, [1, 0]), Line(0.6213319750943451, [0.77405, -0.36179]), Line(0.44198323099897213, [0, -0.82387]) ], 1.9) ray = Line(1.8 * pi / 4, [0.9, 0]) rays = p.getLightPath(ray) for ray in rays: if ray['inside']: seg = ray['seg'] pyplot.plot([seg.A[0], seg.B[0]], [seg.A[1], seg.B[1]], color="blue") else: pass #seg_ray = Segment(ray, y=[-1, 1]) #seg_oray = Segment(oray, y=[-1, 1]) #print seg_ray, seg_oray #pyplot.plot([seg_ray.A[0],seg_ray.B[0]], [seg_ray.A[1], seg_ray.B[1]], color="red") #pyplot.plot([seg_oray.A[0],seg_oray.B[0]], [seg_oray.A[1], seg_oray.B[1]], color="red") for face in p.getgAllFaces(): s = face['seg']
def all_lines_for_points(points): lines = set( Line.get_line_contains_points(a, b) for a, b in itertools.combinations(points, 2)) return lines
def bfs_dump_for_pythagorea(init_param_lines, coord_grid=3, max_depth=3): line_tab = {} fig_tab = set() point_tab = {} test_loop_count = 0 point_checker: Callable[[Fraction, Fraction], bool] = common.POINT_CHECKER(coord_grid) def get_ref_of_line(abc): try: ref = line_tab[abc] except KeyError: ref = line_tab[abc] = abc return ref def list_of_fig_which_has_point(point): nonlocal test_loop_count test_loop_count += 1 if test_loop_count % 10000 == 0: print(test_loop_count, time.time(), file=sys.stderr) point_vector = (point.x.numerator, point.x.denominator, point.y.numerator, point.y.denominator) try: fig_list = point_tab[point_vector] except KeyError: fig_list = point_tab[point_vector] = [] return fig_list def check_if_symmetry_point_exists_in_low_level(point): points = points_symmetry_all(point) for p in points: try: _fig_lst = point_tab[p] if _fig_lst: ll = len(_fig_lst[0]) if ll < level: return True except KeyError: continue return False init_lines = set([Line(i[0], i[1], i[2]) for i in init_param_lines]) init_points = get_points_in_one_lines_set(init_lines) init_fig = () fig_tab.add(init_fig) for p in init_points: fig_lst = list_of_fig_which_has_point(p) fig_lst.append(init_fig) # create first_level_lines, it's for speed up to generate every new potential lines for next level # for all_lines = init_lines + current_lines, # next lines will be all_point * all_point - init_lines - current_lines # and all_point = init_point + new_points; then # all_point * all_point = init_point * init_point + init_points * new_point + new_point * new_point # new_point * new_point must be the old line, init_point * init_point is pre-calculated. # so we just care init_points * new_point and pre-calculated lines. first_level_lines = all_lines_for_points(init_points) - init_lines for line in first_level_lines: get_ref_of_line((line.a, line.b, line.c)) for level in range(max_depth + 1): print(level, time.time(), file=sys.stderr) current_figure_set = [ fig for fig in fig_tab if get_fig_level(fig) == level ] current_figure_set.sort() print(level, len(current_figure_set), time.time(), file=sys.stderr) for fff in current_figure_set: for lll in fff: print(lll[0], lll[1], lll[2], end=' ', file=sys.stdout) print("", file=sys.stdout) if level == max_depth: return {} for cur_fig in current_figure_set: # find all the point and record them # should use all lines cross (all lines + init_lines) geo_lines = set([Line(i[0], i[1], i[2]) for i in cur_fig]) all_lines = geo_lines | init_lines it = itertools.product(geo_lines, all_lines) new_points = set() for line_a, line_b in it: p = line_a.get_cross_point(line_b, point_checker) if p and p not in new_points: new_points.add(p) # added into point->figure table if it's worth p_exists_in_lower = check_if_symmetry_point_exists_in_low_level( (p.x.numerator, p.x.denominator, p.y.numerator, p.y.denominator)) if not p_exists_in_lower: p_fig_list = list_of_fig_which_has_point(p) p_fig_list.append(cur_fig) # produce next level if level == max_depth: continue # no need to do for the last level new_lines = set([ Line.get_line_contains_points(p0, p1) for p0, p1 in itertools.product(new_points, new_points | init_points) ]) - {None} for line in new_lines | first_level_lines: if line and line not in all_lines: # is different with current line_param = get_ref_of_line((line.a, line.b, line.c)) new_fig = tuple(sorted(cur_fig + (line_param, ))) new_figs = figure_symmetry_all(new_fig) for fig in new_figs: if fig in fig_tab: break else: fig_tab.add(min(new_figs)) print(len(point_tab), len(fig_tab), len(line_tab), time.time(), file=sys.stderr) print(point_tab.__sizeof__(), fig_tab.__sizeof__(), line_tab.__sizeof__(), time.time(), file=sys.stderr) return point_tab