def draw(self, screen): surface = screen.surface super().draw() if self._world.view_keys: self._steering.render_aids(surface) #show heading and side if self._world.show_heading: gfxdraw.line(surface, int(self.exact_pos.x), int(self.exact_pos.y), int(self.exact_pos.x + 20 * self.heading.x), int(self.exact_pos.y + 20 * self.heading.y), pg.Color(200, 200, 200)) #steering_force if self._world.show_steering_force: f = self.steering_force / self.max_force f *= 100 gfxdraw.line(surface, int(self.exact_pos.x), int(self.exact_pos.y), int(self.exact_pos.x + f.x), int(self.exact_pos.y + f.y), pg.Color(200, 0, 0)) #hud if self._world.show_hud: self.hud.draw(screen)
def drag_sprite_draw(self, start_x, start_y, curr_x, curr_y): if (dragSpriteStyle == "rectangle"): gfxdraw.rectangle(self.surface, (start_x, start_y, curr_x, curr_y), self.color) else: gfxdraw.line(self.surface, start_x, start_y, curr_x, curr_y, self.color)
def run(self, surface): surface.set_at((31, 0), (255, 0, 0)) surface.set_at((31, 31), (255, 0, 0)) if len(self.items) > 0: bar_height = round(30 / len(self.items)) else: bar_height = 30 gfxdraw.line(surface, 31, 1, 31, 30, (0, 0, 0)) bar_height_end = (1 + bar_height) * (1 + self.selected_index) if bar_height_end > 30: bar_height_end = 30 gfxdraw.line(surface, 31, 1 + bar_height * self.selected_index, 31, bar_height_end, (0, 255, 0)) pos = 0 for item in self.items: test = Surface((31, 6)) if pos == self.selected_index and not self.buttonPressed: test.fill(Color((100, 0, 0))) elif pos == self.selected_index and self.buttonPressed: test.fill(Color((150, 0, 0))) item.run(test) surface.blit(test, (0, pos * 7)) pos += 1
def draw_universe_background(self, surface: 'pygame.Surface', *args) -> None: """ Draw stars as background. :param surface: Surface to draw :param args: Optional arguments """ t = self.menu.get_counter_attribute('t', self.menu.get_clock().get_time() * 0.001) # Draw nebulas for nebula in self.nebulas: nebula.draw(surface) nebula.rotate(nebula.get_angle() + nebula.get_attribute('delta_angle')) # Draw stars for s in self.stars: x, y, flicker = s c = int(127 * max(0.5, 1 + math.cos(t + flicker))) gfxdraw.pixel(surface, x, y, (c, c, c)) # Draw shooting stars for s in self.shooting_stars: x, y, dx, dy, theta, speed, flicker = s c = int(150 * max(0.1, max(math.sin(0.1 * t + 1.5 * math.pi + flicker), math.cos(0.1 * t + flicker)))) x = int(x) y = int(y) gfxdraw.line(surface, x, y, x + dx, y + dy, (c, c, c)) # Update velocity + window constraints s[0] = (s[0] + speed * math.cos(theta)) % self.menu.get_width() s[1] = (s[1] + speed * math.sin(theta)) % self.menu.get_height() # This line forces cache update for submenus that call this method self.menu.force_surface_cache_update()
def run(self): mandelbrot_points = get_points(100) progress = 0 x, y = 0, 0 while self.is_running: if self.flush and progress == 0.0: screen.fill(0) for event in pygame.event.get(): self.handle_event(event) for y in range(win_size_y): x, i, n, z = next(mandelbrot_points) gfxdraw.pixel(screen, x, y, self.get_color(i, n, z, x, y)) if self.show_progress_bar: gfxdraw.line(screen, x + 1, 0, x + 1, win_size_y, RED) filter_text = ', '.join( self.filters[filter_index].name for filter_index in self.active_filters ) if not filter_text: filter_text = 'none' progress = ((x * y) / (win_size_x * win_size_y)) * 100 pygame.display.set_caption( f"Mandelbrot - {progress:0>2.0f}% done - filters: {filter_text}" ) pygame.display.update() clock.tick(-1)
def drag_sprite_erase(self, start_x, start_y, curr_x, curr_y): if (dragSpriteStyle == "rectangle"): gfxdraw.rectangle(self.surface, (start_x, start_y, curr_x, curr_y), self.bgcolor) else: gfxdraw.line(self.surface, start_x, start_y, curr_x, curr_y, self.bgcolor) self.updateWindowCascade(False)
def draw_lines(window, x, y, radius, lines): """This function uses gfxdraw to draw the initial circle in white and the previously computed lines in red. """ gfxdraw.circle(window, x, y, radius, Color(255, 255, 255,255)) for p1, p2 in lines: gfxdraw.line(window, p1[0], p1[1], p2[0], p2[1], Color(255, 0, 0,255))
def draw_lines(window, x, y, radius, lines): """This function uses gfxdraw to draw the initial circle in white and the previously computed lines in red. """ gfxdraw.circle(window, x, y, radius, Color(255, 255, 255,255)) for p1, p2, color in lines: gfxdraw.line(window, p1[0], p1[1], p2[0], p2[1], color)
def _draw_gridlines(self): # отрисовка сетки for index in range(1, len(self.grid)): gfxdraw.line(self.display, 0, index * self.cellsize, self.gridsize * self.cellsize, index * self.cellsize, GRID_COLOR) for index in range(1, len(self.grid[0])): gfxdraw.line(self.display, index * self.cellsize, 0, index * self.cellsize, self.gridsize * self.cellsize, GRID_COLOR)
def render(self): super(LineShapeLayer, self).render() screen = display.get_surface() color = (255, 255, 255) if self.parent: color = self.parent.color gfxdraw.line(screen, self.x + self.app.context.offset_x, self.y + self.app.context.offset_y, self.x2 + self.app.context.offset_x, self.y2 + self.app.context.offset_y, color)
def draw(screen): last = None for this in dragdraw.seq: if last is not None: x1, y1 = last x2, y2 = this color = (255, 255, 255) if dragdraw.active: color = (0, 255, 0) gfxdraw.line(screen, x1, y1, x2, y2, color) last = this
def drawLine(point1, point2, color): global gfxSurface global screen b1 = getPoint(point1) b2 = getPoint(point2) w = math.floor(screen[0] / 2) h = math.floor(screen[1] / 2) x1, y1 = [b1[i] for i in [0,1]] x2, y2 = [b2[i] for i in [0,1]] if b1[2] == 1 and b2[2] == 1: gfxdraw.line(gfxSurface, x1, y1, x2, y2, color)
def update_screen(): pygame.draw.rect(window, (0, 0, 0), (0, 0, windowX*cellSize, windowY*cellSize)) for i in range(windowY): gfxdraw.line(window, 0,cellSize*i,windowX*cellSize,cellSize*i,whiteColor) for i in range(windowX): gfxdraw.line(window, cellSize*i,0,cellSize*i,windowY*cellSize,whiteColor) for i in range(windowX): for j in board[i]: j.draw() pygame.display.flip()
def show(self, w, h, surface): sx = lerp(self.x / self.z, -1, 1, -w, w) + int(w / 2) sy = lerp(self.y / self.z, -1, 1, -h, h) + int(h / 2) r = lerp(self.z, 0, w, 8, 0) filled_circle(surface, sx, sy, r, (255,255,255)) px = lerp(self.x / self.pz, -1, 1, -w, w) + int(w / 2) py = lerp(self.y / self.pz, -1, 1, -h, h) + int(h / 2) line(surface, px, py, sx, sy, (255,255,255)) self.pz = self.z
def draw(self, screen: pygame.display) -> None: for sensor in self.sensors: gfxdraw.line(screen, int(np.round(sensor.coords.xy[0][0])), int(np.round(sensor.coords.xy[1][0])), int(np.round(sensor.coords.xy[0][1])), int(np.round(sensor.coords.xy[1][1])), Const.COLORS.red) screen.blit( Const.FONT_SENSORS.render( f'{np.round(sensor.length if sensor.length > 0 else 0.0, decimals=1)}', True, Const.COLORS.white), (int(np.round(sensor.coords.xy[0][1])), int(np.round(sensor.coords.xy[1][1]))))
def draw(self, screen) -> None: for sensor in self.sensors: gfxdraw.line(screen, int(np.round(sensor.coords.xy[0][0])), int(np.round(sensor.coords.xy[1][0])), int(np.round(sensor.coords.xy[0][1])), int(np.round(sensor.coords.xy[1][1])), Const.COLORS['red']) screen.blit( self.font_sensors.render( f'{np.round(sensor.length if sensor.length > 0 else 0.0, decimals=1)}', True, Const.COLORS["white"]), (int(np.round(sensor.coords.xy[0][1])), int(np.round(sensor.coords.xy[1][1]))))
def draw_robot(self, screen) -> None: s_x, s_y = get_x_y(self.pos) gfxdraw.aacircle( screen, int(np.round(s_x)), int(np.round(s_y)), Const.ROBOT_RADIUS, Const.COLORS['robot'], ) # Draw orientation line ("front" of the robot) e_x, e_y = self.get_orientation_vector() gfxdraw.line(screen, int(np.round(s_x)), int(np.round(s_y)), int(np.round(e_x)), int(np.round(e_y)), Const.COLORS['green'])
def Draw(surface: pygame.Surface, list_points, color_code: tuple, type: str, font=None, int=None): if type == "line": gfxdraw.line(surface, list_points[0], list_points[1], list_points[2], list_points[3], color_code) if type == "bezier": gfxdraw.bezier(surface, list_points, 4500, color_code) if type == "dot": gfxdraw.filled_circle(surface, list_points[0], list_points[1], 5, color_code) surface.blit(font.render(str(int), True, color_code), list_points)
def render_raw(self, res: Tuple[int], frame: int) -> pygame.Surface: surface = pygame.Surface(res, pygame.SRCALPHA) loc1 = self.loc1(frame) loc2 = self.loc2(frame) thickness = self.thickness(frame) color = self.color(frame) antialias = self.antialias(frame) if antialias: gfxdraw.line(surface, *loc1, *loc2, color) else: pygame.draw.line(surface, color, loc1, loc2, thickness) return surface
def draw_man(i): if i == 1: draw.vline(scr, 500, 350, 450, (255, 255, 255)) elif i == 2: draw.aacircle(scr, 500, 320, 20, (255, 255, 255)) elif i == 3: draw.hline(scr, 490, 450, 380, (255, 255, 255)) elif i == 4: draw.hline(scr, 510, 550, 380, (255, 255, 255)) elif i == 5: draw.line(scr, 490, 450, 460, 500, (255, 255, 255)) elif i == 6: draw.line(scr, 510, 450, 540, 500, (255, 255, 255)) py.display.update() return
def update_screen(): pygame.draw.rect(window, (0, 0, 0), (0, 0, windowX * cellSize, windowY * cellSize)) for i in range(windowY): gfxdraw.line(window, 0, cellSize * i, windowX * cellSize, cellSize * i, whiteColor) for i in range(windowX): gfxdraw.line(window, cellSize * i, 0, cellSize * i, windowY * cellSize, whiteColor) for i in range(windowX): for j in board[i]: j.draw() pygame.display.flip()
def draw_all(screen, polylines, holes, triangles, emsize = 1024, zoom = 1.0, polylinecolor = green, holecolor = blue, trianglecolor = red): """This function takes the list of polylines and holes and the triangulation, and draws it in pygame. This function is pending deprecation.""" global args if trianglecolor is not None: for t in triangles: a = get_triangle_point(t, 0) b = get_triangle_point(t, 1) c = get_triangle_point(t, 2) x1 = int(ux(a) * zoom) y1 = int((emsize-uy(a)) * zoom) x2 = int(ux(b) * zoom) y2 = int((emsize-uy(b)) * zoom) x3 = int(ux(c) * zoom) y3 = int((emsize-uy(c)) * zoom) trigon(screen, x1, y1, x2, y2, x3, y3, trianglecolor) # Close the polylines loop again prior to drawing if polylinecolor is not None: for polyline in polylines: if hasattr(polyline, 'coords'): polyline = list(polyline.coords) polyline.append(polyline[0]) flipped = flip_polyline(polyline, emsize) for a, b in pairwise(flipped): x1 = int(a[0] * zoom) y1 = int(a[1] * zoom) x2 = int(b[0] * zoom) y2 = int(b[1] * zoom) line(screen, x1, y1, x2, y2, polylinecolor) # Same for holes if holecolor is not None: for hole in holes: if hasattr(hole, 'coords'): hole = list(hole.coords) hole.append(hole[0]) flipped = flip_polyline(hole, emsize) for a, b in pairwise(flipped): x1 = int(a[0] * zoom) y1 = int(a[1] * zoom) x2 = int(b[0] * zoom) y2 = int(b[1] * zoom) line(screen, x1, y1, x2, y2, holecolor) # Show result pygame.display.update()
def line(surf, start, end, color=BLACK, width=1, style=FLAT): """Draws an antialiased line on the surface.""" width = round(width, 1) if width == 1: # return pygame.draw.aaline(surf, color, start, end) return gfxdraw.line(surf, *start, *end, color) start = V2(*start) end = V2(*end) line_vector = end - start half_side = line_vector.normnorm() * width / 2 point1 = start + half_side point2 = start - half_side point3 = end - half_side point4 = end + half_side liste = [(point1.x, point1.y), (point2.x, point2.y), (point3.x, point3.y), (point4.x, point4.y)] rect = polygon(surf, liste, color) if style == ROUNDED: _ = circle(surf, start, width / 2, color) rect = merge_rects(rect, _) _ = circle(surf, end, width / 2, color) rect = merge_rects(rect, _) return rect
def draw(self, surface): aacircle(surface, 300, 300, 10, pg.Color('red')) for i,p in enumerate(self._way_points): aacircle(surface, int(p.x), int(p.y), 5, pg.Color("gold") ) if (i+1) < len(self._way_points): p2 = self._way_points[i+1] line(surface, int(p.x),int(p.y), int(p2.x),int(p2.y), pg.Color("goldenrod")) aacircle(surface, int(p2.x), int(p2.y), 5, pg.Color("gold") )
def clear(self): for i in range(3): self.field[i] = range(3) for j in range(3): self.field[i][j] = 0 gfxdraw.line(window, 0, 100, 300, 100, (255, 255, 255)) gfxdraw.line(window, 0, 200, 300, 200, (255, 255, 255)) gfxdraw.line(window, 100, 0, 100, 300, (255, 255, 255)) gfxdraw.line(window, 200, 0, 200, 300, (255, 255, 255)) pygame.display.flip()
def draw_midlines(screen, polylines, midpoints, emsize = 1024, zoom = 1.0, polylinecolor = green, midpointcolor = red): """This function takes the list of polylines and midpoints, and draws them in pygame.""" global args #for m in midpoints: #x = int(m[0] * zoom) #y = int((emsize-m[1]) * zoom) #print((x, y)) #pixel(screen, x, y, midpointcolor) # Close the polylines loop again prior to drawing for polyline in polylines: #polyline.append(polyline[0]) flipped = flip_polyline(polyline, emsize) for a, b in pairwise(flipped): x1 = int(a[0] * zoom) y1 = int(a[1] * zoom) x2 = int(b[0] * zoom) y2 = int(b[1] * zoom) line(screen, x1, y1, x2, y2, polylinecolor) pygame.display.update()
def calculate(): if dist_check(): #if the mouse is inside the circle for i in range(0,720,1): #iterate from 0 to 360 in steps of 1. x = mpos[0] x2 = cx + (radius * math.cos(i * 3.14159 / 360)) midx = (x + x2) / 2 y = mpos[1] y2 = cy + (radius * math.sin(i * 3.14159 / 360)) midy = (y + y2) / 2 d = math.sqrt(((x - x2) * (x - x2)) + ((y - y2) * (y - y2))) theta = math.atan2(x - x2, y2 - y) r = d*2 #this can any lenght. Longer is better for extreme values. x3 = midx + (r * math.cos(theta)) y3 = midy + (r * math.sin(theta)) x4 = (2 * midx) - x3 #the reflection of x3 across the ray y4 = (2 * midy) - y3 #the reflection of y3 across the ray #gfxdraw.line(screen,x,y,int(x2),int(y2),(255,0,255)) #rays #the line above draws lines from the cursor to points on the outer white circle gfxdraw.line(screen, int(midx), int(midy), int(x3), int(y3), (0, 255, 0)) # half of rotated rays gfxdraw.line(screen, int(midx), int(midy), int(x4), int(y4), (0, 255, 0)) #other side of the rotated ray #gfxdraw.filled_circle(screen, int(midx), int(midy),2, (0, 0, 255)) #ray midpoints colored blue gfxdraw.filled_circle(screen, int(x), int(y), 2, (0, 255, 0)) #green dot for cursor position.
def render(self, mode="human"): screen_width = 600 screen_height = 400 if self.screen is None: pygame.init() pygame.display.init() self.screen = pygame.display.set_mode( (screen_width, screen_height)) if self.clock is None: self.clock = pygame.time.Clock() self.surf = pygame.Surface((screen_width, screen_height)) self.surf.fill((255, 255, 255)) self.track = gfxdraw.hline(self.surf, 0, screen_width, int(screen_height / 2), (0, 0, 255)) if len(self._path) > 1: for p in range(len(self._path) - 1): gfxdraw.line( self.surf, int(self._path[p][0] * 100), int(screen_height / 2 + self._path[p][1] * 100), int(self._path[p + 1][0] * 100), int(screen_height / 2 + self._path[p + 1][1] * 100), (255, 0, 0)) self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (0, 0)) if mode == "human": pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() if mode == "rgb_array": return np.transpose(np.array(pygame.surfarray.pixels3d( self.screen)), axes=(1, 0, 2)) else: return self.isopen
def draw_tree_scaling(window, x1, y1, x2, y2, scaling, depth): if depth == 0: return x3, y3 = rotate(x1, y1, x2, y2, -90) x4, y4 = rotate(x2, y2, x1, y1, 90) gfxdraw.line(window, x1, y1, x2, y2, Color(255, 0, 0,255)) gfxdraw.line(window, x1, y1, x3, y3, Color(255, 0, 0,255)) gfxdraw.line(window, x2, y2, x4, y4, Color(255, 0, 0,255)) gfxdraw.line(window, x3, y3, x4, y4, Color(255, 0, 0,255)) angle = math.acos(scaling) * 180 / math.pi to_rot_x = x3 * (1 - scaling) + x4 * scaling to_rot_y = y3 * (1 - scaling) + y4 * scaling new_x, new_y = rotate(x3, y3, to_rot_x, to_rot_y, -angle) draw_tree_scaling(window, x3, y3, new_x, new_y, scaling, depth - 1) draw_tree_scaling(window, new_x, new_y, x4, y4, scaling, depth - 1)
def line(self, x1, y1, x2, y2, color): """直線を描画します. Parameters ---------- x1 : int 直線の始点のx座標. y1 : int 直線の始点のy座標. x2 : int 直線の終点のx座標. y2 : int 直線の終点のy座標. color : tuple of int 描画に使用される色を指定します. """ return gfx.line(self.pg.screen, x1, y1, x2, y2, color)
def draw_tree_angle(window, x1, y1, x2, y2, depth, angle): if depth == 0: return x3, y3 = rotate(x1, y1, x2, y2, -90) x4, y4 = rotate(x2, y2, x1, y1, 90) # color = Color(255, 0, 0, 255) if depth % 2 == 0 else Color(255, 255, 255, 255) colors = [ Color(255, 0, 0, 255), Color(255, 255, 255, 255), ] color = colors[depth % len(colors)] gfxdraw.line(window, x1, y1, x2, y2, color) gfxdraw.line(window, x1, y1, x3, y3, color) gfxdraw.line(window, x2, y2, x4, y4, color) gfxdraw.line(window, x3, y3, x4, y4, color) new_x, new_y = rotate(x3, y3, x4, y4, -angle) draw_tree_angle(window, x3, y3, new_x, new_y, depth - 1, angle) draw_tree_angle(window, new_x, new_y, x4, y4, depth - 1, angle)
def check_won(side, layout, active): countD = 0 countD2 = 0 for i in range(3): countX = 0 countY = 0 if (layout.field[i][i] == side): countD += 1 if (layout.field[2 - i][i] == side): countD2 += 1 for j in range(3): if (layout.field[i][j] == side): countX += 1 if (layout.field[j][i] == side): countY += 1 if (countX == 3): if (active == 1): gfxdraw.line(window, 50 + 100 * i, 0, 50 + 100 * i, 300, (255, 255, 255)) pygame.display.flip() return 1 if (countY == 3): if (active == 1): gfxdraw.line(window, 0, 50 + 100 * i, 300, 50 + 100 * i, (255, 255, 255)) pygame.display.flip() return 1 if (countD == 3): if (active == 1): gfxdraw.line(window, 0, 0, 300, 300, (255, 255, 255)) pygame.display.flip() return 1 if (countD2 == 3): if (active == 1): gfxdraw.line(window, 0, 300, 300, 0, (255, 255, 255)) pygame.display.flip() return 1 return 0
def main(file_name_map, file_name_obs, translate, zoom, num_tasks): SCREEN_SIZE = 360,240 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0,0,0) red = Color(0, 0, 0) green = Color(255, 255, 255) yellow = Color(255, 255, 0) screen.fill(black) points = load_points(file_name_map) (holes,bbpath) = load_holes(file_name_obs) polyline = [] for p in points: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] polyline.append(Point(p[0],p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## # xarr = [229, 236, 214, 285, 256, 265] # yarr = [193, 224, 246, 190, 260, 214] hole_arr = [] # obstacles = holes for h in holes: hole = [] for p in h: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0], p[1])) cdt.add_hole(hole) hole_arr.extend(hole) i = 0 xarr = [] yarr = [] while i<num_tasks: x = random.randint(1,119) y = random.randint(1, 79) if checkinPoly((x,y), bbpath): px = x*zoom + translate[0] py = y * zoom + translate[1] if(px in xarr and py in yarr): continue xarr.append(x*zoom +translate[0]) yarr.append(y*zoom +translate[1]) cdt.add_point(Point(x*zoom +translate[0],y*zoom +translate[1])) i = i + 1 ## Step 3: Triangulate ## triangles = cdt.triangulate() print "Elapsed time (ms) = " + str(clock()*1000.0) # The Main Event Loop done = False vertexArr = [] g = Graph(len(points) + len(hole_arr) + len(xarr)) for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) i1 = -1; i2 = -1; i3 = -1; for i in range(len(vertexArr)): if(vertexArr[i].x == x1 and vertexArr[i].y == y1): i1 = i elif(vertexArr[i].x == x2 and vertexArr[i].y == y2): i2 = i elif(vertexArr[i].x == x3 and vertexArr[i].y == y3): i3 = i if(i1 == -1): vertexArr.append(Point(x1, y1)) i1 = len(vertexArr) - 1 if(i2 == -1): vertexArr.append(Point(x2, y2)) i2 = len(vertexArr) - 1 if(i3 == -1): vertexArr.append(Point(x3, y3)) i3 = len(vertexArr) - 1 trigon(screen, x1, y1, x2, y2, x3, y3, red) g.addEdge(i1, i2, int(dist(Point(x1, y1), Point(x2, y2)))) g.addEdge(i3, i2, int(dist(Point(x2, y2), Point(x3, y3)))) g.addEdge(i1, i3, int(dist(Point(x3, y3), Point(x1, y1)))) g.FloydWarshall() guard = [] print "look " + str(len(xarr)) + str(len(vertexArr)) for i in range(g.sz): for j in range(len(xarr)): if(xarr[j] == int(vertexArr[i].x) and yarr[j] == int(vertexArr[i].y)): guard.append(i) g.guard = guard (rcdtgrid, parts, mapping) = g.GraphReduction() colors = [] for i in range(len(parts)): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) colors.append(Color(r, g, b)) flag = 0 while not done: # Draw outline for i in range(len(points)): j = i+1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary for obs in holes: for i in range(len(obs)): j = i+1 if i < len(obs) - 1 else 0 x1 = int(obs[i][0]) y1 = int(obs[i][1]) x2 = int(obs[j][0]) y2 = int(obs[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(vertexArr)): for j in range(len(vertexArr)): if(i not in mapping): continue if(j not in mapping): continue if parts[mapping.index(i)] == 1: pygame.draw.circle(screen, (255, 0, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) elif parts[mapping.index(i)] == 0: pygame.draw.circle(screen, (0, 255, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) elif parts[mapping.index(i)] == 2: pygame.draw.circle(screen, (0, 0, 255), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) else: pygame.draw.circle(screen, (255, 255, 255), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) if(rcdtgrid[i][j] == 1000000007 or parts[mapping.index(i)] != parts[mapping.index(j)]): continue if not flag: print mapping.index(i) line(screen, int(vertexArr[i].x), int(vertexArr[i].y), int(vertexArr[j].x), int(vertexArr[j].y), colors[parts[mapping.index(i)]]) flag = 1 # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get( ) for e in events: if( e.type == QUIT ): done = True break elif (e.type == KEYDOWN): if( e.key == K_ESCAPE ): done = True break if( e.key == K_f ): pygame.display.toggle_fullscreen() return
def main(file_name, translate, zoom): SCREEN_SIZE = 800,600 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0,0,0) red = Color(0, 0, 0) green = Color(255, 255, 255) yellow = Color(255, 255, 0) screen.fill(black) points = load_points(file_name) polyline = [] for p in points: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] polyline.append(Point(p[0],p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## if file_name == "data/dude.dat": hole = [] for p in head_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) hole = [] for p in chest_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) # Add an interior Steiner point x = 361*zoom + translate[0] y = 381*zoom + translate[1] cdt.add_point(Point(x, y)) # xarr = [323, 372, 334, 424, 342, 352, 100, 567] # yarr = [395, 410, 645, 546, 620, 534, 370, 387] xarr = [229, 236, 214, 285, 256, 265] yarr = [193, 224, 246, 190, 260, 214] if file_name == "data/test.dat": for i in range(len(xarr)): x = xarr[i]*zoom + translate[0] y = yarr[i]*zoom + translate[1] xarr[i] = x yarr[i] = y cdt.add_point(Point(x, y)) if file_name == "data/map.dat": for i in range(len(xarr)): x = xarr[i]*zoom + translate[0] y = yarr[i]*zoom + translate[1] xarr[i] = x yarr[i] = y cdt.add_point(Point(x, y)) hole_arr = [] if file_name == "data/map.dat": for obs_hole in obstacle: hole = [] for p in obs_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) cdt.add_hole(hole) hole_arr.extend(hole) ## ## Step 3: Triangulate ## triangles = cdt.triangulate() print "Elapsed time (ms) = " + str(clock()*1000.0) # The Main Event Loop done = False vertexArr = [] g = Graph(len(points) + len(hole_arr) + len(xarr)) for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) i1 = -1; i2 = -1; i3 = -1; for i in range(len(vertexArr)): if(vertexArr[i].x == x1 and vertexArr[i].y == y1): i1 = i elif(vertexArr[i].x == x2 and vertexArr[i].y == y2): i2 = i elif(vertexArr[i].x == x3 and vertexArr[i].y == y3): i3 = i if(i1 == -1): vertexArr.append(Point(x1, y1)) i1 = len(vertexArr) - 1 if(i2 == -1): vertexArr.append(Point(x2, y2)) i2 = len(vertexArr) - 1 if(i3 == -1): vertexArr.append(Point(x3, y3)) i3 = len(vertexArr) - 1 trigon(screen, x1, y1, x2, y2, x3, y3, red) g.addEdge(i1, i2, int(dist(Point(x1, y1), Point(x2, y2)))) g.addEdge(i3, i2, int(dist(Point(x2, y2), Point(x3, y3)))) g.addEdge(i1, i3, int(dist(Point(x3, y3), Point(x1, y1)))) g.FloydWarshall() guard = [] print "look " + str(len(xarr)) + str(len(vertexArr)) for i in range(g.sz): for j in range(len(xarr)): if(xarr[j] == int(vertexArr[i].x) and yarr[j] == int(vertexArr[i].y)): guard.append(i) g.guard = guard (rcdtgrid, parts, mapping) = g.GraphReduction() colors = [] for i in range(len(parts)): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) colors.append(Color(r, g, b)) flag = 0 while not done: # Draw outline for i in range(len(points)): j = i+1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary if file_name == "data/dude.dat": for i in range(len(head_hole)): j = i+1 if i < len(head_hole) - 1 else 0 x1 = int(head_hole[i][0]) y1 = int(head_hole[i][1]) x2 = int(head_hole[j][0]) y2 = int(head_hole[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(chest_hole)): j = i+1 if i < len(chest_hole) - 1 else 0 x1 = int(chest_hole[i][0]) y1 = int(chest_hole[i][1]) x2 = int(chest_hole[j][0]) y2 = int(chest_hole[j][1]) line(screen, x1, y1, x2, y2, green) if file_name == "data/map.dat": for obs in obstacle: for i in range(len(obs)): j = i+1 if i < len(obs) - 1 else 0 x1 = int(obs[i][0]) y1 = int(obs[i][1]) x2 = int(obs[j][0]) y2 = int(obs[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(vertexArr)): for j in range(len(vertexArr)): if(i not in mapping): continue if(j not in mapping): continue if parts[mapping.index(i)] == 1: pygame.draw.circle(screen, (255, 0, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) elif parts[mapping.index(i)] == 0: pygame.draw.circle(screen, (0, 255, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) else: pygame.draw.circle(screen, (0, 0, 255), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) if(rcdtgrid[i][j] == 1000000007):# or parts[mapping.index(i)] != parts[mapping.index(j)]): continue if not flag: print mapping.index(i) line(screen, int(vertexArr[i].x), int(vertexArr[i].y), int(vertexArr[j].x), int(vertexArr[j].y), colors[parts[mapping.index(i)]]) flag = 1 # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get( ) for e in events: if( e.type == QUIT ): done = True break elif (e.type == KEYDOWN): if( e.key == K_ESCAPE ): done = True break if( e.key == K_f ): pygame.display.toggle_fullscreen() return
def main(file_name, translate, zoom): SCREEN_SIZE = 800,600 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0,0,0) red = Color(255, 0, 0) green = Color(0, 255, 0) screen.fill(black) points = load_points(file_name) polyline = [] for p in points: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] polyline.append(Point(p[0],p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## if file_name == "data/dude.dat": hole = [] for p in head_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) hole = [] for p in chest_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) # Add an interior Steiner point x = 361*zoom + translate[0] y = 381*zoom + translate[1] cdt.add_point(Point(x, y)) ## ## Step 3: Triangulate ## triangles = cdt.triangulate() print "Elapsed time (ms) = " + str(clock()*1000.0) # The Main Event Loop done = False while not done: # Draw triangles for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) trigon(screen, x1, y1, x2, y2, x3, y3, red) # Draw outline for i in range(len(points)): j = i+1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary if file_name == "data/dude.dat": for i in range(len(head_hole)): j = i+1 if i < len(head_hole) - 1 else 0 x1 = int(head_hole[i][0]) y1 = int(head_hole[i][1]) x2 = int(head_hole[j][0]) y2 = int(head_hole[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(chest_hole)): j = i+1 if i < len(chest_hole) - 1 else 0 x1 = int(chest_hole[i][0]) y1 = int(chest_hole[i][1]) x2 = int(chest_hole[j][0]) y2 = int(chest_hole[j][1]) line(screen, x1, y1, x2, y2, green) # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get( ) for e in events: if( e.type == QUIT ): done = True break elif (e.type == KEYDOWN): if( e.key == K_ESCAPE ): done = True break if( e.key == K_f ): pygame.display.toggle_fullscreen() return
def draw_line(p1, p2, color, global_coords=True): if global_coords: p1 = world_to_win_pt(p1, c.player.center) p2 = world_to_win_pt(p2, c.player.center) gfxdraw.line(screen, int(p1[0]), int(p1[1]), int(p2[0]), int(p2[1]), color)
def _draw_line(self, x1, y1, x2, y2, c): if drawing == "gfxdraw": gfxdraw.line(self.screen, x1, y1, x2, y2, c) elif drawing == "draw": pygame.draw.line(self.screen, c, (x1, y1), (x2, y2))
def line(self, start_pos, end_pos, color): """Draws a line.""" gfxdraw.line(self.screen, *start_pos, *end_pos, color)
def background(): pos = 0 for backcolor in range(150,0,-2): gfx.box(Fenster,(0,pos,800,height/15),(backcolor+100,backcolor+100,backcolor+100)) pos += height/15 gfx.line(Fenster ,int(width/2),0,int(width/2),height,(200,0,0))