def main(): # creating a new image img = Image.new('RGB', (img_width, img_height), (20,20,20)) # creating dark grey background pixels = img.load() # sphere 1 definition center1 = Point(120,120, -50) radius1 = 60 color1 = RGB_Float(0.0, 0.0, 1.0) # sphere 2 definition center2 = Point(240, 180, -50) radius2 = 50 color2 = RGB_Float(0.0, 1.0, 0.0) # sphere 3 definition center3 = Point(20, 50, -50) radius3 = 20 color3 = RGB_Float(1.0, 1.0, 0.0) # init spheres sphere1 = Sphere(center1, radius1, color1) sphere2 = Sphere(center2, radius2, color2) sphere3 = Sphere(center3, radius3, color3) # creating sphere list sphere_list = [sphere1, sphere2, sphere3] # running raycasting algorithm rayCasting(pixels, sphere_list) # displaying the image img.save("result", "PNG") img.show()
def test_cube_stl(self): far = Point(-20, 16, -17) near = Point(18, -15, 19) b1 = Box(near, far) canvas = STLCanvas() canvas.add_shape(b1) canvas.write_stl('example_test_cube.stl', make_positive=False)
def render(self, override_size=None) -> str: if override_size: corner = math.floor(override_size/2) bottom_left = Point(-corner, -corner) top_right = Point(corner, corner) else: xs, ys = zip(*self.panels.keys()) xs += (self.location.x, ) ys += (self.location.y, ) bottom_left = Point(min(xs), min(ys)) top_right = Point(max(xs), max(ys)) def _to_np_coords(pt: Union[Point, Tuple[int, int]]): try: out = pt - bottom_left except TypeError: out = Point(*pt) - bottom_left return tuple(reversed(out.tuple)) size = _to_np_coords(top_right + Point(1, 1)) map_ = np.zeros(size, dtype=int) for key, value in self.panels.items(): map_[_to_np_coords(key)] = value render_map = {0: '.', 1: '#'} string = array_to_string(map_, render_map, overrides=[(_to_np_coords(self.location), self.bot_chars[self.direction.tuple])]) return string
def task2(intcode): get_output = make_program(intcode) z = np.zeros((1800, 1800)) left, right = Point(32, 29), Point(35, 29) lefts, rights = [], [] for i in range(1600): z[left.y, left.x:right.x + 1] = 1 x, y = left y += 1 while not get_output(x, y): x += 1 left = Point(x, y) x, y = right y += 1 while get_output(x, y): x += 1 right = Point(x - 1, y) lefts.append(left) rights.append(right) def is_square(x, y): return z[y + 99, x + 99] == 1 and z[y + 99, x] and z[y, x + 99] for l, r in zip(lefts, rights): if r.x - l.x > 100: for i in range(l.x, r.x): if is_square(i, r.y): pt = Point(i, r.y) return pt.x * 10000 + pt.y
def draw_line(image, begin, end, color): '''Draw straight line begin point to end point''' '''with given color''' if not (begin.x == end.x and begin.y == end.y): steep = False # if line is steep, we transpose the image if abs(begin.x - end.x) < abs(begin.y - end.y): begin = Point(x=begin.y, y=begin.x, z=begin.z) end = Point(x=end.y, y=end.x, z=end.z) steep = True if begin.x > end.x: (x, y, _) = begin begin = Point(x=end.x, y=end.y, z=begin.z) end = Point(x=x, y=y, z=end.z) (dx, dy) = (end.x - begin.x, end.y - begin.y) derror = abs(dy / dx) error = 0 y = begin.y for x in range(begin.x, end.x): set_pixel(image, (y, x) if steep else (x, y), color) error = error + derror if error > 0.5: y = y + (1 if end.y > begin.y else -1) error = error - 1
def port_open_panel_expedition(): random_sleep(1) port_open_main_sortie() print("port_open_panel_expedition") random_click(Point(594, 170), Point(750, 340)) wait("/kcsapi/api_get_member/mission") random_sleep(2.2) # 动画时间
def dock_back_to_port(): print("dock_back_to_port") point = random_point(Point(30, 35), Point(75, 70)) point.click() request = wait("/kcsapi/api_port/port") random_sleep(1) return request
def get_neigh(i, j): return [ Point(i, j + 1), Point(i + 1, j), Point(i, j - 1), Point(i - 1, j), ]
def is_point_an_eye(board, point, color): if board.get(point) is not None: return False for neighbor in point.neighbors(): if board.is_on_grid(neighbor): neighbor_color = board.get(neighbor) if neighbor_color != color: return False friendly_corners = 0 off_board_corners = 0 corners = [ Point(point.row - 1, point.col - 1), Point(point.row - 1, point.col + 1), Point(point.row + 1, point.col - 1), Point(point.row + 1, point.col + 1), ] for corner in corners: if board.is_on_grid(corner): corner_color = board.get(corner) if corner_color == color: friendly_corners += 1 else: off_board_corners += 1 if off_board_corners > 0: return off_board_corners + friendly_corners == 4 return friendly_corners >= 3
def __init__(self, points, brd_color, bg_color): points = [Point(*p) for p in mbr(points)] self._width = (points[1].x() - points[0].x()) / 2 self._height = (points[1].y() - points[0].y()) / 2 self.location = Point(points[0].x() + self._width, points[0].y() + self._height) super(Rhombus, self).__init__(points, brd_color, bg_color)
def __init__(self, pos: tuple, type_mvt: list, texte: str, dir_: int = 1, sprite: str = 'dad', then: callable = None) -> None: self.pos = Point(*[t * TILE_SIZE for t in pos]) self.type_mvt = type_mvt self.font = ree.load_font(POLICE_PATH, POL_NORMAL_TAILLE) self.cur_scheme = 0 self.real_pos = Point() self.speed = TILE_SIZE self.speak = False self.is_moving = False self._a_parcouru = 0 self.dir = dir_ self.mdt = 0 self.orientation = BAS self.sprites_anim = PlayerAnimator( os.path.join("..", "assets", "pnj", sprite)) self.sprites_anim.load() self.sprites_anim.set_speed(20) self.perso = None self.texte = texte self.on_speak = None self.then = then self._screenshot = None
def draw(self): rect = self.graphics.get_rect() self.graphics.clear_screen(gutils.COLOR_DARK_GREEN) center = Point(rect.width / 2, rect.height / 2) self.draw_card(self.card_back, Card(1, DIAMONDS, 300, center.y)) # draw computer's cards pc_hand = self.game.get_computer_cards() OVERLAP = 60 # how much should cards overlap when drawn y = 30 + GameDisplay.CARD_HEIGHT / 2 x = center.x - ((GameDisplay.CARD_WIDTH + len(pc_hand) * OVERLAP) / 2) for card in pc_hand: card.pos = Point(x, y) self.draw_card(self.card_back, card) x += OVERLAP hand = self.game.get_human_cards() y = rect.height - 30 - GameDisplay.CARD_HEIGHT / 2 x = center.x - ((GameDisplay.CARD_WIDTH + len(pc_hand) * OVERLAP) / 2) for card in hand: card.pos = Point(x, y) image = self.card_images[str(card)] self.draw_card(image, card) x += OVERLAP self.draw_crib() self.graphics.flip()
def place_stirrer_in_pr2_hand(self, teleport=True): cup_r = k*-0.07 height_above = k*0.6 start_x = 0 start_y = 0 above_loc = Point(start_x,start_y,height_above) if teleport: self.set_hand() else: self.move_arm_to_point(above_loc) p.addUserDebugLine(above_loc, [0,0,0]) self.base_world.toggle_real_time() self.set_grip(self.armID) if not teleport: num_steps = 8 desired_end_height=0.31 dz_in_loc = height_above - desired_end_height step_size = dz_in_loc/num_steps for z in range(1,num_steps+1): #fake one indexing in_loc = Point(start_x,start_y,height_above - z*step_size) self.move_arm_to_point(in_loc) #stirring motion #in_loc = Point(cup_r,cup_r,k*0.35) self.move_arm_to_point(in_loc) p.addUserDebugLine(in_loc, [0,0,0])
def factory_destroy_do_destory(): print("factory_destroy_do_destory") point = random_point(Point(641, 420), Point(743, 452)) point.click() random_sleep(4) wait(("/kcsapi/api_req_kousyou/destroyship", "/kcsapi/api_req_kousyou/destroyitem2"))
def port_open_panel_expedition(): print("port_open_panel_expedition") random_click(Point(161, 211), Point(238, 298)) random_sleep(1) random_click(Point(594, 140), Point(765, 300)) wait("/kcsapi/api_get_member/mission") random_sleep(1.2) # 动画时间
def port_open_panel_sortie(): random_sleep(2.1) port_open_main_sortie() point = random_point(Point(144, 175), Point(313, 335)) point.click() wait("/kcsapi/api_get_member/mapinfo") random_sleep(2.5) # 动画时间
def get_response_including_barcode(barcode_coords: BoundingBox, first_response_coords: BoundingBox, page_size: Size) -> BoundingBox: ret_bb = BoundingBox( Point(0, barcode_coords.top_left.y), Point(barcode_coords.bottom_right.x, barcode_coords.bottom_right.y + first_response_coords.height)) return ret_bb.add_padding(15, page_size)
def test_triangle_area(): from utils import Triangle, Point p1 = Point(1, 1) p2 = Point(2, 1) p3 = Point(0, 0) tri = Triangle(p1, p2, p3) area = tri.get_area() assert area == 0.5
def port_open_panel_sortie(): print("port_open_panel_sortie") point = random_point(Point(161, 211), Point(238, 298)) point.click() random_sleep(1) point.click() wait("/kcsapi/api_get_member/mapinfo") random_sleep(1.2) # 动画时间
def test_trapezoid_inside(): from utils import Trapezoid, Point lines = [[1, 1, 2, 1], [0, 0, 2, 0]] trapez = Trapezoid(lines) targets = [Point(1, 1), Point(0.5, 0.5)] res = [] for target in targets: res.append(trapez.inside(target)) assert all(res) == True
def test_trapezoid_outside(): from utils import Trapezoid, Point lines = [[1, 1, 2, 1], [0, 0, 2, 0]] trapez = Trapezoid(lines) targets = [Point(2, 3), Point(-1, 0.5)] res = [] for target in targets: res.append(trapez.inside(target)) assert res == [False] * len(res)
def sortie_confirm(): print("sortie_confirm") point = random_point(Point(638, 450-22), Point(712, 481-22)) point.click() random_sleep(0.6) point.click() request = wait("/kcsapi/api_req_map/start") random_sleep(1) # 动画时间 return request
def draw(self, painter): painter.setPen(self.get_pen()) painter.setBrush(QBrush(self.bg_color)) p = self.points[0] np1 = Point(p.x() + self._width, p.y()) np2 = Point(p.x() + 2 * self._width, p.y() + self._height) np3 = Point(p.x() + self._width, p.y() + 2 * self._height) np4 = Point(p.x(), p.y() + self._height) painter.drawPolygon(np1, np2, np3, np4)
def supply_first_ship(): print("supply_first_ship") point = random_point(Point(175, 150), Point(300, 180)) point.click() random_sleep(0.8) point = random_point(Point(660, 430), Point(748, 455)) point.click() wait("/kcsapi/api_req_hokyu/charge") random_sleep(1) # 动画时间
def merge_temporal(images, alignment): weight = hl.Func("merge_temporal_weights") total_weight = hl.Func("merge_temporal_total_weights") output = hl.Func("merge_temporal_output") ix, iy, tx, ty, n = hl.Var('ix'), hl.Var('iy'), hl.Var('tx'), hl.Var('ty'), hl.Var('n') rdom0 = hl.RDom([(0, 16), (0, 16)]) rdom1 = hl.RDom([(1, images.dim(2).extent() - 1)]) imgs_mirror = hl.BoundaryConditions.mirror_interior(images, [(0, images.width()), (0, images.height())]) layer = box_down2(imgs_mirror, "merge_layer") offset = Point(alignment[tx, ty, n]).clamp(Point(MINIMUM_OFFSET, MINIMUM_OFFSET), Point(MAXIMUM_OFFSET, MAXIMUM_OFFSET)) al_x = idx_layer(tx, rdom0.x) + offset.x / 2 al_y = idx_layer(ty, rdom0.y) + offset.y / 2 ref_val = layer[idx_layer(tx, rdom0.x), idx_layer(ty, rdom0.y), 0] alt_val = layer[al_x, al_y, n] factor = 8.0 min_distance = 10 max_distance = 300 # max L1 distance, otherwise the value is not used distance = hl.sum(hl.abs(hl.cast(hl.Int(32), ref_val) - hl.cast(hl.Int(32), alt_val))) / 256 normal_distance = hl.max(1, hl.cast(hl.Int(32), distance) / factor - min_distance / factor) # Weight for the alternate frame weight[tx, ty, n] = hl.select(normal_distance > (max_distance - min_distance), 0.0, 1.0 / normal_distance) total_weight[tx, ty] = hl.sum(weight[tx, ty, rdom1]) + 1 offset = Point(alignment[tx, ty, rdom1]) al_x = idx_im(tx, ix) + offset.x al_y = idx_im(ty, iy) + offset.y ref_val = imgs_mirror[idx_im(tx, ix), idx_im(ty, iy), 0] alt_val = imgs_mirror[al_x, al_y, rdom1] # Sum all values according to their weight, and divide by total weight to obtain average output[ix, iy, tx, ty] = hl.sum(weight[tx, ty, rdom1] * alt_val / total_weight[tx, ty]) + ref_val / total_weight[ tx, ty] weight.compute_root().parallel(ty).vectorize(tx, 16) total_weight.compute_root().parallel(ty).vectorize(tx, 16) output.compute_root().parallel(ty).vectorize(ix, 32) return output
def sortie_confirm(): print("sortie_confirm") point = random_point(Point(593, 466), Point(768, 490)) point.click() random_sleep(1.6) point = random_point(Point(534, 466), Point(694, 490)) point.click() request = wait("/kcsapi/api_req_map/start") random_sleep(1) # 动画时间 return request
def test_rectangle_stl(self): nw = Point(-20, 2, -17) sw = Point(-20, 2, 19) se = Point(18, 2, 19) ne = Point(18, 2, -17) r1 = Rectangle(nw, sw, se, ne) canvas = STLCanvas() canvas.add_shape(r1) canvas.write_stl('example_test_rect.stl', make_positive=False)
def _setup(self): import random from utils import Point p = self._parameters self._world.setup(p) self._jam_progression = [] assert p.n_agents <= p.grid_width * p.grid_height * p.junction_capacity, \ "not enough room for agent start points with given grid size and junction capacity" def random_point_except(w, h, p): r = p while r == p: r = (random.randint(0, w - 1), random.randint(0, h - 1)) return r sp_list, ep_list = [], [] if not self._parameters.routes: # create a key for every location, then select positions from it and # remove locations which have been assigned the maximum number of times choices = {(x, y): p.junction_capacity for x in xrange(p.grid_width) for y in xrange(p.grid_height)} while len(sp_list) < p.n_agents: rk = random.choice(choices.keys()) if choices[rk] > 1: choices[rk] -= 1 else: del choices[rk] sp_list.append(Point(rk[0], rk[1])) ep = random_point_except(p.grid_width, p.grid_height, rk) ep_list.append(Point(ep[0], ep[1])) else: routes = self._parameters.routes assert len( routes ) == p.n_agents, "number of given routes not equal to n_agents parameter" for i in xrange(p.n_agents): r = routes[i] sp_list.append(r[0]) ep_list.append(r[1]) cars = self._world.get_agents() assert len( cars ) == p.n_agents, "number of agents not equal to simulation parameter value" for i in xrange(p.n_agents): spn = self._world.get_grid().get_item_at(sp_list[i]) epn = self._world.get_grid().get_item_at(ep_list[i]) cars[i].set_route(spn, epn) if not self._parameters.routes: # store routes in parameters if not already there route_points = [a.get_route() for a in self._world.get_agents()] self._parameters.routes = [(s, e) for (s, e) in route_points]
def generate(self, random): # For now, hard code 2 players self.generate_starting_area( random, self.players.keys()[0], Point(World.MAP_SIZE - World.STARTING_AREA_SIZE), Point(World.MAP_SIZE)) self.generate_starting_area(random, self.players.keys()[1], Point(0), Point(World.STARTING_AREA_SIZE))
def __init__(self, points, brd_color, bg_color): if len(points) > 2: points = [Point(*p) for p in mbr(points)] self._radx = (points[1].x() - points[0].x()) / 2 self._rady = (points[1].y() - points[0].y()) / 2 self.location = Point(points[0].x() + self._radx, points[0].y() + self._rady) else: self._radx = distance(points[0], points[1]) self._rady = self._radx self.location = points[0] super(Ellipse, self).__init__(points, brd_color, bg_color)